-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
loopingTimer.js
executable file
·81 lines (72 loc) · 2.03 KB
/
loopingTimer.js
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
const helpers = require('./helpers');
/**
* Implements a looping Timer which is passed a function that is expected to return
* a when supported by time.toZDT. The loop will reschedule the timer based
* on that returned when or, if it return null the looping stops.
*/
class LoopingTimer {
/**
* Constructor
*/
constructor() {
// noop
}
/**
* Kicks off the timer loop. Schedules a timer to call func at when
* @param {function} func function to call at when, must return a when to continue the loop or null to stop
* @param {*} when any of the types supported by time.toZDT
* @param {string} [name] timer name displayed in openHAB
*/
loop(func, when, name) {
this.func = func;
this.name = name;
if (!when) this.expired();
else {
this.timer = helpers.createTimer(when, () => this.expired(), name, 'loopingTimer');
}
}
/**
* Called when the timer expires. Calls the passed in function and
* reschedules it based on the returned when value, or ends if null was
* returned.
* @throws exception when the time returned by the looping function is in the past
*/
expired() {
const when = this.func();
if (when) {
const nextRun = time.toZDT(when);
if (nextRun.isAfter(time.toZDT())) {
this.timer = helpers.createTimer(nextRun.toString(), () => this.expired(),
this.name, 'loopingTimer');
}
else {
throw 'when ' + when + ' returned by the loop function is in the past!';
}
}
}
/**
* Cancels the timer if it exists and hasn't already terminated.
*/
cancel() {
if (this.timer && !this.hasTerminated()) {
this.timer.cancel();
}
}
/**
* Returns true of the timer doesn't exist or has terminated.
*/
hasTerminated() {
return !this.timer || this.timer.hasTerminated();
}
}
/**
* @returns a timer that resheduels itself until the passed in looping function
* return null
*/
function getLoopingTimer() {
return new LoopingTimer();
}
module.exports = {
LoopingTimer,
getLoopingTimer
}