Exemplo n.º 1
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $hasValue = false;
     $hasAccumulation = false;
     $accumulation = $this->seed;
     $hasSeed = $this->seed !== null;
     $cbObserver = new CallbackObserver(function ($x) use($observer, &$hasAccumulation, &$accumulation, &$hasSeed, &$hasValue) {
         $hasValue = true;
         if ($hasAccumulation) {
             $accumulation = call_user_func($this->tryCatch($this->accumulator), $accumulation, $x);
         } else {
             $accumulation = $hasSeed ? call_user_func($this->tryCatch($this->accumulator), $this->seed, $x) : $x;
             $hasAccumulation = true;
         }
         if ($accumulation instanceof \Exception) {
             $observer->onError($accumulation);
             return;
         }
         $observer->onNext($accumulation);
     }, [$observer, 'onError'], function () use($observer, &$hasValue, &$hasSeed) {
         if (!$hasValue && $hasSeed) {
             $observer->onNext($this->seed);
         }
         $observer->onCompleted();
     });
     return $observable->subscribe($cbObserver, $scheduler);
 }
Exemplo n.º 2
0
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $singleDisposable = new SingleAssignmentDisposable();
     $disposable = new CompositeDisposable();
     $disposable->add($singleDisposable);
     $callbackObserver = new CallbackObserver(function (Observable $x) use($disposable, $scheduler, $observer) {
         if ($this->hasCurrent) {
             return;
         }
         $this->hasCurrent = true;
         $inner = new SingleAssignmentDisposable();
         $disposable->add($inner);
         $innerSub = $x->subscribe(new CallbackObserver([$observer, "onNext"], [$observer, "onError"], function () use($disposable, $inner, $observer) {
             $disposable->remove($inner);
             $this->hasCurrent = false;
             if ($this->isStopped && $disposable->count() === 1) {
                 $observer->onCompleted();
             }
         }), $scheduler);
         $inner->setDisposable($innerSub);
     }, [$observer, 'onError'], function () use($disposable, $observer) {
         $this->isStopped = true;
         if (!$this->hasCurrent && $disposable->count() === 1) {
             $observer->onCompleted();
         }
     });
     $singleDisposable->setDisposable($observable->subscribe($callbackObserver, $scheduler));
     return $disposable;
 }
Exemplo n.º 3
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $isOpen = false;
     /** @var DisposableInterface $otherDisposable */
     $otherDisposable = $this->other->subscribe(new CallbackObserver(function ($x) use(&$isOpen, &$otherDisposable) {
         $isOpen = true;
         $otherDisposable->dispose();
     }, function ($e) use($observer) {
         $observer->onError($e);
     }, function () use(&$otherDisposable) {
         $otherDisposable->dispose();
     }));
     $sourceDisposable = $observable->subscribe(new CallbackObserver(function ($x) use($observer, &$isOpen) {
         if ($isOpen) {
             $observer->onNext($x);
         }
     }, function ($e) use($observer) {
         $observer->onError($e);
     }, function () use($observer, &$isOpen) {
         if ($isOpen) {
             $observer->onCompleted();
         }
     }));
     return new CompositeDisposable([$otherDisposable, $sourceDisposable]);
 }
