/
periodictimer.d
62 lines (51 loc) · 1.49 KB
/
periodictimer.d
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
/* Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
*
* This file is part of nucular.
*
* nucular is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* nucular is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with nucular. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
module nucular.periodictimer;
import std.datetime;
import nucular.reactor;
class PeriodicTimer {
this (Duration every, void function () block) {
_every = every;
_block = block;
_started_at = Clock.currTime();
}
~this () {
cancel();
}
void execute () {
_block();
_last_execution_at = Clock.currTime();
}
void cancel () {
cancelTimer(this);
}
@property Duration every () {
return _every;
}
@property SysTime lastExecutionAt () {
return _last_execution_at;
}
@property SysTime startedAt () {
return _started_at;
}
private:
Duration _every;
SysTime _started_at;
SysTime _last_execution_at;
void function () _block;
}