-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutor.php
More file actions
78 lines (62 loc) · 1.99 KB
/
Executor.php
File metadata and controls
78 lines (62 loc) · 1.99 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php declare(strict_types=1);
namespace DynamicComponents;
use Webmozart\Assert\Assert;
use function func_num_args;
class Executor extends \UI\Executor
{
/** @var int */
private $microseconds;
/** @var int */
private $seconds;
/** @var callable|null */
private $onExecute;
/**
* Just like \UI\Executor, if you only give 1 param, it will be treated as microseconds.
*/
public function __construct(int $seconds = 0, ?int $microseconds = null, ?callable $onExecute = null)
{
$numArgs = func_num_args();
if ($numArgs === 0) {
$microseconds = 0;
} elseif ($numArgs === 1) {
$microseconds = $seconds;
$seconds = 0;
}
Assert::notNull($microseconds, 'Cannot set $microseconds to null. Set to 0 for no microseconds.');
parent::__construct($seconds, $microseconds);
$this->microseconds = $microseconds;
$this->seconds = $seconds;
$this->onExecute = $onExecute;
}
/**
* @return int[] Tuple of [$seconds, $microseconds]
*/
public function getInterval(): array
{
return [$this->seconds, $this->microseconds];
}
/**
* Just like \UI\Executor, if you only give 1 param, it will be treated as microseconds.
*/
public function setInterval(int $seconds, ?int $microseconds = null): void
{
if (func_num_args() === 1) {
$microseconds = $seconds;
$seconds = 0;
}
Assert::notNull($microseconds, 'Cannot set $microseconds to null. Set to 0 for no microseconds.');
parent::setInterval($seconds, $microseconds);
$this->seconds = $seconds;
$this->microseconds = $microseconds;
}
public function setOnExecute(callable $onExecute): void
{
$this->onExecute = $onExecute;
}
protected function onExecute(): void
{
if ($onExecute = $this->onExecute) {
$onExecute($this);
}
}
}