Exemplo n.º 4
0
 /**
  * @param \Rx\ObservableInterface $observable
  * @param \Rx\ObserverInterface $observer
  * @param \Rx\SchedulerInterface $scheduler
  * @return \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);
 }
Exemplo n.º 5
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     if ($this->scheduler !== null) {
         $scheduler = $this->scheduler;
     }
     $innerDisp = new SerialDisposable();
     $disp = $observable->subscribe(new CallbackObserver(function ($x) use($innerDisp, $observer, $scheduler) {
         $now = $scheduler->now();
         if ($this->nextSend <= $now) {
             $innerDisp->setDisposable(new EmptyDisposable());
             $observer->onNext($x);
             $this->nextSend = $now + $this->throttleTime - 1;
             return;
         }
         $newDisp = Observable::just($x)->delay($this->nextSend - $now)->subscribe(new CallbackObserver(function ($x) use($observer, $scheduler) {
             $observer->onNext($x);
             $this->nextSend = $scheduler->now() + $this->throttleTime - 1;
             if ($this->completed) {
                 $observer->onCompleted();
             }
         }, [$observer, 'onError']), $scheduler);
         $innerDisp->setDisposable($newDisp);
     }, function (\Exception $e) use($observer, $innerDisp) {
         $innerDisp->dispose();
         $observer->onError($e);
     }, function () use($observer) {
         $this->completed = true;
         if ($this->nextSend === 0) {
             $observer->onCompleted();
         }
     }), $scheduler);
     return new CompositeDisposable([$disp, $innerDisp]);
 }
Exemplo n.º 6
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $previousMax = null;
     $comparing = false;
     return $observable->subscribe(new CallbackObserver(function ($x) use(&$comparing, &$previousMax, $observer) {
         if (!$comparing) {
             $comparing = true;
             $previousMax = $x;
             return;
         }
         try {
             $result = call_user_func($this->comparer, $x, $previousMax);
             if ($result > 0) {
                 $previousMax = $x;
             }
         } catch (\Exception $e) {
             $observer->onError($e);
         }
     }, [$observer, 'onError'], function () use(&$comparing, &$previousMax, $observer) {
         if ($comparing) {
             $observer->onNext($previousMax);
             $observer->onCompleted();
             return;
         }
         $observer->onError(new \Exception("Empty"));
     }), $scheduler);
 }
Exemplo n.º 7
0
 /**
  * @param \Rx\ObservableInterface $observable
  * @param \Rx\ObserverInterface $observer
  * @param \Rx\SchedulerInterface $scheduler
  * @return \Rx\DisposableInterface
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $group = new CompositeDisposable();
     $isStopped = false;
     $sourceSubscription = new SingleAssignmentDisposable();
     $group->add($sourceSubscription);
     $callbackObserver = new CallbackObserver(function (ObservableInterface $innerSource) use(&$group, &$isStopped, $observer, &$scheduler) {
         $innerSubscription = new SingleAssignmentDisposable();
         $group->add($innerSubscription);
         $innerSubscription->setDisposable($innerSource->subscribe(new CallbackObserver(function ($nextValue) use($observer) {
             $observer->onNext($nextValue);
         }, function ($error) use($observer) {
             $observer->onError($error);
         }, function () use(&$group, &$innerSubscription, &$isStopped, $observer) {
             $group->remove($innerSubscription);
             if ($isStopped && $group->count() === 1) {
                 $observer->onCompleted();
             }
         }), $scheduler));
     }, [$observer, 'onError'], function () use(&$group, &$isStopped, $observer) {
         $isStopped = true;
         if ($group->count() === 1) {
             $observer->onCompleted();
         }
     });
     $subscription = $this->sources->subscribe($callbackObserver, $scheduler);
     $sourceSubscription->setDisposable($subscription);
     return $group;
 }
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $hasCurrentKey = false;
     $currentKey = null;
     return $observable->subscribe(new CallbackObserver(function ($value) use($observer, &$hasCurrentKey, &$currentKey) {
         $key = $value;
         if ($this->keySelector) {
             try {
                 $key = call_user_func($this->keySelector, $value);
             } catch (\Exception $e) {
                 return $observer->onError($e);
             }
         }
         $comparerEquals = null;
         if ($hasCurrentKey) {
             try {
                 $comparerEquals = call_user_func($this->comparer, $currentKey, $key);
             } catch (\Exception $e) {
                 return $observer->onError($e);
             }
         }
         if (!$hasCurrentKey || !$comparerEquals) {
             $hasCurrentKey = true;
             $currentKey = $key;
             $observer->onNext($value);
         }
     }, function ($e) use($observer) {
         $observer->onError($e);
     }, function () use($observer) {
         $observer->onCompleted();
     }));
 }
Exemplo n.º 9
0
 /**
  * @param \Rx\ObservableInterface $observable
  * @param \Rx\ObserverInterface $observer
  * @param \Rx\SchedulerInterface $scheduler
  * @return \Rx\DisposableInterface
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $cbObserver = new CallbackObserver(function ($x) use($observer) {
         try {
             $this->onEachObserver->onNext($x);
         } catch (\Exception $e) {
             return $observer->onError($e);
         }
         $observer->onNext($x);
     }, function ($err) use($observer) {
         try {
             $this->onEachObserver->onError($err);
         } catch (\Exception $e) {
             return $observer->onError($e);
         }
         $observer->onError($err);
     }, function () use($observer) {
         try {
             $this->onEachObserver->onCompleted();
         } catch (\Exception $e) {
             return $observer->onError($e);
         }
         $observer->onCompleted();
     });
     return $observable->subscribe($cbObserver, $scheduler);
 }
Exemplo n.º 10
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     return $observable->subscribe(new CallbackObserver([$observer, 'onNext'], [$observer, 'onError'], function () use($observer) {
         $o = new AutoDetachObserver($observer);
         $o->setDisposable($this->subsequentObservable->subscribe($o));
     }));
 }
Exemplo n.º 11
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     return $observable->subscribe(new CallbackObserver(function ($data) use($observer) {
         while (strlen($data) > 0) {
             $frame = $this->frame;
             $frame->addBuffer($data);
             $data = "";
             if ($frame->isCoalesced()) {
                 $result = $this->validator->validateFrame($frame, $this->previousFrame);
                 if (0 !== $result) {
                     $observer->onError(new WebsocketErrorException($result));
                 }
                 $data = $frame->extractOverflow();
                 $frame->unMaskPayload();
                 $observer->onNext($frame);
                 if ($frame->getOpcode() < 3) {
                     $this->previousFrame = $this->frame;
                     if ($frame->isFinal()) {
                         $this->previousFrame = null;
                     }
                 }
                 $this->frame = new Frame();
             }
         }
     }, [$observer, 'onError'], [$observer, 'onCompleted']));
 }
Exemplo n.º 12
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     if ($this->scheduler !== null) {
         $scheduler = $this->scheduler;
     }
     if ($scheduler === null) {
         throw new \Exception("You must use a scheduler that support non-zero delay.");
     }
     $lastScheduledTime = 0;
     $disposable = new CompositeDisposable();
     $sourceDisposable = new EmptyDisposable();
     $sourceDisposable = $observable->subscribe(new CallbackObserver(function ($x) use($scheduler, $observer, &$lastScheduledTime, $disposable) {
         $schedDisp = $scheduler->schedule(function () use($x, $observer, &$schedDisp, $disposable) {
             $observer->onNext($x);
             $disposable->remove($schedDisp);
         }, $this->delay);
         $disposable->add($schedDisp);
     }, function ($err) use($scheduler, $observer, &$lastScheduledTime, $disposable, &$sourceDisposable) {
         $disposable->remove($sourceDisposable);
         $sourceDisposable->dispose();
         $observer->onError($err);
     }, function () use($scheduler, $observer, $disposable, &$sourceDisposable) {
         $disposable->remove($sourceDisposable);
         $sourceDisposable->dispose();
         $schedDisp = $scheduler->schedule(function () use($observer, &$schedDisp, $disposable) {
             $observer->onCompleted();
             $disposable->remove($schedDisp);
         }, $this->delay);
         $disposable->add($schedDisp);
     }), $scheduler);
     $disposable->add($sourceDisposable);
     return $disposable;
 }
Exemplo n.º 13
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $onNext = function ($innerSource) use($observer, $scheduler) {
         $innerDisposable = new SingleAssignmentDisposable();
         $id = ++$this->latest;
         $this->hasLatest = true;
         $this->innerSubscription->setDisposable($innerDisposable);
         $innerCallbackObserver = new CallbackObserver(function ($x) use($id, $observer) {
             if ($this->latest === $id) {
                 $observer->onNext($x);
             }
         }, function ($e) use($id, $observer) {
             if ($this->latest === $id) {
                 $observer->onError($e);
             }
         }, function () use($id, $observer) {
             if ($this->latest === $id) {
                 $this->hasLatest = false;
                 if ($this->isStopped) {
                     $observer->onCompleted();
                 }
             }
         });
         $innerSubscription = $innerSource->subscribe($innerCallbackObserver, $scheduler);
         $innerDisposable->setDisposable($innerSubscription);
     };
     $callbackObserver = new CallbackObserver($onNext, [$observer, 'onError'], function () use($observer) {
         $this->isStopped = true;
         if (!$this->hasLatest) {
             $observer->onCompleted();
         }
     });
     $subscription = $observable->subscribe($callbackObserver, $scheduler);
     return new BinaryDisposable($subscription, $this->innerSubscription);
 }
Exemplo n.º 14
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $cbObserver = new CallbackObserver(function ($x) {
         $this->arr[] = $x;
     }, [$observer, 'onError'], function () use($observer) {
         $observer->onNext($this->arr);
         $observer->onCompleted();
     });
     return $observable->subscribe($cbObserver, $scheduler);
 }
Exemplo n.º 15
0
 /**
  * ConnectionSubject constructor.
  * @param ObservableInterface $rawDataIn
  * @param ObserverInterface $rawDataOut
  * @param bool $mask
  * @param bool $useMessageObject
  * @param string $subProtocol
  * @param RequestInterface $request
  * @param ResponseInterface $response
  */
 public function __construct(ObservableInterface $rawDataIn, ObserverInterface $rawDataOut, $mask = false, $useMessageObject = false, $subProtocol = "", RequestInterface $request, ResponseInterface $response)
 {
     $this->request = $request;
     $this->response = $response;
     $this->rawDataIn = new AnonymousObservable(function ($observer) use($rawDataIn) {
         return $rawDataIn->subscribe($observer);
     });
     $this->rawDataOut = $rawDataOut;
     $this->mask = $mask;
     $this->subProtocol = $subProtocol;
     // This can be used instead of the subjecg when this issue is addressed:
     // https://github.com/asm89/Rx.PHP/issues/20
     // Actually - using the subject is better so that the framing doesn't get done for every
     // subscriber.
     //$frames = $this->rawDataIn
     //    ->lift(new WebsocketFrameOperator());
     $frames = new Subject();
     $this->rawDataIn->lift(function () {
         return new WebsocketFrameOperator();
     })->subscribe(new CallbackObserver([$frames, "onNext"], function ($error) use($frames) {
         $close = $this->createCloseFrame();
         if ($error instanceof WebsocketErrorException) {
             $close = $this->createCloseFrame($error->getCloseCode());
         }
         $this->sendFrame($close);
         $this->rawDataOut->onCompleted();
         // TODO: Should this error through to frame observers?
         $frames->onCompleted();
     }, function () use($frames) {
         $this->rawDataOut->onCompleted();
         $frames->onCompleted();
     }));
     $this->controlFrames = $frames->filter(function (Frame $frame) {
         return $frame->getOpcode() > 2;
     });
     // default ping handler (ping received from far end
     $this->controlFrames->filter(function (Frame $frame) {
         return $frame->getOpcode() === $frame::OP_PING;
     })->subscribe(new CallbackObserver(function (Frame $frame) {
         $pong = new Frame($frame->getPayload(), true, Frame::OP_PONG);
         $this->sendFrame($pong);
     }));
     $frames->filter(function (Frame $frame) {
         return $frame->getOpcode() < 3;
     })->lift(function () use($mask, $useMessageObject) {
         return new WebsocketMessageOperator($mask, $useMessageObject);
     })->subscribe(new CallbackObserver(function ($x) {
         parent::onNext($x);
     }, function ($x) {
         parent::onError($x);
     }, function () {
         parent::onCompleted();
     }));
     $this->subProtocol = $subProtocol;
 }
