-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathTime.ts
More file actions
174 lines (147 loc) · 4.74 KB
/
Time.ts
File metadata and controls
174 lines (147 loc) · 4.74 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
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
/**
* @license
* Copyright 2022-2026 Matter.js Authors
* SPDX-License-Identifier: Apache-2.0
*/
import { DiagnosticPresentation } from "#log/DiagnosticPresentation.js";
import { NoProviderError } from "#MatterError.js";
import { CancelablePromise } from "#util/Cancelable.js";
import type { Lifetime } from "#util/Lifetime.js";
import { Diagnostic } from "../log/Diagnostic.js";
import { DiagnosticSource } from "../log/DiagnosticSource.js";
import { Duration } from "./Duration.js";
import type { Timestamp } from "./Timestamp.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 default: Time;
static startup = {
systemMs: 0 as Timestamp,
processMs: 0 as Timestamp,
};
get now() {
return new Date();
}
static get now() {
return Time.default.now;
}
get nowMs() {
return Date.now();
}
static get nowMs() {
return Time.default.nowMs as Timestamp;
}
get nowUs() {
return Math.floor(performance.now() + performance.timeOrigin) as Timestamp;
}
static get nowUs() {
return Time.default.nowUs;
}
/**
* Create a timer that will call callback after durationMs has passed.
*/
getTimer(_name: string, _duration: Duration, _callback: Timer.Callback): Timer {
throw new NoProviderError();
}
static readonly getTimer = (name: string, duration: Duration, callback: Timer.Callback): Timer =>
Time.default.getTimer(name, duration, callback);
/**
* Create a timer that will periodically call callback at intervalMs intervals.
*/
getPeriodicTimer(_name: string, _duration: Duration, _callback: Timer.Callback): Timer {
throw new NoProviderError();
}
static readonly getPeriodicTimer = (name: string, duration: Duration, callback: Timer.Callback): Timer =>
Time.default.getPeriodicTimer(name, duration, callback);
/**
* Defer to a new macrotask.
*/
get macrotask(): Promise<void> {
throw new NoProviderError();
}
static get macrotask() {
return Time.default.macrotask;
}
/**
* Create a promise that resolves after a specific interval or when canceled, whichever comes first.
*/
sleep(name: string, duration: Duration): CancelablePromise {
let timer: Timer;
let resolver: () => void;
return new CancelablePromise(
resolve => {
resolver = resolve;
timer = Time.getTimer(name, duration, () => resolve());
timer.start();
},
() => {
timer.stop();
resolver();
},
);
}
static sleep(name: string, duration: Duration) {
return Time.default.sleep(name, duration);
}
static register(timer: Timer) {
registry.add(timer);
timer.elapsed = Diagnostic.elapsed();
}
static unregister(timer: Timer) {
registry.delete(timer);
}
static get timers() {
return registry;
}
}
// Check if performance API is available and has the required methods. Use lower accuracy fallback if not.
if (!performance || typeof performance.now !== "function" || typeof performance.timeOrigin !== "number") {
Object.defineProperty(Time.prototype, "nowUs", {
get() {
return Time.nowMs; // Fallback is a bit less accurate
},
});
}
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) */
interval: Duration;
/** 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 = (lifetime: Lifetime) => any;
}
DiagnosticSource.add({
get [DiagnosticPresentation.value]() {
return Diagnostic.node("⏱", "Timers", {
children: [...registry].map(timer => [
timer.name,
Diagnostic.dict({
periodic: timer.isPeriodic,
up: timer.elapsed,
interval: Duration.format(timer.interval),
"system#": timer.systemId,
}),
]),
});
},
});