本文整理汇总了PHP中Rx\Observable::defer方法的典型用法代码示例。如果您正苦于以下问题:PHP Observable::defer方法的具体用法?PHP Observable::defer怎么用?PHP Observable::defer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Rx\Observable
的用法示例。
在下文中一共展示了Observable::defer方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: toObservable
/**
* 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);
}
示例2: refCount_not_connected
/**
* @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);
}
示例3: toObservable
/**
* 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;
});
}
示例4: toObservable
/**
* 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;
});
}
示例5: defer_throw
/**
* @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);
}
示例6: findSharePrice
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;
});
}
示例7: defer_error_while_subscribe_with_immediate_scheduler_passes_through
/**
* @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);
}
示例8:
<?php
require_once __DIR__ . '/../bootstrap.php';
$source = \Rx\Observable::defer(function () {
return \Rx\Observable::just(42);
});
$subscription = $source->subscribe($stdoutObserver);
示例9: fromPromise
/**
* @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;
});
}
示例10: __call
/**
* @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;
});
}