-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
emitter.php
38 lines (32 loc) 路 982 Bytes
/
emitter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env php
<?php
require __DIR__ . '/../../vendor/autoload.php';
use Amp\Emitter;
use Amp\Loop;
use Amp\Pause;
Loop::run(function () {
try {
$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->complete();
});
$iterator = $emitter->getIterator();
while (yield $iterator->advance()) {
printf("Stream emitted %d\n", $iterator->getCurrent());
yield new Pause(100); // Listener consumption takes 100 ms.
}
} catch (\Throwable $exception) {
printf("Exception: %s\n", $exception);
}
});