/** * {@inheritdoc} */ public function done(callable $onFulfilled = null, callable $onRejected = null) { if (null !== $onRejected) { Loop\queue($onRejected, $this->exception); } else { Loop\queue(function () { throw $this->exception; // Rethrow exception in uncatchable way. }); } }
/** * {@inheritdoc} */ public function done(callable $onFulfilled = null, callable $onRejected = null) { if (null !== $onFulfilled) { try { $onFulfilled($this->value); } catch (Throwable $exception) { Loop\queue(function () use($exception) { throw $exception; // Rethrow exception in uncatchable way. }); } } }
/** * {@inheritdoc} */ public function done(callable $onFulfilled = null, callable $onRejected = null) { $exception = $this->exception; if (null !== $onRejected) { try { $onRejected($exception); return; } catch (Throwable $exception) { // Code below will rethrow exception from loop. } } Loop\queue(function () use($exception) { throw $exception; // Rethrow exception in uncatchable way. }); }
/** * Executes the emitter coroutine. */ private function start() { Loop\queue(function () { // Asynchronously start the observable. if (null === $this->emitter) { return; } /** * Emits a value from the observable. * * @coroutine * * @param mixed $value If $value is an instance of \Icicle\Awaitable\Awaitable, the fulfillment value is * used as the value to emit or the rejection reason is thrown from this coroutine. If $value is an * instance of \Generator, it is used to create a coroutine which is then used as an awaitable. * * @return \Generator * * @resolve mixed The emitted value (the resolution value of $value) * * @throws \Icicle\Observable\Exception\CompletedError If the observable has been completed. * @throws \Icicle\Observable\Exception\BusyError If the observable is still busy emitting a value. */ $emit = function ($value = null) : \Generator { return $this->queue->push($value); }; try { $generator = ($this->emitter)($emit); if (!$generator instanceof Generator) { throw new UnexpectedTypeError('Generator', $generator); } $this->coroutine = new Coroutine($generator); $this->coroutine->done(function ($value) { $this->queue->complete($value); }, function (Throwable $exception) { $this->queue->fail($exception); }); } catch (Throwable $exception) { $this->queue->fail(new InvalidEmitterError($this->emitter, $exception)); } $this->emitter = null; }); }
/** * {@inheritdoc} */ public function resume() { if ($this->isPending() && $this->paused) { $this->paused = false; if ($this->ready) { if ($this->current instanceof PromiseInterface) { $this->current->done($this->worker, $this->capture); } else { Loop\queue($this->worker, $this->current); } $this->ready = false; } } }
/** * Examines the value yielded from the generator and prepares the next step in interation. * * @param mixed $yielded */ private function next($yielded) { if (!$this->generator->valid()) { $this->resolve($this->generator->getReturn()); return; } if ($yielded instanceof Generator) { $yielded = new self($yielded); } $this->current = $yielded; if ($yielded instanceof Awaitable) { $yielded->done($this->send, $this->capture); } else { Loop\queue($this->send, $yielded); } }
/** * {@inheritdoc} */ public function done(callable $onFulfilled = null, callable $onRejected = null) { if (null !== $onFulfilled) { Loop\queue($onFulfilled, $this->value); } }
/** * {@inheritdoc} */ public function resume() { if ($this->isPending() && $this->paused) { $this->paused = false; if (null !== $this->next) { Loop\queue(...$this->next); $this->next = null; } } }
/** * Examines the value yielded from the generator and prepares the next step in interation. * * @param mixed $yielded */ private function next($yielded) { if (!$this->generator->valid()) { $result = $this->generator->getReturn(); if ($result instanceof Awaitable) { $this->reject(new AwaitableReturnedError($result)); return; } if ($result instanceof Generator) { $this->reject(new GeneratorReturnedError($result)); return; } $this->resolve($result); return; } $this->busy = true; if ($yielded instanceof Generator) { $yielded = new self($yielded); } $this->current = $yielded; if ($yielded instanceof Awaitable) { $yielded->done($this->send, $this->capture); } else { Loop\queue($this->send, $yielded); } $this->busy = false; }
/** * @inheritdoc * * @param string $key * * @return PromiseInterface * * @resolve void */ public function forget($key) { $deferred = new Deferred(); Loop\queue(function () use($deferred, $key) { unset($this->cache[$key]); $deferred->resolve(); }); return $deferred->getPromise(); }