<?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 = \React\EventLoop\Factory::create(); $scheduler = new \Rx\Scheduler\EventLoopScheduler($loop); $source = Rx\Observable::race([Rx\Observable::timer(500)->map(function () { return 'foo'; }), Rx\Observable::timer(200)->map(function () { return 'bar'; })]); $source->subscribe($stdoutObserver, $scheduler); $loop->run();