-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPeriodicLoop.php
49 lines (47 loc) · 1.33 KB
/
PeriodicLoop.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php declare(strict_types=1);
/**
* Periodic loop.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/MIT MIT
*/
namespace danog\Loop;
/**
* Periodic loop.
*
* @api
*
* @author Daniil Gentili <daniil@daniil.it>
*/
class PeriodicLoop extends GenericLoop
{
/**
* Constructor.
*
* Runs a callback at a periodic interval.
*
* The callable will be passed the instance of the current loop.
*
* The loop can be stopped from the outside by calling stop()
* and from the inside by returning `true`.
*
* @param callable(static):bool $callback Callable to run
* @param string $name Loop name
* @param ?float $interval Loop interval; if null, pauses indefinitely or until `resume()` is called.
*/
public function __construct(callable $callback, string $name, ?float $interval)
{
/** @psalm-suppress InvalidArgument */
parent::__construct(
/** @param static $loop */
static function (self $loop) use ($callback, $interval): ?float {
if ($callback($loop) === true) {
return GenericLoop::STOP;
}
return $interval;
},
$name
);
}
}