Пример #1
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();
         });
     });
 }
Пример #2
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());
 }