-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathTime.ts
216 lines (180 loc) · 5.79 KB
/
Time.ts
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* @license
* Copyright 2022-2024 Matter.js Authors
* SPDX-License-Identifier: Apache-2.0
*/
import { Boot } from "#util/Boot.js";
import { CancelablePromise } from "#util/Promises.js";
import { ImplementationError } from "../MatterError.js";
import { Diagnostic } from "../log/Diagnostic.js";
import { DiagnosticSource } from "../log/DiagnosticSource.js";
const registry = new Set<Timer>();
/**
* Timer and date/time management interface.
*
* You may replace this platform abstraction but we provide an implementation compatible with any standard JS
* environment.
*/
export class Time {
static get: () => Time;
static startup = {
systemMs: 0,
processMs: 0,
};
now() {
return new Date();
}
static readonly now = (): Date => Time.get().now();
nowMs() {
return this.now().getTime();
}
static readonly nowMs = (): number => Time.get().nowMs();
/**
* Create a timer that will call callback after durationMs has passed.
*/
getTimer(name: string, durationMs: number, callback: Timer.Callback): Timer {
return new StandardTimer(name, durationMs, callback, false);
}
static readonly getTimer = (name: string, durationMs: number, callback: Timer.Callback): Timer =>
Time.get().getTimer(name, durationMs, callback);
/**
* Create a timer that will periodically call callback at intervalMs intervals.
*/
getPeriodicTimer(name: string, intervalMs: number, callback: Timer.Callback): Timer {
return new StandardTimer(name, intervalMs, callback, true);
}
static readonly getPeriodicTimer = (name: string, intervalMs: number, callback: Timer.Callback): Timer =>
Time.get().getPeriodicTimer(name, intervalMs, callback);
/**
* Create a promise that resolves after a specific interval or when canceled, whichever comes first.
*/
sleep(name: string, durationMs: number): CancelablePromise {
let timer: Timer;
let resolver: () => void;
return new CancelablePromise(
resolve => {
resolver = resolve;
timer = Time.getTimer(name, durationMs, resolve);
timer.start();
},
() => {
timer.stop();
resolver();
},
);
}
static sleep(name: string, durationMs: number) {
return Time.get().sleep(name, durationMs);
}
static register(timer: Timer) {
timer.elapsed = Diagnostic.elapsed();
registry.add(timer);
}
static unregister(timer: Timer) {
registry.delete(timer);
}
}
const time = new Time();
Time.startup.systemMs = Time.startup.processMs = time.nowMs();
export interface Timer {
/** Name (diagnostics) */
name: string;
/** Set to true to indicate the timer should not prevent program exit */
utility: boolean;
/** System ID (diagnostics) */
systemId: unknown;
/** Interval (diagnostics) */
intervalMs: number;
/** Is the timer periodic? (diagnostics) */
isPeriodic: boolean;
/** Amount of time interval has been active (diagnostics) */
elapsed?: Diagnostic.Elapsed;
/** Is true if this timer is running. */
isRunning: boolean;
/** Starts this timer, chainable. */
start(): Timer;
/** Stops this timer, chainable. */
stop(): Timer;
}
export namespace Timer {
export type Callback = () => any;
}
export class StandardTimer implements Timer {
#timerId: unknown;
#utility = false;
isRunning = false;
get systemId() {
return Number(this.#timerId);
}
constructor(
readonly name: string,
readonly intervalMs: number,
private readonly callback: Timer.Callback,
readonly isPeriodic: boolean,
) {
if (intervalMs < 0 || intervalMs > 2147483647) {
throw new ImplementationError(
`Invalid intervalMs: ${intervalMs}. The value must be between 0 and 32-bit maximum value (2147483647)`,
);
}
}
get utility() {
return this.#utility;
}
set utility(utility: boolean) {
if (utility === this.#utility) {
return;
}
// Support node.js-style environments to control whether the timer blocks process exit
if (this.#timerId != undefined) {
const timerId = this.#timerId as { ref?: () => void; unref?: () => void };
if (utility) {
timerId.unref?.();
} else {
timerId.ref?.();
}
}
this.#utility = utility;
}
start() {
if (this.isRunning) this.stop();
Time.register(this);
this.isRunning = true;
this.#timerId = (this.isPeriodic ? setInterval : setTimeout)(() => {
if (!this.isPeriodic) {
Time.unregister(this);
this.isRunning = false;
}
this.callback();
}, this.intervalMs);
return this;
}
stop() {
(this.isPeriodic ? clearInterval : clearTimeout)(this.#timerId as ReturnType<typeof setTimeout>);
Time.unregister(this);
this.isRunning = false;
return this;
}
}
DiagnosticSource.add({
get [Diagnostic.value]() {
return Diagnostic.node("⏱", "Timers", {
children: [...registry].map(timer => [
timer.name,
Diagnostic.dict({
periodic: timer.isPeriodic,
interval: Diagnostic.interval(timer.intervalMs),
system: timer.systemId,
elapsed: timer.elapsed,
}),
]),
});
},
});
Boot.init(() => {
Time.get = () => time;
// Hook for testing frameworks
if (typeof MatterHooks !== "undefined") {
MatterHooks?.timeSetup?.(Time);
}
});