* @copyright 2016-2020 Daniil Gentili * @license https://opensource.org/licenses/MIT MIT */ namespace danog\Loop\Test\Traits; use danog\Loop\Interfaces\ResumableLoopInterface; use Generator; use function Amp\delay; trait Signal { use Resumable; /** * Signaled payload. * */ private $payload; /** * Signaled exception. * * @var ?\Throwable */ private $exception; /** * Get signaled payload. * */ public function getPayload() { return $this->payload; } /** * Get signaled exception. * * @return \Throwable */ public function getException(): ?\Throwable { return $this->exception; } /** * Test waiting signal on interval. * * @param integer $interval Interval * * @return \Generator */ private function testGenerator(int $interval) { delay($interval); } /** * Loop implementation. * */ public function loop(): Generator { $this->inited = true; try { while (true) { $this->payload = $this->waitSignal($this instanceof ResumableLoopInterface ? $this->pause($this->interval) : $this->testGenerator($this->interval)); } } catch (\Throwable $e) { $this->exception = $e; } $this->ran = true; } }