Exemplo n.º 1
0
 /**
  * Converts a Promise to an Observable sequence
  *
  * @param \React\Promise\PromiseInterface $promise
  * @return \Rx\Observable\AnonymousObservable
  */
 public static function toObservable(PromiseInterface $promise)
 {
     return Observable::defer(function () use($promise) {
         $subject = new AsyncSubject();
         $promise->then(function ($value) use($subject) {
             $subject->onNext($value);
             $subject->onCompleted();
         }, [$subject, "onError"]);
         return $subject;
     });
 }
Exemplo n.º 2
0
 /**
  * Converts a Promise to an Observable sequence
  *
  * @param \React\Promise\PromiseInterface $promise
  * @return \Rx\Observable\AnonymousObservable
  */
 public static function toObservable(PromiseInterface $promise)
 {
     return Observable::defer(function () use($promise) {
         $subject = new AsyncSubject();
         $promise->then(function ($value) use($subject) {
             $subject->onNext($value);
             $subject->onCompleted();
         }, function ($error) use($subject) {
             $error = $error instanceof \Exception ? $error : new RejectedPromiseException($error);
             $subject->onError($error);
         });
         return $subject;
     });
 }
Exemplo n.º 3
0
 /**
  * Converts a Promise to an Observable sequence
  *
  * @param CancellablePromiseInterface $promise
  * @return Observable\AnonymousObservable
  */
 public static function toObservable(CancellablePromiseInterface $promise)
 {
     $subject = new AsyncSubject();
     $p = $promise->then(function ($value) use($subject) {
         $subject->onNext($value);
         $subject->onCompleted();
     }, function ($error) use($subject) {
         $error = $error instanceof \Exception ? $error : new RejectedPromiseException($error);
         $subject->onError($error);
     });
     return new AnonymousObservable(function ($observer, $scheduler = null) use($subject, $p) {
         $disp = $subject->subscribe($observer, $scheduler);
         return new CallbackDisposable(function () use($p, $disp) {
             $disp->dispose();
             $p->cancel();
         });
     });
 }
Exemplo n.º 4
0
 /**
  * Invokes the specified function asynchronously on the specified scheduler, surfacing the result through an
  * observable sequence.
  *
  * @param callable $action
  * @param SchedulerInterface $scheduler
  * @return AnonymousObservable
  *
  * @demo start/start.php
  * @operator
  * @reactivex start
  */
 public static function start(callable $action, SchedulerInterface $scheduler = null)
 {
     $scheduler = $scheduler ?: new ImmediateScheduler();
     $subject = new AsyncSubject();
     $scheduler->schedule(function () use($subject, $action) {
         $result = null;
         try {
             $result = call_user_func($action);
         } catch (\Exception $e) {
             $subject->onError($e);
             return;
         }
         $subject->onNext($result);
         $subject->onCompleted();
     });
     return $subject->asObservable();
 }
Exemplo n.º 5
0
/**
 * @param PromiseInterface $promise
 * @return Observable
 */
function fromPromise(PromiseInterface $promise)
{
    return Observable::defer(function () use($promise) {
        $subject = new AsyncSubject();
        $promise->then(function ($res) use($subject) {
            $subject->onNext($res);
            $subject->onCompleted();
        }, [$subject, 'onError']);
        return $subject;
    });
}
Exemplo n.º 6
0
 /**
  * @test
  */
 public function dispose()
 {
     $results1 = $this->scheduler->createObserver();
     $results2 = $this->scheduler->createObserver();
     $results3 = $this->scheduler->createObserver();
     $subject = null;
     $subscription1 = null;
     $subscription2 = null;
     $subscription3 = null;
     $this->scheduler->scheduleAbsolute(100, function () use(&$subject) {
         $subject = new AsyncSubject();
     });
     $this->scheduler->scheduleAbsolute(200, function () use(&$subscription1, &$subject, $results1) {
         $subscription1 = $subject->subscribe($results1);
     });
     $this->scheduler->scheduleAbsolute(300, function () use(&$subscription2, &$subject, $results2) {
         $subscription2 = $subject->subscribe($results2);
     });
     $this->scheduler->scheduleAbsolute(400, function () use(&$subscription3, &$subject, $results3) {
         $subscription3 = $subject->subscribe($results3);
     });
     $this->scheduler->scheduleAbsolute(500, function () use(&$subscription1) {
         $subscription1->dispose();
     });
     $this->scheduler->scheduleAbsolute(600, function () use(&$subject) {
         $subject->dispose();
     });
     $this->scheduler->scheduleAbsolute(700, function () use(&$subscription2) {
         $subscription2->dispose();
     });
     $this->scheduler->scheduleAbsolute(800, function () use(&$subscription3) {
         $subscription3->dispose();
     });
     $this->scheduler->scheduleAbsolute(150, function () use(&$subject) {
         $subject->onNext(1);
     });
     $this->scheduler->scheduleAbsolute(250, function () use(&$subject) {
         $subject->onNext(2);
     });
     $this->scheduler->scheduleAbsolute(350, function () use(&$subject) {
         $subject->onNext(3);
     });
     $this->scheduler->scheduleAbsolute(450, function () use(&$subject) {
         $subject->onNext(4);
     });
     $this->scheduler->scheduleAbsolute(550, function () use(&$subject) {
         $subject->onNext(5);
     });
     $this->scheduler->scheduleAbsolute(650, function () use(&$subject) {
         $this->assertException(function () use(&$subject) {
             $subject->onNext(6);
         });
     });
     $this->scheduler->scheduleAbsolute(750, function () use(&$subject) {
         $this->assertException(function () use(&$subject) {
             $subject->onCompleted();
         });
     });
     $this->scheduler->scheduleAbsolute(850, function () use(&$subject) {
         $this->assertException(function () use(&$subject) {
             $subject->onError(new \Exception());
         });
     });
     $this->scheduler->scheduleAbsolute(950, function () use(&$subject) {
         $this->assertException(function () use(&$subject) {
             $subject->subscribe(new CallbackObserver());
         });
     });
     $this->scheduler->start();
     $this->assertMessages([], $results1->getMessages());
     $this->assertMessages([], $results2->getMessages());
     $this->assertMessages([], $results3->getMessages());
 }
Exemplo n.º 7
0
 /**
  * @param $name
  * @param $arguments
  * @return AnonymousObservable
  */
 public function __call($name, array $arguments = [])
 {
     $promise = $this->client->__call($name, $arguments);
     return Observable::defer(function () use($promise) {
         $subject = new AsyncSubject();
         $promise->then(function ($data) use($subject) {
             $subject->onNext($data);
             $subject->onCompleted();
         }, [$subject, "onError"]);
         return $subject;
     });
 }