Exemplo n.º 16
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $this->q = [];
     $cbObserver = new CallbackObserver(function ($x) use($observer) {
         $this->q[] = $x;
         if (count($this->q) > $this->count) {
             $observer->onNext(array_shift($this->q));
         }
     }, [$observer, 'onError'], [$observer, 'onCompleted']);
     return $observable->subscribe($cbObserver, $scheduler);
 }
Exemplo n.º 17
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $disposable = new SerialDisposable();
     $singleDisposable = new SingleAssignmentDisposable();
     $disposable->setDisposable($singleDisposable);
     $singleDisposable->setDisposable($this->scheduler->schedule(function () use($disposable, $observer, $observable, $scheduler) {
         $subscription = $observable->subscribe($observer, $scheduler);
         $disposable->setDisposable(new ScheduledDisposable($this->scheduler, $subscription));
     }));
     return $disposable;
 }
Exemplo n.º 18
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     return $observable->subscribe(new CallbackObserver(function ($x) use($observer) {
         $observer->onNext(new OnNextNotification($x));
     }, function ($error) use($observer) {
         $observer->onNext(new OnErrorNotification($error));
         $observer->onCompleted();
     }, function () use($observer) {
         $observer->onNext(new OnCompletedNotification());
         $observer->onCompleted();
     }), $scheduler);
 }
