2016-06-01 18:19:19 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Observable;
|
|
|
|
use Amp\Observer;
|
|
|
|
use Amp\Pause;
|
|
|
|
use Amp\Postponed;
|
|
|
|
use Amp\Loop\NativeLoop;
|
|
|
|
|
2016-08-02 19:21:21 +02:00
|
|
|
Amp\execute(function () {
|
2016-06-01 18:19:19 +02:00
|
|
|
try {
|
|
|
|
$postponed = new Postponed;
|
|
|
|
|
2016-08-02 19:21:21 +02:00
|
|
|
Amp\defer(function () use ($postponed) {
|
2016-06-01 18:19:19 +02:00
|
|
|
// Observer emits all values at once.
|
|
|
|
$postponed->emit(1);
|
|
|
|
$postponed->emit(2);
|
|
|
|
$postponed->emit(3);
|
|
|
|
$postponed->emit(4);
|
|
|
|
$postponed->emit(5);
|
|
|
|
$postponed->emit(6);
|
|
|
|
$postponed->emit(7);
|
|
|
|
$postponed->emit(8);
|
|
|
|
$postponed->emit(9);
|
|
|
|
$postponed->emit(10);
|
|
|
|
$postponed->resolve(11);
|
|
|
|
});
|
|
|
|
|
|
|
|
$observable = $postponed->getObservable();
|
|
|
|
|
|
|
|
$generator = function (Observable $observable) {
|
|
|
|
$observer = new Observer($observable);
|
|
|
|
|
2016-12-13 20:37:51 +01:00
|
|
|
while (yield $observer->advance()) {
|
2016-06-01 18:19:19 +02:00
|
|
|
printf("Observable emitted %d\n", $observer->getCurrent());
|
|
|
|
yield new Pause(100); // Observer consumption takes 100 ms.
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Observable result %d\n", $observer->getResult());
|
|
|
|
};
|
|
|
|
|
|
|
|
yield new Coroutine($generator($observable));
|
|
|
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
printf("Exception: %s\n", $exception);
|
|
|
|
}
|
2016-08-02 19:21:21 +02:00
|
|
|
}, $loop = new NativeLoop());
|