-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathtimer.ts
305 lines (274 loc) · 8.22 KB
/
timer.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { assert } from "./assert";
import { Deferred } from "./promises";
/**
* @deprecated Moved to the `@fluidframework/core-utils` package.
* @internal
*/
export interface ITimer {
/**
* True if timer is currently running
*/
readonly hasTimer: boolean;
/**
* Starts the timer
*/
start(): void;
/**
* Cancels the timer if already running
*/
clear(): void;
}
/**
* @deprecated Moved to the `@fluidframework/core-utils` package.
*/
interface ITimeout {
/**
* Tick that timeout was started.
*/
startTick: number;
/**
* Timeout duration in ms.
*/
duration: number;
/**
* Handler to execute when timeout ends.
*/
handler: () => void;
}
/**
* @deprecated Moved to the `@fluidframework/core-utils` package.
*/
interface IRunningTimerState extends ITimeout {
/**
* JavaScript Timeout object.
*/
timeout: ReturnType<typeof setTimeout>;
/**
* Intended duration in ms.
*/
intendedDuration: number;
/**
* Intended restart timeout.
*/
restart?: ITimeout;
}
const maxSetTimeoutMs = 0x7fffffff; // setTimeout limit is MAX_INT32=(2^31-1).
/**
* Sets timeouts like the setTimeout function allowing timeouts to exceed the setTimeout's max timeout limit.
* Timeouts may not be exactly accurate due to browser implementations and the OS.
* https://stackoverflow.com/questions/21097421/what-is-the-reason-javascript-settimeout-is-so-inaccurate
* @param timeoutFn - Executed when the timeout expires
* @param timeoutMs - Duration of the timeout in milliseconds
* @param setTimeoutIdFn - Executed to update the timeout if multiple timeouts are required when
* timeoutMs greater than maxTimeout
* @returns The initial timeout
*
* @deprecated Moved to the `@fluidframework/core-utils` package.
* @internal
*/
export function setLongTimeout(
timeoutFn: () => void,
timeoutMs: number,
setTimeoutIdFn?: (timeoutId: ReturnType<typeof setTimeout>) => void,
): ReturnType<typeof setTimeout> {
// The setTimeout max is 24.8 days before looping occurs.
let timeoutId: ReturnType<typeof setTimeout>;
if (timeoutMs > maxSetTimeoutMs) {
const newTimeoutMs = timeoutMs - maxSetTimeoutMs;
timeoutId = setTimeout(
() => setLongTimeout(timeoutFn, newTimeoutMs, setTimeoutIdFn),
maxSetTimeoutMs,
);
} else {
timeoutId = setTimeout(() => timeoutFn(), Math.max(timeoutMs, 0));
}
setTimeoutIdFn?.(timeoutId);
return timeoutId;
}
/**
* This class is a thin wrapper over setTimeout and clearTimeout which
* makes it simpler to keep track of recurring timeouts with the same
* or similar handlers and timeouts. This class supports long timeouts
* or timeouts exceeding (2^31)-1 ms or approximately 24.8 days.
*
* @deprecated Moved to the `@fluidframework/core-utils` package.
* @internal
*/
export class Timer implements ITimer {
/**
* Returns true if the timer is running.
*/
public get hasTimer(): boolean {
return !!this.runningState;
}
private runningState: IRunningTimerState | undefined;
constructor(
private readonly defaultTimeout: number,
private readonly defaultHandler: () => void,
private readonly getCurrentTick: () => number = (): number => Date.now(),
) {}
/**
* Calls setTimeout and tracks the resulting timeout.
* @param ms - overrides default timeout in ms
* @param handler - overrides default handler
*/
public start(
ms: number = this.defaultTimeout,
handler: () => void = this.defaultHandler,
): void {
this.startCore(ms, handler, ms);
}
/**
* Calls clearTimeout on the underlying timeout if running.
*/
public clear(): void {
if (!this.runningState) {
return;
}
clearTimeout(this.runningState.timeout);
this.runningState = undefined;
}
/**
* Restarts the timer with the new handler and duration.
* If a new handler is passed, the original handler may
* never execute.
* This is a potentially more efficient way to clear and start
* a new timer.
* @param ms - overrides previous or default timeout in ms
* @param handler - overrides previous or default handler
*/
public restart(ms?: number, handler?: () => void): void {
if (this.runningState) {
const duration = ms ?? this.runningState.intendedDuration;
const handlerToUse =
handler ?? this.runningState.restart?.handler ?? this.runningState.handler;
const remainingTime = this.calculateRemainingTime(this.runningState);
if (duration < remainingTime) {
// If remaining time exceeds restart duration, do a hard restart.
// The existing timeout time is too long.
this.start(duration, handlerToUse);
} else if (duration === remainingTime) {
// The existing timeout time is perfect, just update handler and data.
this.runningState.handler = handlerToUse;
this.runningState.restart = undefined;
this.runningState.intendedDuration = duration;
} else {
// If restart duration exceeds remaining time, set restart info.
// Existing timeout will start a new timeout for remaining time.
this.runningState.restart = {
startTick: this.getCurrentTick(),
duration,
handler: handlerToUse,
};
}
} else {
// If restart is called first, it behaves as a call to start
this.start(ms, handler);
}
}
private startCore(duration: number, handler: () => void, intendedDuration: number): void {
this.clear();
this.runningState = {
startTick: this.getCurrentTick(),
duration,
intendedDuration,
handler,
timeout: setLongTimeout(
() => this.handler(),
duration,
(timer: number) => {
if (this.runningState !== undefined) {
this.runningState.timeout = timer;
}
},
),
};
}
private handler(): void {
assert(!!this.runningState, 0x00a /* "Running timer missing handler" */);
const restart = this.runningState.restart;
if (restart === undefined) {
// Run clear first, in case the handler decides to start again
const handler = this.runningState.handler;
this.clear();
handler();
} else {
// Restart with remaining time
const remainingTime = this.calculateRemainingTime(restart);
this.startCore(remainingTime, () => restart.handler(), restart.duration);
}
}
private calculateRemainingTime(runningTimeout: ITimeout): number {
const elapsedTime = this.getCurrentTick() - runningTimeout.startTick;
return runningTimeout.duration - elapsedTime;
}
}
/**
* @deprecated Moved to the `@fluidframework/core-utils` package.
* @internal
*/
export interface IPromiseTimerResult {
timerResult: "timeout" | "cancel";
}
/**
* Timer which offers a promise that fulfills when the timer
* completes.
*
* @deprecated Moved to the `@fluid-private/client-utils` package.
* @internal
*/
export interface IPromiseTimer extends ITimer {
/**
* Starts the timer and returns a promise that
* resolves when the timer times out or is canceled.
*/
start(): Promise<IPromiseTimerResult>;
}
/**
* This class is a wrapper over setTimeout and clearTimeout which
* makes it simpler to keep track of recurring timeouts with the
* same handlers and timeouts, while also providing a promise that
* resolves when it times out.
*
* @deprecated Moved to the `@fluid-private/client-utils` package.
* @internal
*/
export class PromiseTimer implements IPromiseTimer {
private deferred?: Deferred<IPromiseTimerResult>;
private readonly timer: Timer;
/**
* {@inheritDoc Timer.hasTimer}
*/
public get hasTimer(): boolean {
return this.timer.hasTimer;
}
constructor(defaultTimeout: number, defaultHandler: () => void) {
this.timer = new Timer(defaultTimeout, () => this.wrapHandler(defaultHandler));
}
/**
* {@inheritDoc IPromiseTimer.start}
*/
public async start(ms?: number, handler?: () => void): Promise<IPromiseTimerResult> {
this.clear();
this.deferred = new Deferred<IPromiseTimerResult>();
this.timer.start(ms, handler ? (): void => this.wrapHandler(handler) : undefined);
return this.deferred.promise;
}
public clear(): void {
this.timer.clear();
if (this.deferred) {
this.deferred.resolve({ timerResult: "cancel" });
this.deferred = undefined;
}
}
protected wrapHandler(handler: () => void): void {
handler();
assert(!!this.deferred, 0x00b /* "Handler executed without deferred" */);
this.deferred.resolve({ timerResult: "timeout" });
this.deferred = undefined;
}
}