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

71 lines
1.4 KiB
PHP
Raw Normal View History

2022-12-24 14:36:39 +01:00
<?php declare(strict_types=1);
2020-07-21 21:45:22 +02:00
/**
* 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 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.
*/
public function getException(): ?\Throwable
{
return $this->exception;
}
2020-07-23 13:26:16 +02:00
/**
* Test waiting signal on interval.
*
* @param integer $interval Interval
*/
2022-12-24 15:03:00 +01:00
private function testGenerator(int $interval): void
2020-07-23 13:26:16 +02:00
{
2022-12-23 20:45:00 +01:00
delay($interval);
2020-07-23 13:26:16 +02:00
}
2020-07-21 21:45:22 +02:00
/**
* Loop implementation.
*
*/
2022-12-24 15:03:00 +01:00
public function loop(): void
2020-07-21 21:45:22 +02:00
{
$this->inited = true;
try {
while (true) {
2022-12-23 20:45:00 +01:00
$this->payload = $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;
}
}