1
0
mirror of https://github.com/danog/loop.git synced 2024-12-12 01:09:40 +01:00
loop/test/Traits/Signal.php

79 lines
1.6 KiB
PHP
Raw Normal View History

2020-07-21 21:45:22 +02:00
<?php
/**
* Resumable test trait.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @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.
*
* @var mixed
*/
private $payload;
/**
* Signaled exception.
*
* @var ?\Throwable
*/
private $exception;
/**
* Get signaled payload.
*
* @return mixed
*/
public function getPayload()
{
return $this->payload;
}
/**
* Get signaled exception.
*
* @return \Throwable
*/
public function getException(): ?\Throwable
{
return $this->exception;
}
2020-07-23 13:26:16 +02:00
/**
* Test waiting signal on interval.
*
* @param integer $interval Interval
*
* @return \Generator
*/
private function testGenerator(int $interval): \Generator
{
yield delay($interval);
}
2020-07-21 21:45:22 +02:00
/**
* Loop implementation.
*
* @return Generator
*/
public function loop(): Generator
{
$this->inited = true;
try {
while (true) {
2020-07-23 13:26:16 +02:00
$this->payload = yield $this->waitSignal($this instanceof ResumableLoopInterface ? $this->pause($this->interval) : $this->testGenerator($this->interval));
2020-07-21 21:45:22 +02:00
}
} catch (\Throwable $e) {
$this->exception = $e;
}
$this->ran = true;
}
}