/** * Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes. * * @param callable $factory * @return \Rx\Observable\AnonymousObservable */ public static function toObservable(callable $factory) { $observableFactory = function () use($factory) { return Promise::toObservable($factory()); }; return Observable::defer($observableFactory); }
/** * @test */ public function refCount_not_connected() { $disconnected = false; $count = 0; $xs = Observable::defer(function () use(&$count, &$disconnected) { $count++; return new AnonymousObservable(function () use(&$disconnected) { return new CallbackDisposable(function () use(&$disconnected) { $disconnected = true; }); }); }); $subject = new TestSubject(); $conn = new ConnectableObservable($xs, $subject); $refd = $conn->refCount(); $dis1 = $refd->subscribe(new CallbackObserver()); $this->assertEquals(1, $count); $this->assertEquals(1, $subject->getSubscribeCount()); $this->assertFalse($disconnected); $dis2 = $refd->subscribe(new CallbackObserver()); $this->assertEquals(1, $count); $this->assertEquals(2, $subject->getSubscribeCount()); $this->assertFalse($disconnected); $dis1->dispose(); $this->assertFalse($disconnected); $dis2->dispose(); $this->assertTrue($disconnected); $disconnected = false; $dis3 = $refd->subscribe(new CallbackObserver()); $this->assertEquals(2, $count); $this->assertEquals(3, $subject->getSubscribeCount()); $this->assertFalse($disconnected); $dis3->dispose(); $this->assertTrue($disconnected); }
/** * 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; }); }
/** * 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; }); }
/** * @test */ public function defer_throw() { $invoked = 0; $results = $this->scheduler->startWithCreate(function () use(&$invoked) { return Observable::defer(function () use(&$invoked) { $invoked++; throw new \Exception('error'); }); }); // Note: these tests differ from the RxJS tests that they were based on because RxJS was // explicitly using the immediate scheduler on subscribe internally. When we pass the // proper scheduler in, the subscription gets scheduled which requires an extra tick. $this->assertMessages([onError(201, new \Exception('error'))], $results->getMessages()); $this->assertEquals(1, $invoked); }
function findSharePrice($symbol) { return Observable::just($symbol)->flatMap(function ($symbol) { return Observable::defer(function () use($symbol) { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, "http://finance.yahoo.com/webservice/v1/symbols/{$symbol}/usd/quote?format=json"); $result = curl_exec($ch); curl_close($ch); return Observable::just($result); }); })->map('json_decode')->map(function ($result) { return $result->list->resources[0]->resource->fields->price; }); }
/** * @test */ public function defer_error_while_subscribe_with_immediate_scheduler_passes_through() { $onErrorCalled = false; Observable::defer(function () { return Observable::create(function ($observer, $scheduler = null) { $observer->onError(new \Exception('I take exception')); }); })->subscribeCallback(null, function (\Exception $e) use(&$onErrorCalled) { $onErrorCalled = true; $this->assertEquals('I take exception', $e->getMessage()); }, null, new ImmediateScheduler()); $this->assertTrue($onErrorCalled); }
<?php require_once __DIR__ . '/../bootstrap.php'; $source = \Rx\Observable::defer(function () { return \Rx\Observable::just(42); }); $subscription = $source->subscribe($stdoutObserver);
/** * @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; }); }
/** * @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; }); }