@fluidframework/core-utils 2.0.0-dev.6.4.0.191515 → 2.0.0-dev.7.2.0.203917

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.
Files changed (85) hide show
  1. package/.eslintrc.js +13 -3
  2. package/CHANGELOG.md +16 -0
  3. package/api-extractor.json +1 -1
  4. package/api-report/core-utils.api.md +147 -0
  5. package/dist/assert.d.ts +1 -0
  6. package/dist/assert.d.ts.map +1 -1
  7. package/dist/assert.js +1 -0
  8. package/dist/assert.js.map +1 -1
  9. package/dist/compare.js.map +1 -1
  10. package/dist/core-utils-alpha.d.ts +404 -0
  11. package/dist/core-utils-beta.d.ts +404 -0
  12. package/dist/core-utils-public.d.ts +404 -0
  13. package/dist/core-utils-untrimmed.d.ts +414 -0
  14. package/dist/delay.d.ts +1 -0
  15. package/dist/delay.d.ts.map +1 -1
  16. package/dist/delay.js +1 -0
  17. package/dist/delay.js.map +1 -1
  18. package/dist/heap.d.ts +4 -0
  19. package/dist/heap.d.ts.map +1 -1
  20. package/dist/heap.js +3 -1
  21. package/dist/heap.js.map +1 -1
  22. package/dist/lazy.d.ts +2 -0
  23. package/dist/lazy.d.ts.map +1 -1
  24. package/dist/lazy.js +10 -5
  25. package/dist/lazy.js.map +1 -1
  26. package/dist/promiseCache.d.ts +11 -4
  27. package/dist/promiseCache.d.ts.map +1 -1
  28. package/dist/promiseCache.js +1 -0
  29. package/dist/promiseCache.js.map +1 -1
  30. package/dist/promises.d.ts +1 -0
  31. package/dist/promises.d.ts.map +1 -1
  32. package/dist/promises.js +3 -0
  33. package/dist/promises.js.map +1 -1
  34. package/dist/timer.d.ts +10 -0
  35. package/dist/timer.d.ts.map +1 -1
  36. package/dist/timer.js +22 -19
  37. package/dist/timer.js.map +1 -1
  38. package/dist/tsdoc-metadata.json +1 -1
  39. package/dist/unreachable.d.ts +1 -0
  40. package/dist/unreachable.d.ts.map +1 -1
  41. package/dist/unreachable.js +1 -0
  42. package/dist/unreachable.js.map +1 -1
  43. package/lib/assert.d.ts +1 -0
  44. package/lib/assert.d.ts.map +1 -1
  45. package/lib/assert.js +1 -0
  46. package/lib/assert.js.map +1 -1
  47. package/lib/compare.js.map +1 -1
  48. package/lib/delay.d.ts +1 -0
  49. package/lib/delay.d.ts.map +1 -1
  50. package/lib/delay.js +1 -0
  51. package/lib/delay.js.map +1 -1
  52. package/lib/heap.d.ts +4 -0
  53. package/lib/heap.d.ts.map +1 -1
  54. package/lib/heap.js +3 -1
  55. package/lib/heap.js.map +1 -1
  56. package/lib/lazy.d.ts +2 -0
  57. package/lib/lazy.d.ts.map +1 -1
  58. package/lib/lazy.js +10 -5
  59. package/lib/lazy.js.map +1 -1
  60. package/lib/promiseCache.d.ts +11 -4
  61. package/lib/promiseCache.d.ts.map +1 -1
  62. package/lib/promiseCache.js +1 -0
  63. package/lib/promiseCache.js.map +1 -1
  64. package/lib/promises.d.ts +1 -0
  65. package/lib/promises.d.ts.map +1 -1
  66. package/lib/promises.js +3 -0
  67. package/lib/promises.js.map +1 -1
  68. package/lib/timer.d.ts +10 -0
  69. package/lib/timer.d.ts.map +1 -1
  70. package/lib/timer.js +22 -19
  71. package/lib/timer.js.map +1 -1
  72. package/lib/unreachable.d.ts +1 -0
  73. package/lib/unreachable.d.ts.map +1 -1
  74. package/lib/unreachable.js +1 -0
  75. package/lib/unreachable.js.map +1 -1
  76. package/package.json +13 -14
  77. package/src/assert.ts +1 -0
  78. package/src/compare.ts +1 -1
  79. package/src/delay.ts +1 -0
  80. package/src/heap.ts +5 -1
  81. package/src/lazy.ts +9 -4
  82. package/src/promiseCache.ts +15 -6
  83. package/src/promises.ts +5 -2
  84. package/src/timer.ts +21 -11
  85. package/src/unreachable.ts +1 -0
