defer() публичный статический Метод

Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes.
public static defer ( callable $factory ) : Rx\Observable\AnonymousObservable
$factory callable
Результат Rx\Observable\AnonymousObservable
Пример #1
0
 /**
  * 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
0
 /**
  * @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
0
 /**
  * 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
0
 /**
  * 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
0
 /**
  * @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
0
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
0
 /**
  * @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
0
<?php

require_once __DIR__ . '/../bootstrap.php';
$source = \Rx\Observable::defer(function () {
    return \Rx\Observable::just(42);
});
$subscription = $source->subscribe($stdoutObserver);
Пример #9
0
/**
 * @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
0
 /**
  * @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;
     });
 }