subscribe() public method

public subscribe ( rx\ObserverInterface $observer, $scheduler = null )
$observer rx\ObserverInterface
Beispiel #1
0
 /**
  * @return \Rx\Disposable\BinaryDisposable
  */
 public function connect()
 {
     if ($this->hasSubscription) {
         return $this->subscription;
     }
     $this->hasSubscription = true;
     $isDisposed = false;
     $connectableDisposable = new CallbackDisposable(function () use(&$isDisposed) {
         if ($isDisposed) {
             return;
         }
         $isDisposed = true;
         $this->hasSubscription = false;
     });
     $this->subscription = new BinaryDisposable($this->sourceObservable->subscribe($this->subject, $this->scheduler), $connectableDisposable);
     return $this->subscription;
 }
Beispiel #2
0
/**
 * Wait until observable completes.
 *
 * @param Observable|ObservableInterface $observable
 * @param LoopInterface $loop
 * @return \Generator
 */
function awaitToGenerator(Observable $observable, LoopInterface $loop = null)
{
    $completed = false;
    $results = [];
    $loop = $loop ?: \EventLoop\getLoop();
    $scheduler = new EventLoopScheduler($loop);
    $observable->subscribe(new CallbackObserver(function ($value) use(&$results, &$results, $loop) {
        $results[] = $value;
    }, function ($e) use(&$completed) {
        $completed = true;
        throw $e;
    }, function () use(&$completed) {
        $completed = true;
    }), $scheduler);
    while (!$completed) {
        $loop->tick();
        foreach ($results as $result) {
            (yield $result);
        }
        $results = [];
    }
}