Repository navigation
Expand file tree
/
Copy pathTimer.ts
More file actions
129 lines (114 loc) · 3.63 KB
/
Copy pathTimer.ts
File metadata and controls
129 lines (114 loc) · 3.63 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
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
/*
Copyright 2018, 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
A countdown timer, exposing a promise api.
A timer starts in a non-started state,
and needs to be started by calling `start()`` on it first.
Timers can be `abort()`-ed which makes the promise reject prematurely.
Once a timer is finished or aborted, it can't be started again
(because the promise should not be replaced). Instead, create
a new one through `clone()` or `cloneIfRun()`.
*/
export default class Timer {
private timerHandle: number;
private startTs: number;
private promise: Promise<void>;
private resolve: () => void;
private reject: (Error) => void;
constructor(private timeout: number) {
this.setNotStarted();
}
private setNotStarted() {
this.timerHandle = null;
this.startTs = null;
this.promise = new Promise<void>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
}).finally(() => {
this.timerHandle = null;
});
}
private onTimeout = () => {
const now = Date.now();
const elapsed = now - this.startTs;
if (elapsed >= this.timeout) {
this.resolve();
this.setNotStarted();
} else {
const delta = this.timeout - elapsed;
this.timerHandle = setTimeout(this.onTimeout, delta);
}
};
changeTimeout(timeout: number) {
if (timeout === this.timeout) {
return;
}
const isSmallerTimeout = timeout < this.timeout;
this.timeout = timeout;
if (this.isRunning() && isSmallerTimeout) {
clearTimeout(this.timerHandle);
this.onTimeout();
}
}
/**
* if not started before, starts the timer.
* @returns {Timer} the same timer
*/
start() {
if (!this.isRunning()) {
this.startTs = Date.now();
this.timerHandle = setTimeout(this.onTimeout, this.timeout);
}
return this;
}
/**
* (re)start the timer. If it's running, reset the timeout. If not, start it.
* @returns {Timer} the same timer
*/
restart() {
if (this.isRunning()) {
// don't clearTimeout here as this method
// can be called in fast succession,
// instead just take note and compare
// when the already running timeout expires
this.startTs = Date.now();
return this;
} else {
return this.start();
}
}
/**
* if the timer is running, abort it,
* and reject the promise for this timer.
* @returns {Timer} the same timer
*/
abort() {
if (this.isRunning()) {
clearTimeout(this.timerHandle);
this.reject(new Error("Timer was aborted."));
this.setNotStarted();
}
return this;
}
/**
*promise that will resolve when the timer elapses,
*or is rejected when abort is called
*@return {Promise}
*/
finished() {
return this.promise;
}
isRunning() {
return this.timerHandle !== null;
}
}