@fluidframework/core-utils 2.0.0-internal.6.4.0 → 2.0.0-internal.7.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/api-extractor.json +10 -1
- package/api-report/core-utils.api.md +147 -0
- package/dist/core-utils-alpha.d.ts +381 -0
- package/dist/core-utils-beta.d.ts +381 -0
- package/dist/core-utils-public.d.ts +381 -0
- package/dist/core-utils.d.ts +391 -0
- package/dist/lazy.js +3 -3
- package/dist/lazy.js.map +1 -1
- package/dist/promiseCache.d.ts +1 -1
- package/dist/promiseCache.d.ts.map +1 -1
- package/dist/timer.js +8 -8
- package/dist/timer.js.map +1 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/lazy.js +3 -3
- package/lib/lazy.js.map +1 -1
- package/lib/promiseCache.d.ts +1 -1
- package/lib/promiseCache.d.ts.map +1 -1
- package/lib/timer.js +8 -8
- package/lib/timer.js.map +1 -1
- package/package.json +13 -13
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A browser friendly assert library.
|
|
3
|
+
* Use this instead of the 'assert' package, which has a big impact on bundle sizes.
|
|
4
|
+
* @param condition - The condition that should be true, if the condition is false an error will be thrown.
|
|
5
|
+
* Only use this API when `false` indicates a logic error in the problem and thus a bug that should be fixed.
|
|
6
|
+
* @param message - The message to include in the error when the condition does not hold.
|
|
7
|
+
* A number should not be specified manually: use a string.
|
|
8
|
+
* Before a release, policy-check should be run, which will convert any asserts still using strings to
|
|
9
|
+
* use numbered error codes instead.
|
|
10
|
+
*/
|
|
11
|
+
export declare function assert(condition: boolean, message: string | number): asserts condition;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Compare two arrays. Returns true if their elements are equivalent and in the same order.
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*
|
|
18
|
+
* @param left - The first array to compare
|
|
19
|
+
* @param right - The second array to compare
|
|
20
|
+
* @param comparator - The function used to check if two `T`s are equivalent.
|
|
21
|
+
* Defaults to `Object.is()` equality (a shallow compare where NaN = NaN and -0 ≠ 0)
|
|
22
|
+
*/
|
|
23
|
+
export declare const compareArrays: <T>(left: readonly T[], right: readonly T[], comparator?: (leftItem: T, rightItem: T, index: number) => boolean) => boolean;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A deferred creates a promise and the ability to resolve or reject it
|
|
27
|
+
*/
|
|
28
|
+
export declare class Deferred<T> {
|
|
29
|
+
private readonly p;
|
|
30
|
+
private res;
|
|
31
|
+
private rej;
|
|
32
|
+
private completed;
|
|
33
|
+
constructor();
|
|
34
|
+
/**
|
|
35
|
+
* Returns whether the underlying promise has been completed
|
|
36
|
+
*/
|
|
37
|
+
get isCompleted(): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Retrieves the underlying promise for the deferred
|
|
40
|
+
*
|
|
41
|
+
* @returns the underlying promise
|
|
42
|
+
*/
|
|
43
|
+
get promise(): Promise<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Resolves the promise
|
|
46
|
+
*
|
|
47
|
+
* @param value - the value to resolve the promise with
|
|
48
|
+
*/
|
|
49
|
+
resolve(value: T | PromiseLike<T>): void;
|
|
50
|
+
/**
|
|
51
|
+
* Rejects the promise
|
|
52
|
+
*
|
|
53
|
+
* @param value - the value to reject the promise with
|
|
54
|
+
*/
|
|
55
|
+
reject(error: any): void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Returns a promise that resolves after `timeMs`.
|
|
60
|
+
* @param timeMs - Time in milliseconds to wait.
|
|
61
|
+
*/
|
|
62
|
+
export declare const delay: (timeMs: number) => Promise<void>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Ordered {@link https://en.wikipedia.org/wiki/Heap_(data_structure) | Heap} data structure implementation.
|
|
66
|
+
*/
|
|
67
|
+
export declare class Heap<T> {
|
|
68
|
+
comp: IComparer<T>;
|
|
69
|
+
private L;
|
|
70
|
+
/**
|
|
71
|
+
* Creates an instance of `Heap` with comparer.
|
|
72
|
+
* @param comp - A comparer that specify how elements are ordered.
|
|
73
|
+
*/
|
|
74
|
+
constructor(comp: IComparer<T>);
|
|
75
|
+
/**
|
|
76
|
+
* Return the smallest element in the heap as determined by the order of the comparer
|
|
77
|
+
*
|
|
78
|
+
* @returns Heap node containing the smallest element
|
|
79
|
+
*/
|
|
80
|
+
peek(): IHeapNode<T>;
|
|
81
|
+
/**
|
|
82
|
+
* Get and remove the smallest element in the heap as determined by the order of the comparer
|
|
83
|
+
*
|
|
84
|
+
* @returns The smallest value in the heap
|
|
85
|
+
*/
|
|
86
|
+
get(): T;
|
|
87
|
+
/**
|
|
88
|
+
* Add a value to the heap
|
|
89
|
+
*
|
|
90
|
+
* @param x - value to add
|
|
91
|
+
* @returns The heap node that contains the value
|
|
92
|
+
*/
|
|
93
|
+
add(x: T): IHeapNode<T>;
|
|
94
|
+
/**
|
|
95
|
+
* Allows for the Heap to be updated after a node's value changes.
|
|
96
|
+
*/
|
|
97
|
+
update(node: IHeapNode<T>): void;
|
|
98
|
+
/**
|
|
99
|
+
* Removes the given node from the heap.
|
|
100
|
+
*
|
|
101
|
+
* @param node - The node to remove from the heap.
|
|
102
|
+
*/
|
|
103
|
+
remove(node: IHeapNode<T>): void;
|
|
104
|
+
/**
|
|
105
|
+
* Get the number of elements in the Heap.
|
|
106
|
+
*
|
|
107
|
+
* @returns The number of elements in the Heap.
|
|
108
|
+
*/
|
|
109
|
+
count(): number;
|
|
110
|
+
private fixup;
|
|
111
|
+
private isGreaterThanParent;
|
|
112
|
+
private fixdown;
|
|
113
|
+
private swap;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Interface for a comparer.
|
|
118
|
+
*/
|
|
119
|
+
export declare interface IComparer<T> {
|
|
120
|
+
/**
|
|
121
|
+
* The minimum value of type T.
|
|
122
|
+
*/
|
|
123
|
+
min: T;
|
|
124
|
+
/**
|
|
125
|
+
* Compare the two value
|
|
126
|
+
*
|
|
127
|
+
* @returns 0 if the value is equal, negative number if a is smaller then b, positive number otherwise
|
|
128
|
+
*/
|
|
129
|
+
compare(a: T, b: T): number;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Interface to a node in {@link Heap}.
|
|
134
|
+
*/
|
|
135
|
+
export declare interface IHeapNode<T> {
|
|
136
|
+
value: T;
|
|
137
|
+
position: number;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Timer which offers a promise that fulfills when the timer
|
|
142
|
+
* completes.
|
|
143
|
+
*/
|
|
144
|
+
export declare interface IPromiseTimer extends ITimer {
|
|
145
|
+
/**
|
|
146
|
+
* Starts the timer and returns a promise that
|
|
147
|
+
* resolves when the timer times out or is canceled.
|
|
148
|
+
*/
|
|
149
|
+
start(): Promise<IPromiseTimerResult>;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export declare interface IPromiseTimerResult {
|
|
153
|
+
timerResult: "timeout" | "cancel";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export declare interface ITimer {
|
|
157
|
+
/**
|
|
158
|
+
* True if timer is currently running
|
|
159
|
+
*/
|
|
160
|
+
readonly hasTimer: boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Starts the timer
|
|
163
|
+
*/
|
|
164
|
+
start(): void;
|
|
165
|
+
/**
|
|
166
|
+
* Cancels the timer if already running
|
|
167
|
+
*/
|
|
168
|
+
clear(): void;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Helper class for lazy initialized values. Ensures the value is only generated once, and remain immutable.
|
|
173
|
+
*/
|
|
174
|
+
export declare class Lazy<T> {
|
|
175
|
+
private readonly valueGenerator;
|
|
176
|
+
private _value;
|
|
177
|
+
private _evaluated;
|
|
178
|
+
/**
|
|
179
|
+
* Instantiates an instance of Lazy<T>.
|
|
180
|
+
* @param valueGenerator - The function that will generate the value when value is accessed the first time.
|
|
181
|
+
*/
|
|
182
|
+
constructor(valueGenerator: () => T);
|
|
183
|
+
/**
|
|
184
|
+
* Return true if the value as been generated, otherwise false.
|
|
185
|
+
*/
|
|
186
|
+
get evaluated(): boolean;
|
|
187
|
+
/**
|
|
188
|
+
* Get the value. If this is the first call the value will be generated.
|
|
189
|
+
*/
|
|
190
|
+
get value(): T;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* A lazy evaluated promise. The execute function is delayed until
|
|
195
|
+
* the promise is used, e.g. await, then, catch ...
|
|
196
|
+
* The execute function is only called once.
|
|
197
|
+
* All calls are then proxied to the promise returned by the execute method.
|
|
198
|
+
*/
|
|
199
|
+
export declare class LazyPromise<T> implements Promise<T> {
|
|
200
|
+
private readonly execute;
|
|
201
|
+
get [Symbol.toStringTag](): string;
|
|
202
|
+
private result;
|
|
203
|
+
constructor(execute: () => Promise<T>);
|
|
204
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
|
|
205
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<T | TResult>;
|
|
206
|
+
finally(onfinally?: (() => void) | null | undefined): Promise<T>;
|
|
207
|
+
private getPromise;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* A comparer for numbers.
|
|
212
|
+
*/
|
|
213
|
+
export declare const NumberComparer: IComparer<number>;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* A specialized cache for async work, allowing you to safely cache the promised result of some async work
|
|
217
|
+
* without fear of running it multiple times or losing track of errors.
|
|
218
|
+
*/
|
|
219
|
+
export declare class PromiseCache<TKey, TResult> {
|
|
220
|
+
private readonly cache;
|
|
221
|
+
private readonly gc;
|
|
222
|
+
private readonly removeOnError;
|
|
223
|
+
/**
|
|
224
|
+
* Create the PromiseCache with the given options, with the following defaults:
|
|
225
|
+
*
|
|
226
|
+
* expiry: indefinite, removeOnError: true for all errors
|
|
227
|
+
*/
|
|
228
|
+
constructor({ expiry, removeOnError, }?: PromiseCacheOptions);
|
|
229
|
+
/**
|
|
230
|
+
* Check if there's anything cached at the given key
|
|
231
|
+
*/
|
|
232
|
+
has(key: TKey): boolean;
|
|
233
|
+
/**
|
|
234
|
+
* Get the Promise for the given key, or undefined if it's not found.
|
|
235
|
+
* Extend expiry if applicable.
|
|
236
|
+
*/
|
|
237
|
+
get(key: TKey): Promise<TResult> | undefined;
|
|
238
|
+
/**
|
|
239
|
+
* Remove the Promise for the given key, returning true if it was found and removed
|
|
240
|
+
*/
|
|
241
|
+
remove(key: TKey): boolean;
|
|
242
|
+
/**
|
|
243
|
+
* Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
|
|
244
|
+
* Returns a Promise for the added or existing async work being done at that key.
|
|
245
|
+
* @param key - key name where to store the async work
|
|
246
|
+
* @param asyncFn - the async work to do and store, if not already in progress under the given key
|
|
247
|
+
*/
|
|
248
|
+
addOrGet(key: TKey, asyncFn: () => Promise<TResult>): Promise<TResult>;
|
|
249
|
+
/**
|
|
250
|
+
* Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
|
|
251
|
+
* Returns false if the cache already contained an entry at that key, and true otherwise.
|
|
252
|
+
* @param key - key name where to store the async work
|
|
253
|
+
* @param asyncFn - the async work to do and store, if not already in progress under the given key
|
|
254
|
+
*/
|
|
255
|
+
add(key: TKey, asyncFn: () => Promise<TResult>): boolean;
|
|
256
|
+
/**
|
|
257
|
+
* Try to add the given value, without overwriting an existing cache entry at that key.
|
|
258
|
+
* Returns a Promise for the added or existing async work being done at that key.
|
|
259
|
+
* @param key - key name where to store the async work
|
|
260
|
+
* @param value - value to store
|
|
261
|
+
*/
|
|
262
|
+
addValueOrGet(key: TKey, value: TResult): Promise<TResult>;
|
|
263
|
+
/**
|
|
264
|
+
* Try to add the given value, without overwriting an existing cache entry at that key.
|
|
265
|
+
* Returns false if the cache already contained an entry at that key, and true otherwise.
|
|
266
|
+
* @param key - key name where to store the value
|
|
267
|
+
* @param value - value to store
|
|
268
|
+
*/
|
|
269
|
+
addValue(key: TKey, value: TResult): boolean;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Three supported expiry policies:
|
|
274
|
+
* - indefinite: entries don't expire and must be explicitly removed
|
|
275
|
+
* - absolute: entries expire after the given duration in MS, even if accessed multiple times in the mean time
|
|
276
|
+
* - sliding: entries expire after the given duration in MS of inactivity (i.e. get resets the clock)
|
|
277
|
+
*/
|
|
278
|
+
export declare type PromiseCacheExpiry = {
|
|
279
|
+
policy: "indefinite";
|
|
280
|
+
} | {
|
|
281
|
+
policy: "absolute" | "sliding";
|
|
282
|
+
durationMs: number;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Options for configuring the {@link PromiseCache}
|
|
287
|
+
*/
|
|
288
|
+
export declare interface PromiseCacheOptions {
|
|
289
|
+
/**
|
|
290
|
+
* Common expiration policy for all items added to this cache
|
|
291
|
+
*/
|
|
292
|
+
expiry?: PromiseCacheExpiry;
|
|
293
|
+
/**
|
|
294
|
+
* If the stored Promise is rejected with a particular error, should the given key be removed?
|
|
295
|
+
*/
|
|
296
|
+
removeOnError?: (error: any) => boolean;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* This class is a wrapper over setTimeout and clearTimeout which
|
|
301
|
+
* makes it simpler to keep track of recurring timeouts with the
|
|
302
|
+
* same handlers and timeouts, while also providing a promise that
|
|
303
|
+
* resolves when it times out.
|
|
304
|
+
*/
|
|
305
|
+
export declare class PromiseTimer implements IPromiseTimer {
|
|
306
|
+
private deferred?;
|
|
307
|
+
private readonly timer;
|
|
308
|
+
/**
|
|
309
|
+
* {@inheritDoc Timer.hasTimer}
|
|
310
|
+
*/
|
|
311
|
+
get hasTimer(): boolean;
|
|
312
|
+
constructor(defaultTimeout: number, defaultHandler: () => void);
|
|
313
|
+
/**
|
|
314
|
+
* {@inheritDoc IPromiseTimer.start}
|
|
315
|
+
*/
|
|
316
|
+
start(ms?: number, handler?: () => void): Promise<IPromiseTimerResult>;
|
|
317
|
+
clear(): void;
|
|
318
|
+
protected wrapHandler(handler: () => void): void;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Sets timeouts like the setTimeout function allowing timeouts to exceed the setTimeout's max timeout limit.
|
|
323
|
+
* Timeouts may not be exactly accurate due to browser implementations and the OS.
|
|
324
|
+
* https://stackoverflow.com/questions/21097421/what-is-the-reason-javascript-settimeout-is-so-inaccurate
|
|
325
|
+
* @param timeoutFn - Executed when the timeout expires
|
|
326
|
+
* @param timeoutMs - Duration of the timeout in milliseconds
|
|
327
|
+
* @param setTimeoutIdFn - Executed to update the timeout if multiple timeouts are required when
|
|
328
|
+
* timeoutMs greater than maxTimeout
|
|
329
|
+
* @returns The initial timeout
|
|
330
|
+
*/
|
|
331
|
+
export declare function setLongTimeout(timeoutFn: () => void, timeoutMs: number, setTimeoutIdFn?: (timeoutId: ReturnType<typeof setTimeout>) => void): ReturnType<typeof setTimeout>;
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* This class is a thin wrapper over setTimeout and clearTimeout which
|
|
335
|
+
* makes it simpler to keep track of recurring timeouts with the same
|
|
336
|
+
* or similar handlers and timeouts. This class supports long timeouts
|
|
337
|
+
* or timeouts exceeding (2^31)-1 ms or approximately 24.8 days.
|
|
338
|
+
*/
|
|
339
|
+
export declare class Timer implements ITimer {
|
|
340
|
+
private readonly defaultTimeout;
|
|
341
|
+
private readonly defaultHandler;
|
|
342
|
+
private readonly getCurrentTick;
|
|
343
|
+
/**
|
|
344
|
+
* Returns true if the timer is running.
|
|
345
|
+
*/
|
|
346
|
+
get hasTimer(): boolean;
|
|
347
|
+
private runningState;
|
|
348
|
+
constructor(defaultTimeout: number, defaultHandler: () => void, getCurrentTick?: () => number);
|
|
349
|
+
/**
|
|
350
|
+
* Calls setTimeout and tracks the resulting timeout.
|
|
351
|
+
* @param ms - overrides default timeout in ms
|
|
352
|
+
* @param handler - overrides default handler
|
|
353
|
+
*/
|
|
354
|
+
start(ms?: number, handler?: () => void): void;
|
|
355
|
+
/**
|
|
356
|
+
* Calls clearTimeout on the underlying timeout if running.
|
|
357
|
+
*/
|
|
358
|
+
clear(): void;
|
|
359
|
+
/**
|
|
360
|
+
* Restarts the timer with the new handler and duration.
|
|
361
|
+
* If a new handler is passed, the original handler may
|
|
362
|
+
* never execute.
|
|
363
|
+
* This is a potentially more efficient way to clear and start
|
|
364
|
+
* a new timer.
|
|
365
|
+
* @param ms - overrides previous or default timeout in ms
|
|
366
|
+
* @param handler - overrides previous or default handler
|
|
367
|
+
*/
|
|
368
|
+
restart(ms?: number, handler?: () => void): void;
|
|
369
|
+
private startCore;
|
|
370
|
+
private handler;
|
|
371
|
+
private calculateRemainingTime;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* This function can be used to assert at compile time that a given value has type never.
|
|
376
|
+
* One common usage is in the default case of a switch block,
|
|
377
|
+
* to ensure that all cases are explicitly handled.
|
|
378
|
+
*
|
|
379
|
+
* Example:
|
|
380
|
+
* ```typescript
|
|
381
|
+
* const bool: true | false = ...;
|
|
382
|
+
* switch(bool) {
|
|
383
|
+
* case true: {...}
|
|
384
|
+
* case false: {...}
|
|
385
|
+
* default: unreachableCase(bool);
|
|
386
|
+
* }
|
|
387
|
+
* ```
|
|
388
|
+
*/
|
|
389
|
+
export declare function unreachableCase(_: never, message?: string): never;
|
|
390
|
+
|
|
391
|
+
export { }
|
package/dist/lazy.js
CHANGED
|
@@ -43,12 +43,12 @@ exports.Lazy = Lazy;
|
|
|
43
43
|
* All calls are then proxied to the promise returned by the execute method.
|
|
44
44
|
*/
|
|
45
45
|
class LazyPromise {
|
|
46
|
-
constructor(execute) {
|
|
47
|
-
this.execute = execute;
|
|
48
|
-
}
|
|
49
46
|
get [Symbol.toStringTag]() {
|
|
50
47
|
return this.getPromise()[Symbol.toStringTag];
|
|
51
48
|
}
|
|
49
|
+
constructor(execute) {
|
|
50
|
+
this.execute = execute;
|
|
51
|
+
}
|
|
52
52
|
// eslint-disable-next-line unicorn/no-thenable
|
|
53
53
|
async then(
|
|
54
54
|
// eslint-disable-next-line @rushstack/no-new-null
|
package/dist/lazy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lazy.js","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;GAEG;AACH,MAAa,IAAI;IAGhB;;;OAGG;IACH,YAAoC,cAAuB;QAAvB,mBAAc,GAAd,cAAc,CAAS;QALnD,eAAU,GAAY,KAAK,CAAC;IAK0B,CAAC;IAE/D;;OAEG;IACH,IAAW,SAAS;QACnB,OAAO,IAAI,CAAC,UAAU,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,IAAW,KAAK;QACf,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACrB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;YACvB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;SACpC;QACD,oEAAoE;QACpE,OAAO,IAAI,CAAC,MAAO,CAAC;IACrB,CAAC;CACD;AA3BD,oBA2BC;AAED;;;;;GAKG;AACH,MAAa,WAAW;
|
|
1
|
+
{"version":3,"file":"lazy.js","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;GAEG;AACH,MAAa,IAAI;IAGhB;;;OAGG;IACH,YAAoC,cAAuB;QAAvB,mBAAc,GAAd,cAAc,CAAS;QALnD,eAAU,GAAY,KAAK,CAAC;IAK0B,CAAC;IAE/D;;OAEG;IACH,IAAW,SAAS;QACnB,OAAO,IA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C,MAAM,CAAC;IACpB,CAAC;CACD;AA1CD,kCA0CC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Helper class for lazy initialized values. Ensures the value is only generated once, and remain immutable.\n */\nexport class Lazy<T> {\n\tprivate _value: T | undefined;\n\tprivate _evaluated: boolean = false;\n\t/**\n\t * Instantiates an instance of Lazy<T>.\n\t * @param valueGenerator - The function that will generate the value when value is accessed the first time.\n\t */\n\tpublic constructor(private readonly valueGenerator: () => T) {}\n\n\t/**\n\t * Return true if the value as been generated, otherwise false.\n\t */\n\tpublic get evaluated(): boolean {\n\t\treturn this._evaluated;\n\t}\n\n\t/**\n\t * Get the value. If this is the first call the value will be generated.\n\t */\n\tpublic get value(): T {\n\t\tif (!this._evaluated) {\n\t\t\tthis._evaluated = true;\n\t\t\tthis._value = this.valueGenerator();\n\t\t}\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\treturn this._value!;\n\t}\n}\n\n/**\n * A lazy evaluated promise. The execute function is delayed until\n * the promise is used, e.g. await, then, catch ...\n * The execute function is only called once.\n * All calls are then proxied to the promise returned by the execute method.\n */\nexport class LazyPromise<T> implements Promise<T> {\n\tpublic get [Symbol.toStringTag](): string {\n\t\treturn this.getPromise()[Symbol.toStringTag];\n\t}\n\n\tprivate result: Promise<T> | undefined;\n\n\tpublic constructor(private readonly execute: () => Promise<T>) {}\n\n\t// eslint-disable-next-line unicorn/no-thenable\n\tpublic async then<TResult1 = T, TResult2 = never>(\n\t\t// eslint-disable-next-line @rushstack/no-new-null\n\t\tonfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,\n\t\t// TODO: Use `unknown` instead (API breaking)\n\t\t// eslint-disable-next-line @rushstack/no-new-null, @typescript-eslint/no-explicit-any\n\t\tonrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined,\n\t): Promise<TResult1 | TResult2> {\n\t\t// eslint-disable-next-line prefer-rest-params\n\t\treturn this.getPromise().then<TResult1, TResult2>(...arguments);\n\t}\n\n\tpublic async catch<TResult = never>(\n\t\t// TODO: Use `unknown` instead (API breaking)\n\t\t// eslint-disable-next-line @rushstack/no-new-null, @typescript-eslint/no-explicit-any\n\t\tonrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined,\n\t): Promise<T | TResult> {\n\t\t// eslint-disable-next-line prefer-rest-params\n\t\treturn this.getPromise().catch<TResult>(...arguments);\n\t}\n\n\t// eslint-disable-next-line @rushstack/no-new-null\n\tpublic async finally(onfinally?: (() => void) | null | undefined): Promise<T> {\n\t\t// eslint-disable-next-line prefer-rest-params\n\t\treturn this.getPromise().finally(...arguments);\n\t}\n\n\tprivate async getPromise(): Promise<T> {\n\t\tif (this.result === undefined) {\n\t\t\tthis.result = this.execute();\n\t\t}\n\t\treturn this.result;\n\t}\n}\n"]}
|
package/dist/promiseCache.d.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - absolute: entries expire after the given duration in MS, even if accessed multiple times in the mean time
|
|
9
9
|
* - sliding: entries expire after the given duration in MS of inactivity (i.e. get resets the clock)
|
|
10
10
|
*/
|
|
11
|
-
export
|
|
11
|
+
export type PromiseCacheExpiry = {
|
|
12
12
|
policy: "indefinite";
|
|
13
13
|
} | {
|
|
14
14
|
policy: "absolute" | "sliding";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promiseCache.d.ts","sourceRoot":"","sources":["../src/promiseCache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AACH,
|
|
1
|
+
{"version":3,"file":"promiseCache.d.ts","sourceRoot":"","sources":["../src/promiseCache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,kBAAkB,GAC3B;IACA,MAAM,EAAE,YAAY,CAAC;CACpB,GACD;IACA,MAAM,EAAE,UAAU,GAAG,SAAS,CAAC;IAC/B,UAA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ceeding (2^31)-1 ms or approximately 24.8 days.
|
|
40
40
|
*/
|
|
41
41
|
class Timer {
|
|
42
|
-
constructor(defaultTimeout, defaultHandler, getCurrentTick = () => Date.now()) {
|
|
43
|
-
this.defaultTimeout = defaultTimeout;
|
|
44
|
-
this.defaultHandler = defaultHandler;
|
|
45
|
-
this.getCurrentTick = getCurrentTick;
|
|
46
|
-
}
|
|
47
42
|
/**
|
|
48
43
|
* Returns true if the timer is running.
|
|
49
44
|
*/
|
|
50
45
|
get hasTimer() {
|
|
51
46
|
return !!this.runningState;
|
|
52
47
|
}
|
|
48
|
+
constructor(defaultTimeout, defaultHandler, getCurrentTick = () => Date.now()) {
|
|
49
|
+
this.defaultTimeout = defaultTimeout;
|
|
50
|
+
this.defaultHandler = defaultHandler;
|
|
51
|
+
this.getCurrentTick = getCurrentTick;
|
|
52
|
+
}
|
|
53
53
|
/**
|
|
54
54
|
* Calls setTimeout and tracks the resulting timeout.
|
|
55
55
|
* @param ms - overrides default timeout in ms
|
|
@@ -150,15 +150,15 @@ exports.Timer = Timer;
|
|
|
150
150
|
* resolves when it times out.
|
|
151
151
|
*/
|
|
152
152
|
class PromiseTimer {
|
|
153
|
-
constructor(defaultTimeout, defaultHandler) {
|
|
154
|
-
this.timer = new Timer(defaultTimeout, () => this.wrapHandler(defaultHandler));
|
|
155
|
-
}
|
|
156
153
|
/**
|
|
157
154
|
* {@inheritDoc Timer.hasTimer}
|
|
158
155
|
*/
|
|
159
156
|
get hasTimer() {
|
|
160
157
|
return this.timer.hasTimer;
|
|
161
158
|
}
|
|
159
|
+
constructor(defaultTimeout, defaultHandler) {
|
|
160
|
+
this.timer = new Timer(defaultTimeout, () => this.wrapHandler(defaultHandler));
|
|
161
|
+
}
|
|
162
162
|
/**
|
|
163
163
|
* {@inheritDoc IPromiseTimer.start}
|
|
164
164
|
*/
|
package/dist/timer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timer.js","sourceRoot":"","sources":["../src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n */\n\nimport { assert } from \"./assert\";\nimport { Deferred } from \"./promises\";\n\nexport interface ITimer {\n\t/**\n\t * True if timer is currently running\n\t */\n\treadonly hasTimer: boolean;\n\n\t/**\n\t * Starts the timer\n\t */\n\tstart(): void;\n\n\t/**\n\t * Cancels the timer if already running\n\t */\n\tclear(): void;\n}\n\ninterface ITimeout {\n\t/**\n\t * Tick that timeout was started.\n\t */\n\tstartTick: number;\n\n\t/**\n\t * Timeout duration in ms.\n\t */\n\tduration: number;\n\n\t/**\n\t * Handler to execute when timeout ends.\n\t */\n\thandler: () => void;\n}\n\ninterface IRunningTimerState extends ITimeout {\n\t/**\n\t * JavaScript Timeout object.\n\t */\n\ttimeout: ReturnType<typeof setTimeout>;\n\n\t/**\n\t * Intended duration in ms.\n\t */\n\tintendedDuration: number;\n\n\t/**\n\t * Intended restart timeout.\n\t */\n\trestart?: ITimeout;\n}\n\nconst maxSetTimeoutMs = 0x7fffffff; // setTimeout limit is MAX_INT32=(2^31-1).\n\n/**\n * Sets timeouts like the setTimeout function allowing timeouts to exceed the setTimeout's max timeout limit.\n * Timeouts may not be exactly accurate due to browser implementations and the OS.\n * https://stackoverflow.com/questions/21097421/what-is-the-reason-javascript-settimeout-is-so-inaccurate\n * @param timeoutFn - Executed when the timeout expires\n * @param timeoutMs - Duration of the timeout in milliseconds\n * @param setTimeoutIdFn - Executed to update the timeout if multiple timeouts are required when\n * timeoutMs greater than maxTimeout\n * @returns The initial timeout\n */\nexport function setLongTimeout(\n\ttimeoutFn: () => void,\n\ttimeoutMs: number,\n\tsetTimeoutIdFn?: (timeoutId: ReturnType<typeof setTimeout>) => void,\n): ReturnType<typeof setTimeout> {\n\t// The setTimeout max is 24.8 days before looping occurs.\n\tlet timeoutId: ReturnType<typeof setTimeout>;\n\tif (timeoutMs > maxSetTimeoutMs) {\n\t\tconst newTimeoutMs = timeoutMs - maxSetTimeoutMs;\n\t\ttimeoutId = setTimeout(\n\t\t\t() => setLongTimeout(timeoutFn, newTimeoutMs, setTimeoutIdFn),\n\t\t\tmaxSetTimeoutMs,\n\t\t);\n\t} else {\n\t\ttimeoutId = setTimeout(() => timeoutFn(), Math.max(timeoutMs, 0));\n\t}\n\n\tsetTimeoutIdFn?.(timeoutId);\n\treturn timeoutId;\n}\n\n/**\n * This class is a thin wrapper over setTimeout and clearTimeout which\n * makes it simpler to keep track of recurring timeouts with the same\n * or similar handlers and timeouts. This class supports long timeouts\n * or timeouts exceeding (2^31)-1 ms or approximately 24.8 days.\n */\nexport class Timer implements ITimer {\n\t/**\n\t * Returns true if the timer is running.\n\t */\n\tpublic get hasTimer(): boolean {\n\t\treturn !!this.runningState;\n\t}\n\n\tprivate runningState: IRunningTimerState | undefined;\n\n\tpublic constructor(\n\t\tprivate readonly defaultTimeout: number,\n\t\tprivate readonly defaultHandler: () => void,\n\t\tprivate readonly getCurrentTick: () => number = (): number => Date.now(),\n\t) {}\n\n\t/**\n\t * Calls setTimeout and tracks the resulting timeout.\n\t * @param ms - overrides default timeout in ms\n\t * @param handler - overrides default handler\n\t */\n\tpublic start(\n\t\tms: number = this.defaultTimeout,\n\t\thandler: () => void = this.defaultHandler,\n\t): void {\n\t\tthis.startCore(ms, handler, ms);\n\t}\n\n\t/**\n\t * Calls clearTimeout on the underlying timeout if running.\n\t */\n\tpublic clear(): void {\n\t\tif (!this.runningState) {\n\t\t\treturn;\n\t\t}\n\t\tclearTimeout(this.runningState.timeout);\n\t\tthis.runningState = undefined;\n\t}\n\n\t/**\n\t * Restarts the timer with the new handler and duration.\n\t * If a new handler is passed, the original handler may\n\t * never execute.\n\t * This is a potentially more efficient way to clear and start\n\t * a new timer.\n\t * @param ms - overrides previous or default timeout in ms\n\t * @param handler - overrides previous or default handler\n\t */\n\tpublic restart(ms?: number, handler?: () => void): void {\n\t\tif (this.runningState) {\n\t\t\tconst duration = ms ?? this.runningState.intendedDuration;\n\t\t\tconst handlerToUse =\n\t\t\t\thandler ?? this.runningState.restart?.handler ?? this.runningState.handler;\n\t\t\tconst remainingTime = this.calculateRemainingTime(this.runningState);\n\n\t\t\tif (duration < remainingTime) {\n\t\t\t\t// If remaining time exceeds restart duration, do a hard restart.\n\t\t\t\t// The existing timeout time is too long.\n\t\t\t\tthis.start(duration, handlerToUse);\n\t\t\t} else if (duration === remainingTime) {\n\t\t\t\t// The existing timeout time is perfect, just update handler and data.\n\t\t\t\tthis.runningState.handler = handlerToUse;\n\t\t\t\tthis.runningState.restart = undefined;\n\t\t\t\tthis.runningState.intendedDuration = duration;\n\t\t\t} else {\n\t\t\t\t// If restart duration exceeds remaining time, set restart info.\n\t\t\t\t// Existing timeout will start a new timeout for remaining time.\n\t\t\t\tthis.runningState.restart = {\n\t\t\t\t\tstartTick: this.getCurrentTick(),\n\t\t\t\t\tduration,\n\t\t\t\t\thandler: handlerToUse,\n\t\t\t\t};\n\t\t\t}\n\t\t} else {\n\t\t\t// If restart is called first, it behaves as a call to start\n\t\t\tthis.start(ms, handler);\n\t\t}\n\t}\n\n\tprivate startCore(duration: number, handler: () => void, intendedDuration: number): void {\n\t\tthis.clear();\n\t\tthis.runningState = {\n\t\t\tstartTick: this.getCurrentTick(),\n\t\t\tduration,\n\t\t\tintendedDuration,\n\t\t\thandler,\n\t\t\ttimeout: setLongTimeout(\n\t\t\t\t() => this.handler(),\n\t\t\t\tduration,\n\t\t\t\t(timer: number) => {\n\t\t\t\t\tif (this.runningState !== undefined) {\n\t\t\t\t\t\tthis.runningState.timeout = timer;\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t};\n\t}\n\n\tprivate handler(): void {\n\t\tassert(!!this.runningState, 0x764 /* Running timer missing handler */);\n\t\tconst restart = this.runningState.restart;\n\t\tif (restart === undefined) {\n\t\t\t// Run clear first, in case the handler decides to start again\n\t\t\tconst handler = this.runningState.handler;\n\t\t\tthis.clear();\n\t\t\thandler();\n\t\t} else {\n\t\t\t// Restart with remaining time\n\t\t\tconst remainingTime = this.calculateRemainingTime(restart);\n\t\t\tthis.startCore(remainingTime, () => restart.handler(), restart.duration);\n\t\t}\n\t}\n\n\tprivate calculateRemainingTime(runningTimeout: ITimeout): number {\n\t\tconst elapsedTime = this.getCurrentTick() - runningTimeout.startTick;\n\t\treturn runningTimeout.duration - elapsedTime;\n\t}\n}\n\nexport interface IPromiseTimerResult {\n\ttimerResult: \"timeout\" | \"cancel\";\n}\n\n/**\n * Timer which offers a promise that fulfills when the timer\n * completes.\n */\nexport interface IPromiseTimer extends ITimer {\n\t/**\n\t * Starts the timer and returns a promise that\n\t * resolves when the timer times out or is canceled.\n\t */\n\tstart(): Promise<IPromiseTimerResult>;\n}\n\n/**\n * This class is a wrapper over setTimeout and clearTimeout which\n * makes it simpler to keep track of recurring timeouts with the\n * same handlers and timeouts, while also providing a promise that\n * resolves when it times out.\n */\nexport class PromiseTimer implements IPromiseTimer {\n\tprivate deferred?: Deferred<IPromiseTimerResult>;\n\tprivate readonly timer: Timer;\n\n\t/**\n\t * {@inheritDoc Timer.hasTimer}\n\t */\n\tpublic get hasTimer(): boolean {\n\t\treturn this.timer.hasTimer;\n\t}\n\n\tpublic constructor(defaultTimeout: number, defaultHandler: () => void) {\n\t\tthis.timer = new Timer(defaultTimeout, () => this.wrapHandler(defaultHandler));\n\t}\n\n\t/**\n\t * {@inheritDoc IPromiseTimer.start}\n\t */\n\tpublic async start(ms?: number, handler?: () => void): Promise<IPromiseTimerResult> {\n\t\tthis.clear();\n\t\tthis.deferred = new Deferred<IPromiseTimerResult>();\n\t\tthis.timer.start(ms, handler ? (): void => this.wrapHandler(handler) : undefined);\n\t\treturn this.deferred.promise;\n\t}\n\n\tpublic clear(): void {\n\t\tthis.timer.clear();\n\t\tif (this.deferred) {\n\t\t\tthis.deferred.resolve({ timerResult: \"cancel\" });\n\t\t\tthis.deferred = undefined;\n\t\t}\n\t}\n\n\tprotected wrapHandler(handler: () => void): void {\n\t\thandler();\n\t\tassert(!!this.deferred, 0x765 /* Handler executed without deferred */);\n\t\tthis.deferred.resolve({ timerResult: \"timeout\" });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vCD,oCAuCC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"./assert\";\nimport { Deferred } from \"./promises\";\n\nexport interface ITimer {\n\t/**\n\t * True if timer is currently running\n\t */\n\treadonly hasTimer: boolean;\n\n\t/**\n\t * Starts the timer\n\t */\n\tstart(): void;\n\n\t/**\n\t * Cancels the timer if already running\n\t */\n\tclear(): void;\n}\n\ninterface ITimeout {\n\t/**\n\t * Tick that timeout was started.\n\t */\n\tstartTick: number;\n\n\t/**\n\t * Timeout duration in ms.\n\t */\n\tduration: number;\n\n\t/**\n\t * Handler to execute when timeout ends.\n\t */\n\thandler: () => void;\n}\n\ninterface IRunningTimerState extends ITimeout {\n\t/**\n\t * JavaScript Timeout object.\n\t */\n\ttimeout: ReturnType<typeof setTimeout>;\n\n\t/**\n\t * Intended duration in ms.\n\t */\n\tintendedDuration: number;\n\n\t/**\n\t * Intended restart timeout.\n\t */\n\trestart?: ITimeout;\n}\n\nconst maxSetTimeoutMs = 0x7fffffff; // setTimeout limit is MAX_INT32=(2^31-1).\n\n/**\n * Sets timeouts like the setTimeout function allowing timeouts to exceed the setTimeout's max timeout limit.\n * Timeouts may not be exactly accurate due to browser implementations and the OS.\n * https://stackoverflow.com/questions/21097421/what-is-the-reason-javascript-settimeout-is-so-inaccurate\n * @param timeoutFn - Executed when the timeout expires\n * @param timeoutMs - Duration of the timeout in milliseconds\n * @param setTimeoutIdFn - Executed to update the timeout if multiple timeouts are required when\n * timeoutMs greater than maxTimeout\n * @returns The initial timeout\n */\nexport function setLongTimeout(\n\ttimeoutFn: () => void,\n\ttimeoutMs: number,\n\tsetTimeoutIdFn?: (timeoutId: ReturnType<typeof setTimeout>) => void,\n): ReturnType<typeof setTimeout> {\n\t// The setTimeout max is 24.8 days before looping occurs.\n\tlet timeoutId: ReturnType<typeof setTimeout>;\n\tif (timeoutMs > maxSetTimeoutMs) {\n\t\tconst newTimeoutMs = timeoutMs - maxSetTimeoutMs;\n\t\ttimeoutId = setTimeout(\n\t\t\t() => setLongTimeout(timeoutFn, newTimeoutMs, setTimeoutIdFn),\n\t\t\tmaxSetTimeoutMs,\n\t\t);\n\t} else {\n\t\ttimeoutId = setTimeout(() => timeoutFn(), Math.max(timeoutMs, 0));\n\t}\n\n\tsetTimeoutIdFn?.(timeoutId);\n\treturn timeoutId;\n}\n\n/**\n * This class is a thin wrapper over setTimeout and clearTimeout which\n * makes it simpler to keep track of recurring timeouts with the same\n * or similar handlers and timeouts. This class supports long timeouts\n * or timeouts exceeding (2^31)-1 ms or approximately 24.8 days.\n */\nexport class Timer implements ITimer {\n\t/**\n\t * Returns true if the timer is running.\n\t */\n\tpublic get hasTimer(): boolean {\n\t\treturn !!this.runningState;\n\t}\n\n\tprivate runningState: IRunningTimerState | undefined;\n\n\tpublic constructor(\n\t\tprivate readonly defaultTimeout: number,\n\t\tprivate readonly defaultHandler: () => void,\n\t\tprivate readonly getCurrentTick: () => number = (): number => Date.now(),\n\t) {}\n\n\t/**\n\t * Calls setTimeout and tracks the resulting timeout.\n\t * @param ms - overrides default timeout in ms\n\t * @param handler - overrides default handler\n\t */\n\tpublic start(\n\t\tms: number = this.defaultTimeout,\n\t\thandler: () => void = this.defaultHandler,\n\t): void {\n\t\tthis.startCore(ms, handler, ms);\n\t}\n\n\t/**\n\t * Calls clearTimeout on the underlying timeout if running.\n\t */\n\tpublic clear(): void {\n\t\tif (!this.runningState) {\n\t\t\treturn;\n\t\t}\n\t\tclearTimeout(this.runningState.timeout);\n\t\tthis.runningState = undefined;\n\t}\n\n\t/**\n\t * Restarts the timer with the new handler and duration.\n\t * If a new handler is passed, the original handler may\n\t * never execute.\n\t * This is a potentially more efficient way to clear and start\n\t * a new timer.\n\t * @param ms - overrides previous or default timeout in ms\n\t * @param handler - overrides previous or default handler\n\t */\n\tpublic restart(ms?: number, handler?: () => void): void {\n\t\tif (this.runningState) {\n\t\t\tconst duration = ms ?? this.runningState.intendedDuration;\n\t\t\tconst handlerToUse =\n\t\t\t\thandler ?? this.runningState.restart?.handler ?? this.runningState.handler;\n\t\t\tconst remainingTime = this.calculateRemainingTime(this.runningState);\n\n\t\t\tif (duration < remainingTime) {\n\t\t\t\t// If remaining time exceeds restart duration, do a hard restart.\n\t\t\t\t// The existing timeout time is too long.\n\t\t\t\tthis.start(duration, handlerToUse);\n\t\t\t} else if (duration === remainingTime) {\n\t\t\t\t// The existing timeout time is perfect, just update handler and data.\n\t\t\t\tthis.runningState.handler = handlerToUse;\n\t\t\t\tthis.runningState.restart = undefined;\n\t\t\t\tthis.runningState.intendedDuration = duration;\n\t\t\t} else {\n\t\t\t\t// If restart duration exceeds remaining time, set restart info.\n\t\t\t\t// Existing timeout will start a new timeout for remaining time.\n\t\t\t\tthis.runningState.restart = {\n\t\t\t\t\tstartTick: this.getCurrentTick(),\n\t\t\t\t\tduration,\n\t\t\t\t\thandler: handlerToUse,\n\t\t\t\t};\n\t\t\t}\n\t\t} else {\n\t\t\t// If restart is called first, it behaves as a call to start\n\t\t\tthis.start(ms, handler);\n\t\t}\n\t}\n\n\tprivate startCore(duration: number, handler: () => void, intendedDuration: number): void {\n\t\tthis.clear();\n\t\tthis.runningState = {\n\t\t\tstartTick: this.getCurrentTick(),\n\t\t\tduration,\n\t\t\tintendedDuration,\n\t\t\thandler,\n\t\t\ttimeout: setLongTimeout(\n\t\t\t\t() => this.handler(),\n\t\t\t\tduration,\n\t\t\t\t(timer: number) => {\n\t\t\t\t\tif (this.runningState !== undefined) {\n\t\t\t\t\t\tthis.runningState.timeout = timer;\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t};\n\t}\n\n\tprivate handler(): void {\n\t\tassert(!!this.runningState, 0x764 /* Running timer missing handler */);\n\t\tconst restart = this.runningState.restart;\n\t\tif (restart === undefined) {\n\t\t\t// Run clear first, in case the handler decides to start again\n\t\t\tconst handler = this.runningState.handler;\n\t\t\tthis.clear();\n\t\t\thandler();\n\t\t} else {\n\t\t\t// Restart with remaining time\n\t\t\tconst remainingTime = this.calculateRemainingTime(restart);\n\t\t\tthis.startCore(remainingTime, () => restart.handler(), restart.duration);\n\t\t}\n\t}\n\n\tprivate calculateRemainingTime(runningTimeout: ITimeout): number {\n\t\tconst elapsedTime = this.getCurrentTick() - runningTimeout.startTick;\n\t\treturn runningTimeout.duration - elapsedTime;\n\t}\n}\n\nexport interface IPromiseTimerResult {\n\ttimerResult: \"timeout\" | \"cancel\";\n}\n\n/**\n * Timer which offers a promise that fulfills when the timer\n * completes.\n */\nexport interface IPromiseTimer extends ITimer {\n\t/**\n\t * Starts the timer and returns a promise that\n\t * resolves when the timer times out or is canceled.\n\t */\n\tstart(): Promise<IPromiseTimerResult>;\n}\n\n/**\n * This class is a wrapper over setTimeout and clearTimeout which\n * makes it simpler to keep track of recurring timeouts with the\n * same handlers and timeouts, while also providing a promise that\n * resolves when it times out.\n */\nexport class PromiseTimer implements IPromiseTimer {\n\tprivate deferred?: Deferred<IPromiseTimerResult>;\n\tprivate readonly timer: Timer;\n\n\t/**\n\t * {@inheritDoc Timer.hasTimer}\n\t */\n\tpublic get hasTimer(): boolean {\n\t\treturn this.timer.hasTimer;\n\t}\n\n\tpublic constructor(defaultTimeout: number, defaultHandler: () => void) {\n\t\tthis.timer = new Timer(defaultTimeout, () => this.wrapHandler(defaultHandler));\n\t}\n\n\t/**\n\t * {@inheritDoc IPromiseTimer.start}\n\t */\n\tpublic async start(ms?: number, handler?: () => void): Promise<IPromiseTimerResult> {\n\t\tthis.clear();\n\t\tthis.deferred = new Deferred<IPromiseTimerResult>();\n\t\tthis.timer.start(ms, handler ? (): void => this.wrapHandler(handler) : undefined);\n\t\treturn this.deferred.promise;\n\t}\n\n\tpublic clear(): void {\n\t\tthis.timer.clear();\n\t\tif (this.deferred) {\n\t\t\tthis.deferred.resolve({ timerResult: \"cancel\" });\n\t\t\tthis.deferred = undefined;\n\t\t}\n\t}\n\n\tprotected wrapHandler(handler: () => void): void {\n\t\thandler();\n\t\tassert(!!this.deferred, 0x765 /* Handler executed without deferred */);\n\t\tthis.deferred.resolve({ timerResult: \"timeout\" });\n\t\tthis.deferred = undefined;\n\t}\n}\n"]}
|
package/dist/tsdoc-metadata.json
CHANGED
package/lib/lazy.js
CHANGED
|
@@ -39,12 +39,12 @@ export class Lazy {
|
|
|
39
39
|
* All calls are then proxied to the promise returned by the execute method.
|
|
40
40
|
*/
|
|
41
41
|
export class LazyPromise {
|
|
42
|
-
constructor(execute) {
|
|
43
|
-
this.execute = execute;
|
|
44
|
-
}
|
|
45
42
|
get [Symbol.toStringTag]() {
|
|
46
43
|
return this.getPromise()[Symbol.toStringTag];
|
|
47
44
|
}
|
|
45
|
+
constructor(execute) {
|
|
46
|
+
this.execute = execute;
|
|
47
|
+
}
|
|
48
48
|
// eslint-disable-next-line unicorn/no-thenable
|
|
49
49
|
async then(
|
|
50
50
|
// eslint-disable-next-line @rushstack/no-new-null
|
package/lib/lazy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lazy.js","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,OAAO,IAAI;IAGhB;;;OAGG;IACH,YAAoC,cAAuB;QAAvB,mBAAc,GAAd,cAAc,CAAS;QALnD,eAAU,GAAY,KAAK,CAAC;IAK0B,CAAC;IAE/D;;OAEG;IACH,IAAW,SAAS;QACnB,OAAO,IAAI,CAAC,UAAU,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,IAAW,KAAK;QACf,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACrB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;YACvB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,cAAc,EAAE,CAAC;SACpC;QACD,oEAAoE;QACpE,OAAO,IAAI,CAAC,MAAO,CAAC;IACrB,CAAC;CACD;AAED;;;;;GAKG;AACH,MAAM,OAAO,WAAW;
|
|
1
|
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8CAA8C;QAC9C,OAAO,IAAI,CAAC,UAAU,EAAE,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC,CAAC;IAChD,CAAC;IAEO,KAAK,CAAC,UAAU;QACvB,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,EAAE;YAC9B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;SAC7B;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACpB,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Helper class for lazy initialized values. Ensures the value is only generated once, and remain immutable.\n */\nexport class Lazy<T> {\n\tprivate _value: T | undefined;\n\tprivate _evaluated: boolean = false;\n\t/**\n\t * Instantiates an instance of Lazy<T>.\n\t * @param valueGenerator - The function that will generate the value when value is accessed the first time.\n\t */\n\tpublic constructor(private readonly valueGenerator: () => T) {}\n\n\t/**\n\t * Return true if the value as been generated, otherwise false.\n\t */\n\tpublic get evaluated(): boolean {\n\t\treturn this._evaluated;\n\t}\n\n\t/**\n\t * Get the value. If this is the first call the value will be generated.\n\t */\n\tpublic get value(): T {\n\t\tif (!this._evaluated) {\n\t\t\tthis._evaluated = true;\n\t\t\tthis._value = this.valueGenerator();\n\t\t}\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\treturn this._value!;\n\t}\n}\n\n/**\n * A lazy evaluated promise. The execute function is delayed until\n * the promise is used, e.g. await, then, catch ...\n * The execute function is only called once.\n * All calls are then proxied to the promise returned by the execute method.\n */\nexport class LazyPromise<T> implements Promise<T> {\n\tpublic get [Symbol.toStringTag](): string {\n\t\treturn this.getPromise()[Symbol.toStringTag];\n\t}\n\n\tprivate result: Promise<T> | undefined;\n\n\tpublic constructor(private readonly execute: () => Promise<T>) {}\n\n\t// eslint-disable-next-line unicorn/no-thenable\n\tpublic async then<TResult1 = T, TResult2 = never>(\n\t\t// eslint-disable-next-line @rushstack/no-new-null\n\t\tonfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,\n\t\t// TODO: Use `unknown` instead (API breaking)\n\t\t// eslint-disable-next-line @rushstack/no-new-null, @typescript-eslint/no-explicit-any\n\t\tonrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined,\n\t): Promise<TResult1 | TResult2> {\n\t\t// eslint-disable-next-line prefer-rest-params\n\t\treturn this.getPromise().then<TResult1, TResult2>(...arguments);\n\t}\n\n\tpublic async catch<TResult = never>(\n\t\t// TODO: Use `unknown` instead (API breaking)\n\t\t// eslint-disable-next-line @rushstack/no-new-null, @typescript-eslint/no-explicit-any\n\t\tonrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined,\n\t): Promise<T | TResult> {\n\t\t// eslint-disable-next-line prefer-rest-params\n\t\treturn this.getPromise().catch<TResult>(...arguments);\n\t}\n\n\t// eslint-disable-next-line @rushstack/no-new-null\n\tpublic async finally(onfinally?: (() => void) | null | undefined): Promise<T> {\n\t\t// eslint-disable-next-line prefer-rest-params\n\t\treturn this.getPromise().finally(...arguments);\n\t}\n\n\tprivate async getPromise(): Promise<T> {\n\t\tif (this.result === undefined) {\n\t\t\tthis.result = this.execute();\n\t\t}\n\t\treturn this.result;\n\t}\n}\n"]}
|
package/lib/promiseCache.d.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - absolute: entries expire after the given duration in MS, even if accessed multiple times in the mean time
|
|
9
9
|
* - sliding: entries expire after the given duration in MS of inactivity (i.e. get resets the clock)
|
|
10
10
|
*/
|
|
11
|
-
export
|
|
11
|
+
export type PromiseCacheExpiry = {
|
|
12
12
|
policy: "indefinite";
|
|
13
13
|
} | {
|
|
14
14
|
policy: "absolute" | "sliding";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promiseCache.d.ts","sourceRoot":"","sources":["../src/promiseCache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAK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