/** * @test */ public function connectable_observable_creation() { $y = 0; $s2 = new Subject(); $co2 = new ConnectableObservable(Observable::just(1), $s2); $co2->subscribe(new CallbackObserver(function ($x) use(&$y) { $y = $x; })); $this->assertNotEquals(1, $y); $co2->connect(); $this->assertEquals(1, $y); }
/** * @param \Rx\ObserverInterface $observer * @param null $scheduler * @return \Rx\Disposable\CallbackDisposable */ public function subscribe(ObserverInterface $observer, $scheduler = null) { $this->count++; $shouldConnect = $this->count === 1; $subscription = $this->source->subscribe($observer, $scheduler); if ($shouldConnect) { $this->connectableSubscription = $this->source->connect(); } $isDisposed = false; return new CallbackDisposable(function () use($subscription, &$isDisposed) { if ($isDisposed) { return; } $isDisposed = true; $subscription->dispose(); $this->count--; if ($this->count === 0) { $this->connectableSubscription->dispose(); } }); }