Rx\Operator\ReduceOperator::__invoke PHP Метод

__invoke() публичный Метод

public __invoke ( Rx\ObservableInterface $observable, Rx\ObserverInterface $observer, Rx\SchedulerInterface $scheduler = null ) : Rx\DisposableInterface
$observable Rx\ObservableInterface
$observer Rx\ObserverInterface
$scheduler Rx\SchedulerInterface
Результат Rx\DisposableInterface
    public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
    {
        $hasAccumulation = false;
        $accumulation = null;
        $hasValue = false;
        $cbObserver = new CallbackObserver(function ($x) use($observer, &$hasAccumulation, &$accumulation, &$hasValue) {
            $hasValue = true;
            try {
                if ($hasAccumulation) {
                    $accumulation = call_user_func($this->accumulator, $accumulation, $x);
                } else {
                    $accumulation = $this->hasSeed ? call_user_func($this->accumulator, $this->seed, $x) : $x;
                    $hasAccumulation = true;
                }
            } catch (\Exception $e) {
                $observer->onError($e);
            }
        }, function ($e) use($observer) {
            $observer->onError($e);
        }, function () use($observer, &$hasAccumulation, &$accumulation, &$hasValue) {
            if ($hasValue) {
                $observer->onNext($accumulation);
            } else {
                $this->hasSeed && $observer->onNext($this->seed);
            }
            if (!$hasValue && !$this->hasSeed) {
                $observer->onError(new \Exception("Missing Seed and or Value"));
            }
            $observer->onCompleted();
        });
        return $observable->subscribe($cbObserver, $scheduler);
    }