2016-05-24 18:47:14 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
|
2017-03-12 19:54:52 +01:00
|
|
|
require __DIR__ . '/../../vendor/autoload.php';
|
2016-05-24 18:47:14 +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;
|
2017-04-23 14:43:46 +02:00
|
|
|
use Amp\Loop;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Stream;
|
2017-04-23 14:43:46 +02:00
|
|
|
use Amp\StreamIterator;
|
2017-03-10 21:31:57 +01:00
|
|
|
|
|
|
|
Loop::run(function () {
|
2016-05-24 18:47:14 +02:00
|
|
|
try {
|
2017-01-04 02:10:27 +01:00
|
|
|
$emitter = new Emitter;
|
|
|
|
|
|
|
|
Loop::defer(function () use ($emitter) {
|
|
|
|
// Listener emits all values at once.
|
|
|
|
$emitter->emit(1);
|
|
|
|
$emitter->emit(2);
|
|
|
|
$emitter->emit(3);
|
|
|
|
$emitter->emit(4);
|
|
|
|
$emitter->emit(5);
|
|
|
|
$emitter->emit(6);
|
|
|
|
$emitter->emit(7);
|
|
|
|
$emitter->emit(8);
|
|
|
|
$emitter->emit(9);
|
|
|
|
$emitter->emit(10);
|
|
|
|
$emitter->resolve(11);
|
2016-05-27 22:44:01 +02:00
|
|
|
});
|
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$stream = $emitter->stream();
|
2016-05-27 22:44:01 +02:00
|
|
|
|
2017-04-26 19:20:30 +02:00
|
|
|
while (yield $stream->advance()) {
|
|
|
|
printf("Stream emitted %d\n", $stream->getCurrent());
|
|
|
|
yield new Delayed(100); // Listener consumption takes 100 ms.
|
|
|
|
}
|
2017-03-10 21:31:57 +01:00
|
|
|
} catch (\Throwable $exception) {
|
2016-05-24 18:47:14 +02:00
|
|
|
printf("Exception: %s\n", $exception);
|
|
|
|
}
|
2017-04-24 15:39:08 +02:00
|
|
|
});
|