/** * Invoked when tick() is called for the first time. * * @param StrandInterface $strand The strand that is executing the coroutine. */ public function call(StrandInterface $strand) { $strand->suspend(); $this->timer = $strand->kernel()->eventLoop()->addTimer($this->timeout, function () use($strand) { $strand->resumeWithValue(null); }); }
/** * Start the coroutine. * * @param StrandInterface $strand The strand that is executing the coroutine. */ public function call(StrandInterface $strand) { $this->strand = $strand; $this->strand->suspend(); foreach ($this->coroutines as $index => $coroutine) { $this->values[$index] = null; $this->substrands[$index] = $substrand = $strand->kernel()->execute($coroutine); $substrand->on('success', function ($strand, $value) use($index) { $this->values[$index] = $value; }); $substrand->on('error', function ($strand, $exception, $preventDefault) { $this->exception = $exception; $preventDefault(); }); $substrand->on('terminate', function () { $this->exception = new StrandTerminatedException(); }); $substrand->on('exit', function () use($index) { unset($this->substrands[$index]); if (!$this->strand) { return; } elseif ($this->exception) { $this->strand->resumeWithException($this->exception); $this->strand = null; } elseif (!$this->substrands) { $this->strand->resumeWithValue($this->values); $this->strand = null; } }); } }
/** * Invoked when tick() is called for the first time. * * @param StrandInterface $strand The strand that is executing the coroutine. */ public function call(StrandInterface $strand) { $this->timer = $strand->kernel()->eventLoop()->addTimer($this->timeout, function () use($strand) { $this->timer = null; $strand->terminate(); }); $strand->call($this->coroutine); }
/** * Start the coroutine. * * @param StrandInterface $strand The strand that is executing the coroutine. */ public function call(StrandInterface $strand) { $method = [$strand->kernel()->api(), $this->name]; if (!is_callable($method)) { $strand->throwException(new BadMethodCallException('The kernel API does not have an operation named "' . $this->name . '".')); return; } $strand->pop(); $arguments = $this->arguments; array_unshift($arguments, $strand); // If the kernel API implementation returns a non-null value it is // treated as a coroutine to be executed. This allows implementation of // kernel API operations to be implemented as generators. $coroutine = call_user_func_array($method, $arguments); if (null !== $coroutine) { $strand->call($coroutine); } }
/** * Stop the coroutine kernel / event-loop. * * The React event-loop can optionally be stopped when all strands have been * terminated. * * @param StrandInterface $strand The currently executing strand. * @param boolean $stopEventLoop Indicates whether or not the React event-loop should also be stopped. */ public function stop(StrandInterface $strand, $stopEventLoop = true) { $strand->terminate(); $strand->kernel()->stop($stopEventLoop); }