-
-
Notifications
You must be signed in to change notification settings - Fork 251
/
Emanator.php
38 lines (30 loc) 路 1.02 KB
/
Emanator.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
<?php
namespace Amp;
use Interop\Async\Loop;
final class Emanator implements Emitter {
use CallableMaker, Internal\Producer;
/**
* @param callable(callable(mixed $value): Promise $emit): \Generator $emitter
*
* @throws \TypeError Thrown if the callable does not return a Generator.
*/
public function __construct(callable $emitter) {
$result = $emitter($this->callableFromInstanceMethod("emit"));
if (!$result instanceof \Generator) {
throw new \TypeError("The callable did not return a Generator");
}
Loop::defer(function () use ($result) {
$coroutine = new Coroutine($result);
$coroutine->when(function ($exception, $value) {
if ($this->resolved) {
return;
}
if ($exception) {
$this->fail($exception);
return;
}
$this->resolve($value);
});
});
}
}