package/src/lazy.ts CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
  /**
7
7
  * Helper class for lazy initialized values. Ensures the value is only generated once, and remain immutable.
8
+ * @public
8
9
  */
9
10
  export class Lazy<T> {
10
11
  private _value: T | undefined;
@@ -13,7 +14,7 @@ export class Lazy<T> {
13
14
  * Instantiates an instance of Lazy<T>.
14
15
  * @param valueGenerator - The function that will generate the value when value is accessed the first time.
15
16
  */
16
- constructor(private readonly valueGenerator: () => T) {}
17
+ public constructor(private readonly valueGenerator: () => T) {}
17
18
 
18
19
  /**
19
20
  * Return true if the value as been generated, otherwise false.
@@ -40,6 +41,7 @@ export class Lazy<T> {
40
41
  * the promise is used, e.g. await, then, catch ...
41
42
  * The execute function is only called once.
42
43
  * All calls are then proxied to the promise returned by the execute method.
44
+ * @public
43
45
  */
44
46
  export class LazyPromise<T> implements Promise<T> {
45
47
  public get [Symbol.toStringTag](): string {
@@ -48,12 +50,14 @@ export class LazyPromise<T> implements Promise<T> {
48
50
 
49
51
  private result: Promise<T> | undefined;
50
52
 
51
- constructor(private readonly execute: () => Promise<T>) {}
53
+ public constructor(private readonly execute: () => Promise<T>) {}
52
54
 
55
+ // eslint-disable-next-line unicorn/no-thenable
53
56
  public async then<TResult1 = T, TResult2 = never>(
54
57
  // eslint-disable-next-line @rushstack/no-new-null
55
58
  onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,
56
- // eslint-disable-next-line @rushstack/no-new-null
59
+ // TODO: Use `unknown` instead (API breaking)
60
+ // eslint-disable-next-line @rushstack/no-new-null, @typescript-eslint/no-explicit-any
57
61
  onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined,
58
62
  ): Promise<TResult1 | TResult2> {
59
63
  // eslint-disable-next-line prefer-rest-params
@@ -61,7 +65,8 @@ export class LazyPromise<T> implements Promise<T> {
61
65
  }
62
66
 
63
67
  public async catch<TResult = never>(
64
- // eslint-disable-next-line @rushstack/no-new-null
68
+ // TODO: Use `unknown` instead (API breaking)
69
+ // eslint-disable-next-line @rushstack/no-new-null, @typescript-eslint/no-explicit-any
65
70
  onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined,
66
71
  ): Promise<T | TResult> {
67
72
  // eslint-disable-next-line prefer-rest-params
@@ -8,6 +8,7 @@
8
8
  * - indefinite: entries don't expire and must be explicitly removed
9
9
  * - absolute: entries expire after the given duration in MS, even if accessed multiple times in the mean time
10
10
  * - sliding: entries expire after the given duration in MS of inactivity (i.e. get resets the clock)
11
+ * @public
11
12
  */
12
13
  export type PromiseCacheExpiry =
13
14
  | {
@@ -20,12 +21,19 @@ export type PromiseCacheExpiry =
20
21
 
21
22
  /**
22
23
  * Options for configuring the {@link PromiseCache}
24
+ * @public
23
25
  */
24
26
  export interface PromiseCacheOptions {
25
- /** Common expiration policy for all items added to this cache */
27
+ /**
28
+ * Common expiration policy for all items added to this cache
29
+ */
26
30
  expiry?: PromiseCacheExpiry;
27
- /** If the stored Promise is rejected with a particular error, should the given key be removed? */
28
- removeOnError?: (e: any) => boolean;
31
+ /**
32
+ * If the stored Promise is rejected with a particular error, should the given key be removed?
33
+ */
34
+ // TODO: Use `unknown` instead (API breaking)
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
+ removeOnError?: (error: any) => boolean;
29
37
  }
30
38
 
31
39
  /**
@@ -35,7 +43,7 @@ export interface PromiseCacheOptions {
35
43
  class GarbageCollector<TKey> {
36
44
  private readonly gcTimeouts = new Map<TKey, ReturnType<typeof setTimeout>>();
37
45
 
38
- constructor(
46
+ public constructor(
39
47
  private readonly expiry: PromiseCacheExpiry,
40
48
  private readonly cleanup: (key: TKey) => void,
41
49
  ) {}
@@ -81,19 +89,20 @@ class GarbageCollector<TKey> {
81
89
  /**
82
90
  * A specialized cache for async work, allowing you to safely cache the promised result of some async work
83
91
  * without fear of running it multiple times or losing track of errors.
92
+ * @public
84
93
  */
85
94
  export class PromiseCache<TKey, TResult> {
86
95
  private readonly cache = new Map<TKey, Promise<TResult>>();
87
96
  private readonly gc: GarbageCollector<TKey>;
88
97
 
89
- private readonly removeOnError: (error: any) => boolean;
98
+ private readonly removeOnError: (error: unknown) => boolean;
90
99
 
91
100
  /**
92
101
  * Create the PromiseCache with the given options, with the following defaults:
93
102
  *
94
103
  * expiry: indefinite, removeOnError: true for all errors
95
104
  */
96
- constructor({
105
+ public constructor({
97
106
  expiry = { policy: "indefinite" },
98
107
  removeOnError = (): boolean => true,
99
108
  }: PromiseCacheOptions = {}) {
package/src/promises.ts CHANGED
@@ -5,14 +5,15 @@
5
5
 
6
6
  /**
7
7
  * A deferred creates a promise and the ability to resolve or reject it
8
+ * @public
8
9
  */
9
10
  export class Deferred<T> {
10
11
  private readonly p: Promise<T>;
11
12
  private res: ((value: T | PromiseLike<T>) => void) | undefined;
12
- private rej: ((reason?: any) => void) | undefined;
13
+ private rej: ((reason?: unknown) => void) | undefined;
13
14
  private completed: boolean = false;
14
15
 
15
- constructor() {
16
+ public constructor() {
16
17
  this.p = new Promise<T>((resolve, reject) => {
17
18
  this.res = resolve;
18
19
  this.rej = reject;
@@ -51,6 +52,8 @@ export class Deferred<T> {
51
52
  *
52
53
  * @param value - the value to reject the promise with
53
54
  */
55
+ // TODO: Use `unknown` instead (API breaking)
56
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
54
57
  public reject(error: any): void {
55
58
  if (this.rej !== undefined) {
56
59
  this.completed = true;
package/src/timer.ts CHANGED
@@ -6,6 +6,9 @@
6
6
  import { assert } from "./assert";
7
7
  import { Deferred } from "./promises";
8
8
 
9
+ /**
10
+ * @public
11
+ */
9
12
  export interface ITimer {
10
13
  /**
11
14
  * True if timer is currently running
@@ -68,6 +71,7 @@ const maxSetTimeoutMs = 0x7fffffff; // setTimeout limit is MAX_INT32=(2^31-1).
68
71
  * @param setTimeoutIdFn - Executed to update the timeout if multiple timeouts are required when
69
72
  * timeoutMs greater than maxTimeout
70
73
  * @returns The initial timeout
74
+ * @public
71
75
  */
72
76
  export function setLongTimeout(
73
77
  timeoutFn: () => void,
@@ -95,6 +99,7 @@ export function setLongTimeout(
95
99
  * makes it simpler to keep track of recurring timeouts with the same
96
100
  * or similar handlers and timeouts. This class supports long timeouts
97
101
  * or timeouts exceeding (2^31)-1 ms or approximately 24.8 days.
102
+ * @public
98
103
  */
99
104
  export class Timer implements ITimer {
100
105
  /**
@@ -106,7 +111,7 @@ export class Timer implements ITimer {
106
111
 
107
112
  private runningState: IRunningTimerState | undefined;
108
113
 
109
- constructor(
114
+ public constructor(
110
115
  private readonly defaultTimeout: number,
111
116
  private readonly defaultHandler: () => void,
112
117
  private readonly getCurrentTick: () => number = (): number => Date.now(),
@@ -145,10 +150,7 @@ export class Timer implements ITimer {
145
150
  * @param handler - overrides previous or default handler
146
151
  */
147
152
  public restart(ms?: number, handler?: () => void): void {
148
- if (!this.runningState) {
149
- // If restart is called first, it behaves as a call to start
150
- this.start(ms, handler);
151
- } else {
153
+ if (this.runningState) {
152
154
  const duration = ms ?? this.runningState.intendedDuration;
153
155
  const handlerToUse =
154
156
  handler ?? this.runningState.restart?.handler ?? this.runningState.handler;
@@ -172,6 +174,9 @@ export class Timer implements ITimer {
172
174
  handler: handlerToUse,
173
175
  };
174
176
  }
177
+ } else {
178
+ // If restart is called first, it behaves as a call to start
179
+ this.start(ms, handler);
175
180
  }
176
181
  }
177
182
 
@@ -197,15 +202,15 @@ export class Timer implements ITimer {
197
202
  private handler(): void {
198
203
  assert(!!this.runningState, 0x764 /* Running timer missing handler */);
199
204
  const restart = this.runningState.restart;
200
- if (restart !== undefined) {
201
- // Restart with remaining time
202
- const remainingTime = this.calculateRemainingTime(restart);
203
- this.startCore(remainingTime, () => restart.handler(), restart.duration);
204
- } else {
205
+ if (restart === undefined) {
205
206
  // Run clear first, in case the handler decides to start again
206
207
  const handler = this.runningState.handler;
207
208
  this.clear();
208
209
  handler();
210
+ } else {
211
+ // Restart with remaining time
212
+ const remainingTime = this.calculateRemainingTime(restart);
213
+ this.startCore(remainingTime, () => restart.handler(), restart.duration);
209
214
  }
210
215
  }
211
216
 
@@ -215,6 +220,9 @@ export class Timer implements ITimer {
215
220
  }
216
221
  }
217
222
 
223
+ /**
224
+ * @public
225
+ */
218
226
  export interface IPromiseTimerResult {
219
227
  timerResult: "timeout" | "cancel";
220
228
  }
@@ -222,6 +230,7 @@ export interface IPromiseTimerResult {
222
230
  /**
223
231
  * Timer which offers a promise that fulfills when the timer
224
232
  * completes.
233
+ * @public
225
234
  */
226
235
  export interface IPromiseTimer extends ITimer {
227
236
  /**
@@ -236,6 +245,7 @@ export interface IPromiseTimer extends ITimer {
236
245
  * makes it simpler to keep track of recurring timeouts with the
237
246
  * same handlers and timeouts, while also providing a promise that
238
247
  * resolves when it times out.
248
+ * @public
239
249
  */
240
250
  export class PromiseTimer implements IPromiseTimer {
241
251
  private deferred?: Deferred<IPromiseTimerResult>;
@@ -248,7 +258,7 @@ export class PromiseTimer implements IPromiseTimer {
248
258
  return this.timer.hasTimer;
249
259
  }
250
260
 
251
- constructor(defaultTimeout: number, defaultHandler: () => void) {
261
+ public constructor(defaultTimeout: number, defaultHandler: () => void) {
252
262
  this.timer = new Timer(defaultTimeout, () => this.wrapHandler(defaultHandler));
253
263
  }
254
264
 
@@ -17,6 +17,7 @@
17
17
  * default: unreachableCase(bool);
18
18
  * }
19
19
  * ```
20
+ * @public
20
21
  */
21
22
  export function unreachableCase(_: never, message = "Unreachable Case"): never {
22
23
  throw new Error(message);