2016-05-27 01:20:05 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Pause;
|
|
|
|
use Amp\Postponed;
|
|
|
|
use Amp\Loop\NativeLoop;
|
|
|
|
use Interop\Async\Loop;
|
|
|
|
|
|
|
|
Loop::execute(Amp\coroutine(function () {
|
|
|
|
try {
|
|
|
|
$postponed = new Postponed;
|
|
|
|
|
2016-05-27 22:44:01 +02:00
|
|
|
$observable = $postponed->getObservable();
|
|
|
|
|
2016-06-02 17:35:41 +02:00
|
|
|
$subscriber = $observable->subscribe(function ($value) {
|
2016-05-27 22:44:01 +02:00
|
|
|
printf("Observable emitted %d\n", $value);
|
|
|
|
return new Pause(500); // Artificial back-pressure on observable.
|
|
|
|
});
|
|
|
|
|
2016-06-02 17:35:41 +02:00
|
|
|
$subscriber->when(function ($exception, $value) {
|
2016-05-27 22:44:01 +02:00
|
|
|
if ($exception) {
|
|
|
|
printf("Observable failed: %s\n", $exception->getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Observable result %d\n", $value);
|
|
|
|
});
|
|
|
|
|
2016-05-27 01:20:05 +02:00
|
|
|
$generator = function (Postponed $postponed) {
|
|
|
|
yield $postponed->emit(new Pause(500, 1));
|
|
|
|
yield $postponed->emit(new Pause(1500, 2));
|
|
|
|
yield $postponed->emit(new Pause(1000, 3));
|
|
|
|
yield $postponed->emit(new Pause(2000, 4));
|
|
|
|
yield $postponed->emit(5);
|
|
|
|
yield $postponed->emit(6);
|
|
|
|
yield $postponed->emit(7);
|
|
|
|
yield $postponed->emit(new Pause(2000, 8));
|
|
|
|
yield $postponed->emit(9);
|
|
|
|
yield $postponed->emit(10);
|
2016-05-29 18:35:09 +02:00
|
|
|
$postponed->resolve(11);
|
2016-05-27 01:20:05 +02:00
|
|
|
};
|
|
|
|
|
2016-05-27 22:44:01 +02:00
|
|
|
yield new Coroutine($generator($postponed));
|
2016-05-27 01:20:05 +02:00
|
|
|
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
printf("Exception: %s\n", $exception);
|
|
|
|
}
|
|
|
|
}), $loop = new NativeLoop());
|