Exemplo n.º 19
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     if ($this->scheduler !== null) {
         $scheduler = $this->scheduler;
     }
     if ($scheduler === null) {
         throw new \Exception("You must use a scheduler with timestamp.");
     }
     return $observable->subscribe(new CallbackObserver(function ($x) use($observer, $scheduler) {
         $observer->onNext(new Timestamped($scheduler->now(), $x));
     }, [$observer, "onError"], [$observer, "onCompleted"]), $scheduler);
 }
Exemplo n.º 20
0
 public function __construct($key, ObservableInterface $underlyingObservable, DisposableInterface $mergedDisposable = null)
 {
     $this->key = $key;
     if (null === $mergedDisposable) {
         $this->underlyingObservable = $underlyingObservable;
     } else {
         $this->underlyingObservable = new AnonymousObservable(function ($observer, $scheduler) use($mergedDisposable, $underlyingObservable) {
             // todo, typehint $mergedDisposable?
             return new CompositeDisposable(array($mergedDisposable->getDisposable(), $underlyingObservable->subscribe($observer, $scheduler)));
         });
     }
 }
Exemplo n.º 21
0
 /**
  * @param \Rx\ObservableInterface $observable
  * @param \Rx\ObserverInterface $observer
  * @param \Rx\SchedulerInterface $scheduler
  * @return \Rx\DisposableInterface
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $remaining = $this->count;
     $cbObserver = new CallbackObserver(function ($nextValue) use($observer, &$remaining) {
         if ($remaining <= 0) {
             $observer->onNext($nextValue);
         } else {
             $remaining--;
         }
     }, [$observer, 'onError'], [$observer, 'onCompleted']);
     return $observable->subscribe($cbObserver, $scheduler);
 }
Exemplo n.º 22
0
 /**
  * @param \Rx\ObservableInterface $observable
  * @param \Rx\ObserverInterface $observer
  * @param \Rx\SchedulerInterface $scheduler
  * @return \Rx\DisposableInterface
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $selectObserver = new CallbackObserver(function ($nextValue) use($observer) {
         $value = null;
         try {
             $value = call_user_func($this->selector, $nextValue);
         } catch (\Exception $e) {
             $observer->onError($e);
         }
         $observer->onNext($value);
     }, [$observer, 'onError'], [$observer, 'onCompleted']);
     return $observable->subscribe($selectObserver, $scheduler);
 }
Exemplo n.º 23
0
 /**
  * Converts an existing observable sequence to React Promise
  *
  * @param PromisorInterface|null $deferred
  * @return \React\Promise\Promise
  */
 public static function fromObservable(ObservableInterface $observable, Deferred $deferred = null)
 {
     $d = $deferred ?: new Deferred();
     $value = null;
     $observable->subscribe(new CallbackObserver(function ($v) use(&$value) {
         $value = $v;
     }, function ($error) use($d) {
         $d->reject($error);
     }, function () use($d, &$value) {
         $d->resolve($value);
     }));
     return $d->promise();
 }
