-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemit.php
53 lines (41 loc) · 1.3 KB
/
emit.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
use Amp\Delayed;
use PHPinnacle\Ensign\Dispatcher;
use PHPinnacle\Ensign\DispatcherBuilder;
require __DIR__ . '/../vendor/autoload.php';
class SimpleEvent
{
public $num;
public function __construct(int $num)
{
$this->num = $num;
}
}
class SimpleCommand
{
public $num;
public $delay;
public function __construct(int $num, int $delay = 100)
{
$this->num = $num;
$this->delay = $delay;
}
}
Amp\Loop::run(function () {
$builder = new DispatcherBuilder;
$builder
->register(SimpleCommand::class, function (SimpleCommand $cmd) {
yield new Delayed($cmd->delay); // Just do some heavy calculations
yield SimpleEvent::class => new SimpleEvent($cmd->num + $cmd->num);
yield new Delayed($cmd->delay); // Do more work
yield new SimpleEvent($cmd->num * $cmd->num);
return $cmd->num;
})
->register(SimpleEvent::class, function (SimpleEvent $event) {
echo \sprintf('Signal dispatched with value: %d at %s' . \PHP_EOL, $event->num, \microtime(true));
})
;
$dispatcher = $builder->build();
$data = yield $dispatcher->dispatch(new SimpleCommand(10));
echo \sprintf('Action resolved with value: %d at %s' . \PHP_EOL, $data, \microtime(true));
});