/** * @test */ public function await_default_timeout() { $start = microtime(true); $observable = Observable::never()->takeUntil(Observable::timer(2000)); $generator = \Rx\await($observable); foreach ($generator as $item) { } $totalTime = microtime(true) - $start; $this->assertEquals('2', round($totalTime)); }
/** * @test */ public function concatAll_timer_missing_item() { $xs = $this->createHotObservable([onNext(201, 0), onNext(206, 1), onNext(211, 2), onCompleted(212)]); $results = $this->scheduler->startWithCreate(function () use($xs) { return $xs->map(function ($x) { return Observable::timer(5)->mapTo($x); })->concatAll(); }); $this->assertMessages([onNext(206, 0), onNext(211, 1), onNext(216, 2), onCompleted(216)], $results->getMessages()); }
public function __invoke(Observable $attempts) { return Observable::range(1, $this->max)->zip([$attempts], function ($i, $e) { if ($i >= $this->max) { throw $e; } return $i; })->flatMap(function ($i) { return Observable::timer(1000); }); }
/** * @test */ public function timer_one_shot_relative_time_throws() { $scheduler1 = new TestScheduler(); $xs = Observable::timer(1, $scheduler1); $xs->subscribeCallback(function () { throw new \Exception(); }); $this->assertException(function () use($scheduler1) { $scheduler1->start(); }); $scheduler2 = new TestScheduler(); $ys = Observable::timer(1, $scheduler2); $ys->subscribeCallback(null, null, function () { throw new \Exception(); }); $this->assertException(function () use($scheduler2) { $scheduler2->start(); }); }
<?php require_once __DIR__ . '/../bootstrap.php'; $loop = \React\EventLoop\Factory::create(); $scheduler = new \Rx\Scheduler\EventLoopScheduler($loop); $observable = Rx\Observable::interval(1000)->skipUntil(\Rx\Observable::timer(5000))->take(3); $observable->subscribe($stdoutObserver, $scheduler); $loop->run();
<?php use Rx\Observer\CallbackObserver; require_once __DIR__ . '/../bootstrap.php'; $loop = \React\EventLoop\Factory::create(); $scheduler = new \Rx\Scheduler\EventLoopScheduler($loop); $codes = [['id' => 38], ['id' => 38], ['id' => 40], ['id' => 40], ['id' => 37], ['id' => 39], ['id' => 37], ['id' => 39], ['id' => 66], ['id' => 65]]; $source = Rx\Observable::fromArray($codes)->concatMap(function ($x) { return \Rx\Observable::timer(100)->mapTo($x); })->groupByUntil(function ($x) { return $x['id']; }, function ($x) { return $x['id']; }, function ($x) { return Rx\Observable::timer(200); }); $subscription = $source->subscribe(new CallbackObserver(function (\Rx\Observable $obs) { // Print the count $obs->count()->subscribe(new CallbackObserver(function ($x) { echo 'Count: ', $x, PHP_EOL; })); }, function (Exception $err) { echo 'Error', $err->getMessage(), PHP_EOL; }, function () { echo 'Completed', PHP_EOL; }), $scheduler); $loop->run();
<?php require_once __DIR__ . '/../bootstrap.php'; $loop = new \React\EventLoop\StreamSelectLoop(); $scheduler = new \Rx\Scheduler\EventLoopScheduler($loop); $source = \Rx\Observable::timer(200, $scheduler); $source->subscribe($createStdoutObserver()); $loop->run(); //Next value: 0 //Complete!
<?php require __DIR__ . '/../vendor/autoload.php'; $source = \Rx\Observable::interval(1000)->takeUntil(\Rx\Observable::timer(10000)); //timeout after 10 seconds $generator = \Rx\await($source); foreach ($generator as $item) { echo $item, PHP_EOL; } echo "DONE";
/** * @test */ public function combineLatest_delay() { $loop = Factory::create(); $scheduler = new EventLoopScheduler($loop); $source1 = Observable::timer(100); $source2 = Observable::timer(120); $source3 = Observable::timer(140); $source = $source1->combineLatest([$source2, $source3]); $result = null; $completed = false; $source->subscribe(new CallbackObserver(function ($x) use(&$result) { $result = $x; }, null, function () use(&$completed) { $completed = true; }), $scheduler); $loop->run(); $this->assertEquals([0, 0, 0], $result); $this->assertTrue($completed); }
<?php require_once __DIR__ . '/../bootstrap.php'; $loop = \React\EventLoop\Factory::create(); $scheduler = new \Rx\Scheduler\EventLoopScheduler($loop); $source = \Rx\Observable::interval(105, $scheduler)->takeUntil(\Rx\Observable::timer(1000)); $subscription = $source->subscribe($stdoutObserver, $scheduler); $loop->run();
<?php require_once __DIR__ . '/../bootstrap.php'; $loop = \React\EventLoop\Factory::create(); $scheduler = new \Rx\Scheduler\EventLoopScheduler($loop); $interval = Rx\Observable::interval(1000); $source = $interval->take(4)->doOnNext(function ($x) { echo 'Side effect', PHP_EOL; }); $published = $source->shareReplay(3); $published->subscribe($createStdoutObserver('SourceA '), $scheduler); $published->subscribe($createStdoutObserver('SourceB '), $scheduler); Rx\Observable::just(true)->concatMapTo(\Rx\Observable::timer(6000))->flatMap(function () use($published) { return $published; })->subscribe($createStdoutObserver('SourceC '), $scheduler); $loop->run();