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;
|
2017-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Emitter;
|
|
|
|
use Amp\Loop;
|
2017-04-26 19:20:30 +02:00
|
|
|
use Amp\Promise;
|
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;
|
2017-05-01 07:29:23 +02:00
|
|
|
$iterator = $emitter->iterate();
|
2016-05-27 22:44:01 +02:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$generator = function (Emitter $emitter) {
|
2017-04-28 14:42:02 +02:00
|
|
|
yield $emitter->emit(new Delayed(500, 1));
|
|
|
|
yield $emitter->emit(new Delayed(1500, 2));
|
|
|
|
yield $emitter->emit(new Delayed(1000, 3));
|
|
|
|
yield $emitter->emit(new Delayed(2000, 4));
|
2017-01-04 02:10:27 +01:00
|
|
|
yield $emitter->emit(5);
|
|
|
|
yield $emitter->emit(6);
|
|
|
|
yield $emitter->emit(7);
|
2017-04-28 14:42:02 +02:00
|
|
|
yield $emitter->emit(new Delayed(2000, 8));
|
2017-01-04 02:10:27 +01:00
|
|
|
yield $emitter->emit(9);
|
|
|
|
yield $emitter->emit(10);
|
2017-04-27 17:51:06 +02:00
|
|
|
$emitter->complete();
|
2016-05-27 01:20:05 +02:00
|
|
|
};
|
|
|
|
|
2017-04-26 19:20:30 +02:00
|
|
|
Promise\rethrow(new Coroutine($generator($emitter)));
|
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
while (yield $iterator->advance()) {
|
2017-04-27 18:18:25 +02:00
|
|
|
printf("Emitter emitted %d\n", $iterator->getCurrent());
|
2017-04-26 19:20:30 +02:00
|
|
|
yield new Pause(500); // Listener consumption takes 500 ms.
|
|
|
|
}
|
2016-05-27 01:20:05 +02:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
printf("Exception: %s\n", $exception);
|
|
|
|
}
|
2017-04-24 15:39:08 +02:00
|
|
|
});
|