-
Notifications
You must be signed in to change notification settings - Fork 42
/
Timer.php
150 lines (130 loc) · 3.11 KB
/
Timer.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* This file is part of Icicle, a library for writing asynchronous code in PHP using promises and coroutines.
*
* @copyright 2014-2015 Aaron Piotrowski. All rights reserved.
* @license Apache-2.0 See the LICENSE file that was distributed with this source code for more information.
*/
namespace Icicle\Loop\Events;
use Icicle\Loop\Manager\TimerManagerInterface;
class Timer implements TimerInterface
{
const MIN_INTERVAL = 0.001; // 1ms minimum interval.
/**
* @var \Icicle\Loop\Manager\TimerManagerInterface
*/
private $manager;
/**
* Callback function to be called when the timer expires.
*
* @var callable
*/
private $callback;
/**
* @var mixed[]|null
*/
private $args;
/**
* Number of seconds until the timer is called.
*
* @var float
*/
private $interval;
/**
* True if the timer is periodic, false if the timer should only be called once.
*
* @var bool
*/
private $periodic;
/**
* @param \Icicle\Loop\Manager\TimerManagerInterface $manager
* @param int|float $interval Number of seconds until the callback function is called.
* @param bool $periodic True to repeat the timer, false to only run it once.
* @param callable $callback Function called when the interval expires.
* @param mixed[] $args Optional array of arguments to pass the callback function.
*/
public function __construct(
TimerManagerInterface $manager,
$interval,
$periodic,
callable $callback,
array $args = []
) {
$this->manager = $manager;
$this->interval = (float) $interval;
$this->periodic = (bool) $periodic;
$this->callback = $callback;
$this->args = $args;
if (self::MIN_INTERVAL > $this->interval) {
$this->interval = self::MIN_INTERVAL;
}
}
/**
* {@inheritdoc}
*/
public function call()
{
if (empty($this->args)) {
$callback = $this->callback;
$callback();
} else {
call_user_func_array($this->callback, $this->args);
}
}
/**
* {@inheritdoc}
*/
public function __invoke()
{
$this->call();
}
/**
* {@inheritdoc}
*/
public function isPending()
{
return $this->manager->isPending($this);
}
/**
* {@inheritdoc}
*/
public function start()
{
$this->manager->start($this);
}
/**
* {@inheritdoc}
*/
public function stop()
{
$this->manager->stop($this);
}
/**
* {@inheritdoc}
*/
public function unreference()
{
$this->manager->unreference($this);
}
/**
* {@inheritdoc}
*/
public function reference()
{
$this->manager->reference($this);
}
/**
* {@inheritdoc}
*/
public function getInterval()
{
return $this->interval;
}
/**
* {@inheritdoc}
*/
public function isPeriodic()
{
return $this->periodic;
}
}