926eb736e756f066d61d9b7db1b497dfa0b18a4e
9 InvalidArgumentException
, UnexpectedValueException
, function exception
21 * The return value of the generator
30 private $cancelled = false;
58 * Create a new generator consumer
59 * @param Future $future
60 * @param callable $loop
62 function __construct(Future
$future, callable
$loop) {
65 $this->future
= $future;
66 $this->context
= $future->createContext(function() {
67 $this->cancelled
= true;
69 $this->resolve
= API\Future\resolver
($future, $this->context
);
70 $this->reject
= API\Future\rejecter
($future, $this->context
);
71 $this->reduce
= API\Future\reducer
($future, $this->context
);
75 * Iterate over $gen, a \Generator yielding promises
77 * @param Generator $gen
78 * @return mixed promise
80 function __invoke(Generator
$gen) {
81 $this->cancelled
= false;
83 foreach ($gen as $promise) {
84 if ($this->cancelled
) {
87 $this->give($promise, $gen);
92 if (!$this->cancelled
) {
93 $this->result
= $gen->getReturn();
95 if (isset($this->result
)) {
96 ($this->resolve
)($this->result
);
98 ($this->reject
)("Cancelled");
101 return $this->context
->promise();
107 * @param mixed $promise
108 * @param Generator $gen
110 private function give($promise, Generator
$gen) {
111 if ($promise instanceof \Traversable
) {
112 $promise = iterator_to_array($promise);
114 if (is_array($promise)) {
115 $promise = ($this->reduce
)($promise);
118 $this->future
->handlePromise($promise, function($result) use($gen) {
119 if (($promise = $gen->send($result))) {
120 $this->give($promise, $gen);
122 }, function($error) use($gen) {
123 $gen->throw(exception($error));
126 /* FIXME: external loop */