Пример #1
0
            public Logic(Shape shape, Merge <TIn, TOut> stage) : base(shape)
            {
                _stage            = stage;
                _runningUpstreams = _stage._inputPorts;
                _pendingQueue     = FixedSizeBuffer.Create <Inlet <TIn> >(_stage._inputPorts);

                var outlet = _stage.Out;

                foreach (var inlet in _stage.Shape.Ins)
                {
                    SetHandler(inlet, onPush: () =>
                    {
                        if (IsAvailable(outlet))
                        {
                            if (!IsPending)
                            {
                                Push(outlet, Grab(inlet));
                                TryPull(inlet);
                            }
                        }
                        else
                        {
                            _pendingQueue.Enqueue(inlet);
                        }
                    },
                               onUpstreamFinish: () =>
                    {
                        if (_stage._eagerComplete)
                        {
                            foreach (var i in _stage.Shape.Ins)
                            {
                                Cancel(i);
                            }
                            _runningUpstreams = 0;
                            if (!IsPending)
                            {
                                CompleteStage();
                            }
                        }
                        else
                        {
                            _runningUpstreams--;
                            if (AreUpstreamsClosed && !IsPending)
                            {
                                CompleteStage();
                            }
                        }
                    });
                }

                SetHandler(outlet, onPull: () =>
                {
                    if (IsPending)
                    {
                        DequeueAndDispatch();
                    }
                });
            }