Exemplo n.º 24
0
 /**
  * @param \Rx\ObservableInterface $observable
  * @param \Rx\ObserverInterface $observer
  * @param \Rx\SchedulerInterface $scheduler
  * @return \Rx\DisposableInterface
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $buffer = "";
     return $observable->defaultIfEmpty(Observable::just(null))->concat(Observable::just($this->delimiter))->concatMap(function ($x) use(&$buffer) {
         if ($x === null || $buffer === null) {
             $buffer = null;
             return Observable::emptyObservable();
         }
         $items = explode($this->delimiter, $buffer . $x);
         $buffer = array_pop($items);
         return Observable::fromArray($items);
     })->subscribe($observer, $scheduler);
 }
Exemplo n.º 25
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     return $observable->subscribe(new CallbackObserver(function ($frame) use($observer) {
         $this->message->addFrame($frame);
         if ($this->message->isCoalesced()) {
             if ($this->useMessageObject) {
                 $observer->onNext(new \Rx\Websocket\Message($this->message->getPayload(), $this->message->isBinary()));
             } else {
                 $observer->onNext($this->message->getPayload());
             }
             $this->message = new Message();
         }
     }, [$observer, 'onError'], [$observer, 'onCompleted']));
 }
Exemplo n.º 26
0
 /**
  * @param \Rx\ObservableInterface $observable
  * @param \Rx\ObserverInterface $observer
  * @param \Rx\SchedulerInterface $scheduler
  * @return \Rx\DisposableInterface
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $callbackObserver = new CallbackObserver(function ($nextValue) use($observer) {
         $this->items[] = $nextValue;
         if (count($this->items) > $this->count) {
             array_shift($this->items);
         }
     }, [$observer, 'onError'], function () use($observer) {
         while (count($this->items) > 0) {
             $observer->onNext(array_shift($this->items));
         }
         $observer->onCompleted();
     });
     return $observable->subscribe($callbackObserver, $scheduler);
 }
Exemplo n.º 27
0
 /**
  * @param ObservableInterface $observable
  * @param ObserverInterface $observer
  * @param SchedulerInterface|null $scheduler
  * @return \Rx\DisposableInterface
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $onNext = function ($value) use($observer) {
         try {
             if (call_user_func($this->predicate, $value)) {
                 $observer->onNext($value);
             } else {
                 $observer->onCompleted();
             }
         } catch (\Exception $e) {
             $observer->onError($e);
         }
     };
     $callbackObserver = new CallbackObserver($onNext, [$observer, 'onError'], [$observer, 'onCompleted']);
     return $observable->subscribe($callbackObserver, $scheduler);
 }
Exemplo n.º 28
0
 private function subscribeToResult(ObservableInterface $observable, SchedulerInterface $scheduler, ObserverInterface $observer, $outerIndex)
 {
     return $observable->subscribe(new CallbackObserver(function ($value) use($observer, $outerIndex) {
         if (!$this->hasFirst) {
             $this->hasFirst = true;
             foreach ($this->subscriptions as $i => $subscription) {
                 if ($i !== $outerIndex) {
                     $subscription->dispose();
                     unset($this->subscriptions[$i]);
                     $this->innerSubscription->remove($subscription);
                 }
             }
         }
         $observer->onNext($value);
     }, [$observer, 'onError'], [$observer, 'onCompleted']), $scheduler);
 }
Exemplo n.º 29
0
 /**
  * @param \Rx\ObservableInterface $observable
  * @param \Rx\ObserverInterface $observer
  * @param \Rx\SchedulerInterface $scheduler
  * @return \Rx\DisposableInterface
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $callbackObserver = new CallbackObserver(function ($value) use($observer, $observable) {
         try {
             if ($this->isSkipping) {
                 $this->isSkipping = call_user_func_array($this->predicate, [$value, $observable]);
             }
             if (!$this->isSkipping) {
                 $observer->onNext($value);
             }
         } catch (\Exception $e) {
             $observer->onError($e);
         }
     }, [$observer, 'onError'], [$observer, 'onCompleted']);
     return $observable->subscribe($callbackObserver, $scheduler);
 }
Exemplo n.º 30
0
 /**
  * @param \Rx\ObservableInterface $observable
  * @param \Rx\ObserverInterface $observer
  * @param \Rx\SchedulerInterface $scheduler
  * @return \Rx\DisposableInterface
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $disposable = new SerialDisposable();
     $cbObserver = new CallbackObserver(function ($x) use($observer) {
         $this->passThrough = true;
         $observer->onNext($x);
     }, [$observer, 'onError'], function () use($observer, $disposable, $scheduler) {
         if (!$this->passThrough) {
             $disposable->setDisposable($this->observable->subscribe($observer, $scheduler));
             return;
         }
         $observer->onCompleted();
     });
     $subscription = $observable->subscribe($cbObserver, $scheduler);
     $disposable->setDisposable($subscription);
     return $disposable;
 }