2016-05-27 01:20:05 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
|
2017-03-12 19:54:52 +01:00
|
|
|
require __DIR__ . '/../../vendor/autoload.php';
|
2016-05-27 01:20:05 +02:00
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Emitter;
|
|
|
|
use Amp\Loop;
|
2017-04-23 14:43:46 +02:00
|
|
|
use Amp\Pause;
|
2016-05-27 01:20:05 +02:00
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () {
|
2016-05-27 01:20:05 +02:00
|
|
|
try {
|
2017-01-04 02:10:27 +01:00
|
|
|
$emitter = new Emitter;
|
2016-05-27 01:20:05 +02:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$stream = $emitter->stream();
|
2016-05-27 22:44:01 +02:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$stream->onEmit(function ($value) {
|
2017-01-04 02:10:27 +01:00
|
|
|
printf("Stream emitted %d\n", $value);
|
|
|
|
return new Pause(500); // Artificial back-pressure on stream.
|
2016-05-27 22:44:01 +02:00
|
|
|
});
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve(function (Throwable $exception = null, $value) {
|
2016-05-27 22:44:01 +02:00
|
|
|
if ($exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
printf("Stream failed: %s\n", $exception->getMessage());
|
2016-05-27 22:44:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
printf("Stream result %d\n", $value);
|
2016-05-27 22:44:01 +02:00
|
|
|
});
|
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$generator = function (Emitter $emitter) {
|
|
|
|
yield $emitter->emit(new Pause(500, 1));
|
|
|
|
yield $emitter->emit(new Pause(1500, 2));
|
|
|
|
yield $emitter->emit(new Pause(1000, 3));
|
|
|
|
yield $emitter->emit(new Pause(2000, 4));
|
|
|
|
yield $emitter->emit(5);
|
|
|
|
yield $emitter->emit(6);
|
|
|
|
yield $emitter->emit(7);
|
|
|
|
yield $emitter->emit(new Pause(2000, 8));
|
|
|
|
yield $emitter->emit(9);
|
|
|
|
yield $emitter->emit(10);
|
|
|
|
$emitter->resolve(11);
|
2016-05-27 01:20:05 +02:00
|
|
|
};
|
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
yield new Coroutine($generator($emitter));
|
2016-05-27 01:20:05 +02:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
printf("Exception: %s\n", $exception);
|
|
|
|
}
|
2017-03-12 19:47:08 +01:00
|
|
|
});
|