blob: 5d09e3acd0ed58a51e9d555e180316186c7b9144 (
plain)
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
|
class Timing {
constructor(workSlot = 4, longTime = 20000, pauseTime = 20) {
this.workSlot = workSlot;
this.longTime = longTime;
this.pauseTime = pauseTime;
this.interrupted = false;
this.fatalTimeout = false;
this.reset();
}
static sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async pause() {
if (this.interrupted) throw new TimingException("Interrupted");
let now = Date.now();
this.elapsed = now - this.timeOrigin;
if (now - this.lastPause > this.workSlot) {
this.tooLong = this.elapsed >= this.longTime;
if (this.tooLong && this.fatalTimeout) {
throw new TimingException(`Exceeded ${this.longTime}ms timeout`);
}
await Timing.sleep(this.pauseTime);
this.lastPause = Date.now();
return true;
}
return false;
}
reset() {
this.elapsed = 0;
this.timeOrigin = this.lastPause = Date.now();
this.tooLong = false;
}
}
class TimingException extends Error {};
|