/** * @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)); })); }
/** * @override */ public function subscribe(ObserverInterface $observer, $scheduler = null) { $subscribeAction = $this->subscribeAction; $autoDetachObserver = new AutoDetachObserver($observer); $autoDetachObserver->setDisposable($subscribeAction($autoDetachObserver, $scheduler)); return new CallbackDisposable(function () use($autoDetachObserver) { $autoDetachObserver->dispose(); }); }
/** * @test * @expectedException Exception * @expectedExceptionMessage fail */ public function it_disposes_if_observer_on_next_throws() { $disposed = false; $disposable = new CallbackDisposable(function () use(&$disposed) { $disposed = true; }); $observer = new AutoDetachObserver(new CallbackObserver(function () { throw new Exception('fail'); })); $observer->setDisposable($disposable); $observer->onNext(42); $this->assertTrue($disposed); }