Lines
100.00%
7 / 7
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| WorkerTimers | 100.00% | 7 / 7 | 100.00% | 3 / 3 | 4 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| startTimer | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | |||||
| waitFor | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Schedule; | |
| 6 | ||
| 7 | use Closure; | |
| 8 | use Stewart\Contracts\Schedule\Scheduler; | |
| 9 | use Stewart\Contracts\Time\Duration; | |
| 10 | use Stewart\Contracts\Time\PendingWait; | |
| 11 | use Stewart\Contracts\Time\TimerHandle; | |
| 12 | use Stewart\Contracts\Time\Timers; | |
| 13 | ||
| 14 | final readonly class WorkerTimers implements Timers | |
| 15 | { | |
| 16 | // Waits time out on the loop timers, so a paused app's wait still ends. | |
| 17 | public function __construct( | |
| 18 | private Scheduler $scheduler, | |
| 19 | private Timers $loopTimers, | |
| 20 | ) {} | |
| 21 | ||
| 22 | public function startTimer(Duration $delay, Closure $callback): TimerHandle | |
| 23 | { | |
| 24 | $shortestDelay = Duration::milliseconds(1); | |
| 25 | $task = $this->scheduler->runAfter($shortestDelay->isLongerThan($delay) ? $shortestDelay : $delay, static function () use ($callback): void { | |
| 26 | $callback(); | |
| 27 | }); | |
| 28 | ||
| 29 | return new ScheduledTimerHandle($task); | |
| 30 | } | |
| 31 | ||
| 32 | /** | |
| 33 | * @template T | |
| 34 | * @param Closure(PendingWait<T>): (Closure(): void) $arm | |
| 35 | * @return T|null | |
| 36 | */ | |
| 37 | public function waitFor(Closure $arm, ?Duration $timeout = null): mixed | |
| 38 | { | |
| 39 | return $this->loopTimers->waitFor($arm, $timeout); | |
| 40 | } | |
| 41 | } |