@fluidframework/core-utils 2.0.0-dev-rc.1.0.0.224419

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 (111) hide show
  1. package/.eslintrc.js +36 -0
  2. package/.mocharc.js +13 -0
  3. package/CHANGELOG.md +81 -0
  4. package/LICENSE +21 -0
  5. package/README.md +75 -0
  6. package/api-extractor-lint.json +4 -0
  7. package/api-extractor.json +4 -0
  8. package/api-report/core-utils.api.md +147 -0
  9. package/dist/assert.d.ts +17 -0
  10. package/dist/assert.d.ts.map +1 -0
  11. package/dist/assert.js +25 -0
  12. package/dist/assert.js.map +1 -0
  13. package/dist/compare.d.ts +16 -0
  14. package/dist/compare.d.ts.map +1 -0
  15. package/dist/compare.js +28 -0
  16. package/dist/compare.js.map +1 -0
  17. package/dist/core-utils-alpha.d.ts +191 -0
  18. package/dist/core-utils-beta.d.ts +41 -0
  19. package/dist/core-utils-public.d.ts +41 -0
  20. package/dist/core-utils-untrimmed.d.ts +414 -0
  21. package/dist/delay.d.ts +11 -0
  22. package/dist/delay.d.ts.map +1 -0
  23. package/dist/delay.js +15 -0
  24. package/dist/delay.js.map +1 -0
  25. package/dist/heap.d.ts +86 -0
  26. package/dist/heap.d.ts.map +1 -0
  27. package/dist/heap.js +144 -0
  28. package/dist/heap.js.map +1 -0
  29. package/dist/index.d.ts +14 -0
  30. package/dist/index.d.ts.map +1 -0
  31. package/dist/index.js +30 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/lazy.d.ts +44 -0
  34. package/dist/lazy.d.ts.map +1 -0
  35. package/dist/lazy.js +84 -0
  36. package/dist/lazy.js.map +1 -0
  37. package/dist/promiseCache.d.ts +89 -0
  38. package/dist/promiseCache.d.ts.map +1 -0
  39. package/dist/promiseCache.js +148 -0
  40. package/dist/promiseCache.js.map +1 -0
  41. package/dist/promises.d.ts +38 -0
  42. package/dist/promises.d.ts.map +1 -0
  43. package/dist/promises.js +60 -0
  44. package/dist/promises.js.map +1 -0
  45. package/dist/timer.d.ts +115 -0
  46. package/dist/timer.d.ts.map +1 -0
  47. package/dist/timer.js +189 -0
  48. package/dist/timer.js.map +1 -0
  49. package/dist/tsdoc-metadata.json +11 -0
  50. package/dist/unreachable.d.ts +22 -0
  51. package/dist/unreachable.d.ts.map +1 -0
  52. package/dist/unreachable.js +28 -0
  53. package/dist/unreachable.js.map +1 -0
  54. package/lib/assert.d.ts +17 -0
  55. package/lib/assert.d.ts.map +1 -0
  56. package/lib/assert.js +25 -0
  57. package/lib/assert.js.map +1 -0
  58. package/lib/compare.d.ts +16 -0
  59. package/lib/compare.d.ts.map +1 -0
  60. package/lib/compare.js +28 -0
  61. package/lib/compare.js.map +1 -0
  62. package/lib/core-utils-alpha.d.ts +191 -0
  63. package/lib/core-utils-beta.d.ts +41 -0
  64. package/lib/core-utils-public.d.ts +41 -0
  65. package/lib/core-utils-untrimmed.d.ts +414 -0
  66. package/lib/delay.d.ts +11 -0
  67. package/lib/delay.d.ts.map +1 -0
  68. package/lib/delay.js +15 -0
  69. package/lib/delay.js.map +1 -0
  70. package/lib/heap.d.ts +86 -0
  71. package/lib/heap.d.ts.map +1 -0
  72. package/lib/heap.js +144 -0
  73. package/lib/heap.js.map +1 -0
  74. package/lib/index.d.ts +14 -0
  75. package/lib/index.d.ts.map +1 -0
  76. package/lib/index.js +30 -0
  77. package/lib/index.js.map +1 -0
  78. package/lib/lazy.d.ts +44 -0
  79. package/lib/lazy.d.ts.map +1 -0
  80. package/lib/lazy.js +84 -0
  81. package/lib/lazy.js.map +1 -0
  82. package/lib/promiseCache.d.ts +89 -0
  83. package/lib/promiseCache.d.ts.map +1 -0
  84. package/lib/promiseCache.js +148 -0
  85. package/lib/promiseCache.js.map +1 -0
  86. package/lib/promises.d.ts +38 -0
  87. package/lib/promises.d.ts.map +1 -0
  88. package/lib/promises.js +60 -0
  89. package/lib/promises.js.map +1 -0
  90. package/lib/timer.d.ts +115 -0
  91. package/lib/timer.d.ts.map +1 -0
  92. package/lib/timer.js +189 -0
  93. package/lib/timer.js.map +1 -0
  94. package/lib/unreachable.d.ts +22 -0
  95. package/lib/unreachable.d.ts.map +1 -0
  96. package/lib/unreachable.js +28 -0
  97. package/lib/unreachable.js.map +1 -0
  98. package/package.json +111 -0
  99. package/prettier.config.cjs +8 -0
  100. package/src/assert.ts +23 -0
  101. package/src/compare.ts +33 -0
  102. package/src/delay.ts +12 -0
  103. package/src/heap.ts +182 -0
  104. package/src/index.ts +21 -0
  105. package/src/lazy.ts +88 -0
  106. package/src/promiseCache.ts +205 -0
  107. package/src/promises.ts +63 -0
  108. package/src/timer.ts +289 -0
  109. package/src/unreachable.ts +24 -0
  110. package/tsconfig.esnext.json +6 -0
  111. package/tsconfig.json +12 -0
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.PromiseCache = void 0;
8
+ /**
9
+ * Handles garbage collection of expiring cache entries.
10
+ * Not exported.
11
+ */
12
+ class GarbageCollector {
13
+ constructor(expiry, cleanup) {
14
+ this.expiry = expiry;
15
+ this.cleanup = cleanup;
16
+ this.gcTimeouts = new Map();
17
+ }
18
+ /**
19
+ * Schedule GC for the given key, as applicable
20
+ */
21
+ schedule(key) {
22
+ if (this.expiry.policy !== "indefinite") {
23
+ this.gcTimeouts.set(key, setTimeout(() => {
24
+ this.cleanup(key);
25
+ this.cancel(key);
26
+ }, this.expiry.durationMs));
27
+ }
28
+ }
29
+ /**
30
+ * Cancel any pending GC for the given key
31
+ */
32
+ cancel(key) {
33
+ const timeout = this.gcTimeouts.get(key);
34
+ if (timeout !== undefined) {
35
+ clearTimeout(timeout);
36
+ this.gcTimeouts.delete(key);
37
+ }
38
+ }
39
+ /**
40
+ * Update any pending GC for the given key, as applicable
41
+ */
42
+ update(key) {
43
+ // Cancel/reschedule new GC if the policy is sliding
44
+ if (this.expiry.policy === "sliding") {
45
+ this.cancel(key);
46
+ this.schedule(key);
47
+ }
48
+ }
49
+ }
50
+ /**
51
+ * A specialized cache for async work, allowing you to safely cache the promised result of some async work
52
+ * without fear of running it multiple times or losing track of errors.
53
+ * @alpha
54
+ */
55
+ class PromiseCache {
56
+ /**
57
+ * Create the PromiseCache with the given options, with the following defaults:
58
+ *
59
+ * expiry: indefinite, removeOnError: true for all errors
60
+ */
61
+ constructor({ expiry = { policy: "indefinite" }, removeOnError = () => true, } = {}) {
62
+ this.cache = new Map();
63
+ this.removeOnError = removeOnError;
64
+ this.gc = new GarbageCollector(expiry, (key) => this.remove(key));
65
+ }
66
+ /**
67
+ * Check if there's anything cached at the given key
68
+ */
69
+ has(key) {
70
+ return this.cache.has(key);
71
+ }
72
+ /**
73
+ * Get the Promise for the given key, or undefined if it's not found.
74
+ * Extend expiry if applicable.
75
+ */
76
+ get(key) {
77
+ if (this.has(key)) {
78
+ this.gc.update(key);
79
+ }
80
+ return this.cache.get(key);
81
+ }
82
+ /**
83
+ * Remove the Promise for the given key, returning true if it was found and removed
84
+ */
85
+ remove(key) {
86
+ this.gc.cancel(key);
87
+ return this.cache.delete(key);
88
+ }
89
+ /**
90
+ * Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
91
+ * Returns a Promise for the added or existing async work being done at that key.
92
+ * @param key - key name where to store the async work
93
+ * @param asyncFn - the async work to do and store, if not already in progress under the given key
94
+ */
95
+ async addOrGet(key, asyncFn) {
96
+ // NOTE: Do not await the Promise returned by asyncFn!
97
+ // Let the caller do so once we return or after a subsequent call to get
98
+ let promise = this.get(key);
99
+ if (promise === undefined) {
100
+ // Wrap in an async lambda in case asyncFn disabled @typescript-eslint/promise-function-async
101
+ const safeAsyncFn = async () => asyncFn();
102
+ // Start the async work and put the Promise in the cache
103
+ promise = safeAsyncFn();
104
+ this.cache.set(key, promise);
105
+ // If asyncFn throws, we may remove the Promise from the cache
106
+ promise.catch((error) => {
107
+ if (this.removeOnError(error)) {
108
+ this.remove(key);
109
+ }
110
+ });
111
+ this.gc.schedule(key);
112
+ }
113
+ return promise;
114
+ }
115
+ /**
116
+ * Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
117
+ * Returns false if the cache already contained an entry at that key, and true otherwise.
118
+ * @param key - key name where to store the async work
119
+ * @param asyncFn - the async work to do and store, if not already in progress under the given key
120
+ */
121
+ add(key, asyncFn) {
122
+ const alreadyPresent = this.has(key);
123
+ // We are blindly adding the Promise to the cache here, which introduces a Promise in this scope.
124
+ // Swallow Promise rejections here, since whoever gets this out of the cache to use it will await/catch.
125
+ this.addOrGet(key, asyncFn).catch(() => { });
126
+ return !alreadyPresent;
127
+ }
128
+ /**
129
+ * Try to add the given value, without overwriting an existing cache entry at that key.
130
+ * Returns a Promise for the added or existing async work being done at that key.
131
+ * @param key - key name where to store the async work
132
+ * @param value - value to store
133
+ */
134
+ async addValueOrGet(key, value) {
135
+ return this.addOrGet(key, async () => value);
136
+ }
137
+ /**
138
+ * Try to add the given value, without overwriting an existing cache entry at that key.
139
+ * Returns false if the cache already contained an entry at that key, and true otherwise.
140
+ * @param key - key name where to store the value
141
+ * @param value - value to store
142
+ */
143
+ addValue(key, value) {
144
+ return this.add(key, async () => value);
145
+ }
146
+ }
147
+ exports.PromiseCache = PromiseCache;
148
+ //# sourceMappingURL=promiseCache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promiseCache.js","sourceRoot":"","sources":["../src/promiseCache.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAmCH;;;GAGG;AACH,MAAM,gBAAgB;IAGrB,YACkB,MAA0B,EAC1B,OAA4B;QAD5B,WAAM,GAAN,MAAM,CAAoB;QAC1B,YAAO,GAAP,OAAO,CAAqB;QAJ7B,eAAU,GAAG,IAAI,GAAG,EAAuC,CAAC;IAK1E,CAAC;IAEJ;;OAEG;IACI,QAAQ,CAAC,GAAS;QACxB,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,KAA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AAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAErC,iGAAiG;QACjG,wGAAwG;QACxG,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QAE5C,OAAO,CAAC,cAAc,CAAC;IACxB,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,aAAa,CAAC,GAAS,EAAE,KAAc;QACnD,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,KAAK,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC;IAC9C,CAAC;IAED;;;;;OAKG;IACI,QAAQ,CAAC,GAAS,EAAE,KAAc;QACxC,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC;IACzC,CAAC;CACD;AA/GD,oCA+GC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Three supported expiry policies:\n * - indefinite: entries don't expire and must be explicitly removed\n * - absolute: entries expire after the given duration in MS, even if accessed multiple times in the mean time\n * - sliding: entries expire after the given duration in MS of inactivity (i.e. get resets the clock)\n * @alpha\n */\nexport type PromiseCacheExpiry =\n\t| {\n\t\t\tpolicy: \"indefinite\";\n\t }\n\t| {\n\t\t\tpolicy: \"absolute\" | \"sliding\";\n\t\t\tdurationMs: number;\n\t };\n\n/**\n * Options for configuring the {@link PromiseCache}\n * @alpha\n */\nexport interface PromiseCacheOptions {\n\t/**\n\t * Common expiration policy for all items added to this cache\n\t */\n\texpiry?: PromiseCacheExpiry;\n\t/**\n\t * If the stored Promise is rejected with a particular error, should the given key be removed?\n\t */\n\t// TODO: Use `unknown` instead (API breaking)\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tremoveOnError?: (error: any) => boolean;\n}\n\n/**\n * Handles garbage collection of expiring cache entries.\n * Not exported.\n */\nclass GarbageCollector<TKey> {\n\tprivate readonly gcTimeouts = new Map<TKey, ReturnType<typeof setTimeout>>();\n\n\tpublic constructor(\n\t\tprivate readonly expiry: PromiseCacheExpiry,\n\t\tprivate readonly cleanup: (key: TKey) => void,\n\t) {}\n\n\t/**\n\t * Schedule GC for the given key, as applicable\n\t */\n\tpublic schedule(key: TKey): void {\n\t\tif (this.expiry.policy !== \"indefinite\") {\n\t\t\tthis.gcTimeouts.set(\n\t\t\t\tkey,\n\t\t\t\tsetTimeout(() => {\n\t\t\t\t\tthis.cleanup(key);\n\t\t\t\t\tthis.cancel(key);\n\t\t\t\t}, this.expiry.durationMs),\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Cancel any pending GC for the given key\n\t */\n\tpublic cancel(key: TKey): void {\n\t\tconst timeout = this.gcTimeouts.get(key);\n\t\tif (timeout !== undefined) {\n\t\t\tclearTimeout(timeout);\n\t\t\tthis.gcTimeouts.delete(key);\n\t\t}\n\t}\n\n\t/**\n\t * Update any pending GC for the given key, as applicable\n\t */\n\tpublic update(key: TKey): void {\n\t\t// Cancel/reschedule new GC if the policy is sliding\n\t\tif (this.expiry.policy === \"sliding\") {\n\t\t\tthis.cancel(key);\n\t\t\tthis.schedule(key);\n\t\t}\n\t}\n}\n\n/**\n * A specialized cache for async work, allowing you to safely cache the promised result of some async work\n * without fear of running it multiple times or losing track of errors.\n * @alpha\n */\nexport class PromiseCache<TKey, TResult> {\n\tprivate readonly cache = new Map<TKey, Promise<TResult>>();\n\tprivate readonly gc: GarbageCollector<TKey>;\n\n\tprivate readonly removeOnError: (error: unknown) => boolean;\n\n\t/**\n\t * Create the PromiseCache with the given options, with the following defaults:\n\t *\n\t * expiry: indefinite, removeOnError: true for all errors\n\t */\n\tpublic constructor({\n\t\texpiry = { policy: \"indefinite\" },\n\t\tremoveOnError = (): boolean => true,\n\t}: PromiseCacheOptions = {}) {\n\t\tthis.removeOnError = removeOnError;\n\t\tthis.gc = new GarbageCollector<TKey>(expiry, (key) => this.remove(key));\n\t}\n\n\t/**\n\t * Check if there's anything cached at the given key\n\t */\n\tpublic has(key: TKey): boolean {\n\t\treturn this.cache.has(key);\n\t}\n\n\t/**\n\t * Get the Promise for the given key, or undefined if it's not found.\n\t * Extend expiry if applicable.\n\t */\n\tpublic get(key: TKey): Promise<TResult> | undefined {\n\t\tif (this.has(key)) {\n\t\t\tthis.gc.update(key);\n\t\t}\n\t\treturn this.cache.get(key);\n\t}\n\n\t/**\n\t * Remove the Promise for the given key, returning true if it was found and removed\n\t */\n\tpublic remove(key: TKey): boolean {\n\t\tthis.gc.cancel(key);\n\t\treturn this.cache.delete(key);\n\t}\n\n\t/**\n\t * Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.\n\t * Returns a Promise for the added or existing async work being done at that key.\n\t * @param key - key name where to store the async work\n\t * @param asyncFn - the async work to do and store, if not already in progress under the given key\n\t */\n\tpublic async addOrGet(key: TKey, asyncFn: () => Promise<TResult>): Promise<TResult> {\n\t\t// NOTE: Do not await the Promise returned by asyncFn!\n\t\t// Let the caller do so once we return or after a subsequent call to get\n\t\tlet promise = this.get(key);\n\t\tif (promise === undefined) {\n\t\t\t// Wrap in an async lambda in case asyncFn disabled @typescript-eslint/promise-function-async\n\t\t\tconst safeAsyncFn = async (): Promise<TResult> => asyncFn();\n\n\t\t\t// Start the async work and put the Promise in the cache\n\t\t\tpromise = safeAsyncFn();\n\t\t\tthis.cache.set(key, promise);\n\n\t\t\t// If asyncFn throws, we may remove the Promise from the cache\n\t\t\tpromise.catch((error) => {\n\t\t\t\tif (this.removeOnError(error)) {\n\t\t\t\t\tthis.remove(key);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tthis.gc.schedule(key);\n\t\t}\n\n\t\treturn promise;\n\t}\n\n\t/**\n\t * Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.\n\t * Returns false if the cache already contained an entry at that key, and true otherwise.\n\t * @param key - key name where to store the async work\n\t * @param asyncFn - the async work to do and store, if not already in progress under the given key\n\t */\n\tpublic add(key: TKey, asyncFn: () => Promise<TResult>): boolean {\n\t\tconst alreadyPresent = this.has(key);\n\n\t\t// We are blindly adding the Promise to the cache here, which introduces a Promise in this scope.\n\t\t// Swallow Promise rejections here, since whoever gets this out of the cache to use it will await/catch.\n\t\tthis.addOrGet(key, asyncFn).catch(() => {});\n\n\t\treturn !alreadyPresent;\n\t}\n\n\t/**\n\t * Try to add the given value, without overwriting an existing cache entry at that key.\n\t * Returns a Promise for the added or existing async work being done at that key.\n\t * @param key - key name where to store the async work\n\t * @param value - value to store\n\t */\n\tpublic async addValueOrGet(key: TKey, value: TResult): Promise<TResult> {\n\t\treturn this.addOrGet(key, async () => value);\n\t}\n\n\t/**\n\t * Try to add the given value, without overwriting an existing cache entry at that key.\n\t * Returns false if the cache already contained an entry at that key, and true otherwise.\n\t * @param key - key name where to store the value\n\t * @param value - value to store\n\t */\n\tpublic addValue(key: TKey, value: TResult): boolean {\n\t\treturn this.add(key, async () => value);\n\t}\n}\n"]}
@@ -0,0 +1,38 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * A deferred creates a promise and the ability to resolve or reject it
7
+ * @alpha
8
+ */
9
+ export declare class Deferred<T> {
10
+ private readonly p;
11
+ private res;
12
+ private rej;
13
+ private completed;
14
+ constructor();
15
+ /**
16
+ * Returns whether the underlying promise has been completed
17
+ */
18
+ get isCompleted(): boolean;
19
+ /**
20
+ * Retrieves the underlying promise for the deferred
21
+ *
22
+ * @returns the underlying promise
23
+ */
24
+ get promise(): Promise<T>;
25
+ /**
26
+ * Resolves the promise
27
+ *
28
+ * @param value - the value to resolve the promise with
29
+ */
30
+ resolve(value: T | PromiseLike<T>): void;
31
+ /**
32
+ * Rejects the promise
33
+ *
34
+ * @param value - the value to reject the promise with
35
+ */
36
+ reject(error: any): void;
37
+ }
38
+ //# sourceMappingURL=promises.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promises.d.ts","sourceRoot":"","sources":["../src/promises.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,qBAAa,QAAQ,CAAC,CAAC;IACtB,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAa;IAC/B,OAAO,CAAC,GAAG,CAAoD;IAC/D,OAAO,CAAC,GAAG,CAA2C;IACtD,OAAO,CAAC,SAAS,CAAkB;;IAQnC;;OAEG;IACH,IAAW,WAAW,IAAI,OAAO,CAEhC;IAED;;;;OAIG;IACH,IAAW,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,CAE/B;IAED;;;;OAIG;IACI,OAAO,CAAC,KAAK,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI;IAO/C;;;;OAIG;IAGI,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,IAAI;CAM/B"}
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.Deferred = void 0;
8
+ /**
9
+ * A deferred creates a promise and the ability to resolve or reject it
10
+ * @alpha
11
+ */
12
+ class Deferred {
13
+ constructor() {
14
+ this.completed = false;
15
+ this.p = new Promise((resolve, reject) => {
16
+ this.res = resolve;
17
+ this.rej = reject;
18
+ });
19
+ }
20
+ /**
21
+ * Returns whether the underlying promise has been completed
22
+ */
23
+ get isCompleted() {
24
+ return this.completed;
25
+ }
26
+ /**
27
+ * Retrieves the underlying promise for the deferred
28
+ *
29
+ * @returns the underlying promise
30
+ */
31
+ get promise() {
32
+ return this.p;
33
+ }
34
+ /**
35
+ * Resolves the promise
36
+ *
37
+ * @param value - the value to resolve the promise with
38
+ */
39
+ resolve(value) {
40
+ if (this.res !== undefined) {
41
+ this.completed = true;
42
+ this.res(value);
43
+ }
44
+ }
45
+ /**
46
+ * Rejects the promise
47
+ *
48
+ * @param value - the value to reject the promise with
49
+ */
50
+ // TODO: Use `unknown` instead (API breaking)
51
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
52
+ reject(error) {
53
+ if (this.rej !== undefined) {
54
+ this.completed = true;
55
+ this.rej(error);
56
+ }
57
+ }
58
+ }
59
+ exports.Deferred = Deferred;
60
+ //# sourceMappingURL=promises.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promises.js","sourceRoot":"","sources":["../src/promises.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;GAGG;AACH,MAAa,QAAQ;IAMpB;QAFQ,cAAS,GAAY,KAAK,CAAC;QAGlC,IAAI,CAAC,CAAC,GAAG,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3C,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC;YACnB,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC;QACnB,CAAC,CAAC,CAAC;IACJ,CAAC;IACD;;OAEG;IACH,IAAW,WAAW;QACrB,OAAO,IAAI,CAAC,SAAS,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,IAAW,OAAO;QACjB,OAAO,IAAI,CAAC,CAAC,CAAC;IACf,CAAC;IAED;;;;OAIG;IACI,OAAO,CAAC,KAAyB;QACvC,IAAI,IAAI,CAAC,GAAG,KAAK,SAAS,EAAE;YAC3B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;YACtB,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;SAChB;IACF,CAAC;IAED;;;;OAIG;IACH,6CAA6C;IAC7C,iHAAiH;IAC1G,MAAM,CAAC,KAAU;QACvB,IAAI,IAAI,CAAC,GAAG,KAAK,SAAS,EAAE;YAC3B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;YACtB,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;SAChB;IACF,CAAC;CACD;AArDD,4BAqDC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * A deferred creates a promise and the ability to resolve or reject it\n * @alpha\n */\nexport class Deferred<T> {\n\tprivate readonly p: Promise<T>;\n\tprivate res: ((value: T | PromiseLike<T>) => void) | undefined;\n\tprivate rej: ((reason?: unknown) => void) | undefined;\n\tprivate completed: boolean = false;\n\n\tpublic constructor() {\n\t\tthis.p = new Promise<T>((resolve, reject) => {\n\t\t\tthis.res = resolve;\n\t\t\tthis.rej = reject;\n\t\t});\n\t}\n\t/**\n\t * Returns whether the underlying promise has been completed\n\t */\n\tpublic get isCompleted(): boolean {\n\t\treturn this.completed;\n\t}\n\n\t/**\n\t * Retrieves the underlying promise for the deferred\n\t *\n\t * @returns the underlying promise\n\t */\n\tpublic get promise(): Promise<T> {\n\t\treturn this.p;\n\t}\n\n\t/**\n\t * Resolves the promise\n\t *\n\t * @param value - the value to resolve the promise with\n\t */\n\tpublic resolve(value: T | PromiseLike<T>): void {\n\t\tif (this.res !== undefined) {\n\t\t\tthis.completed = true;\n\t\t\tthis.res(value);\n\t\t}\n\t}\n\n\t/**\n\t * Rejects the promise\n\t *\n\t * @param value - the value to reject the promise with\n\t */\n\t// TODO: Use `unknown` instead (API breaking)\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types\n\tpublic reject(error: any): void {\n\t\tif (this.rej !== undefined) {\n\t\t\tthis.completed = true;\n\t\t\tthis.rej(error);\n\t\t}\n\t}\n}\n"]}
@@ -0,0 +1,115 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * @internal
7
+ */
8
+ export interface ITimer {
9
+ /**
10
+ * True if timer is currently running
11
+ */
12
+ readonly hasTimer: boolean;
13
+ /**
14
+ * Starts the timer
15
+ */
16
+ start(): void;
17
+ /**
18
+ * Cancels the timer if already running
19
+ */
20
+ clear(): void;
21
+ }
22
+ /**
23
+ * Sets timeouts like the setTimeout function allowing timeouts to exceed the setTimeout's max timeout limit.
24
+ * Timeouts may not be exactly accurate due to browser implementations and the OS.
25
+ * https://stackoverflow.com/questions/21097421/what-is-the-reason-javascript-settimeout-is-so-inaccurate
26
+ * @param timeoutFn - Executed when the timeout expires
27
+ * @param timeoutMs - Duration of the timeout in milliseconds
28
+ * @param setTimeoutIdFn - Executed to update the timeout if multiple timeouts are required when
29
+ * timeoutMs greater than maxTimeout
30
+ * @returns The initial timeout
31
+ * @internal
32
+ */
33
+ export declare function setLongTimeout(timeoutFn: () => void, timeoutMs: number, setTimeoutIdFn?: (timeoutId: ReturnType<typeof setTimeout>) => void): ReturnType<typeof setTimeout>;
34
+ /**
35
+ * This class is a thin wrapper over setTimeout and clearTimeout which
36
+ * makes it simpler to keep track of recurring timeouts with the same
37
+ * or similar handlers and timeouts. This class supports long timeouts
38
+ * or timeouts exceeding (2^31)-1 ms or approximately 24.8 days.
39
+ * @internal
40
+ */
41
+ export declare class Timer implements ITimer {
42
+ private readonly defaultTimeout;
43
+ private readonly defaultHandler;
44
+ private readonly getCurrentTick;
45
+ /**
46
+ * Returns true if the timer is running.
47
+ */
48
+ get hasTimer(): boolean;
49
+ private runningState;
50
+ constructor(defaultTimeout: number, defaultHandler: () => void, getCurrentTick?: () => number);
51
+ /**
52
+ * Calls setTimeout and tracks the resulting timeout.
53
+ * @param ms - overrides default timeout in ms
54
+ * @param handler - overrides default handler
55
+ */
56
+ start(ms?: number, handler?: () => void): void;
57
+ /**
58
+ * Calls clearTimeout on the underlying timeout if running.
59
+ */
60
+ clear(): void;
61
+ /**
62
+ * Restarts the timer with the new handler and duration.
63
+ * If a new handler is passed, the original handler may
64
+ * never execute.
65
+ * This is a potentially more efficient way to clear and start
66
+ * a new timer.
67
+ * @param ms - overrides previous or default timeout in ms
68
+ * @param handler - overrides previous or default handler
69
+ */
70
+ restart(ms?: number, handler?: () => void): void;
71
+ private startCore;
72
+ private handler;
73
+ private calculateRemainingTime;
74
+ }
75
+ /**
76
+ * @internal
77
+ */
78
+ export interface IPromiseTimerResult {
79
+ timerResult: "timeout" | "cancel";
80
+ }
81
+ /**
82
+ * Timer which offers a promise that fulfills when the timer
83
+ * completes.
84
+ * @internal
85
+ */
86
+ export interface IPromiseTimer extends ITimer {
87
+ /**
88
+ * Starts the timer and returns a promise that
89
+ * resolves when the timer times out or is canceled.
90
+ */
91
+ start(): Promise<IPromiseTimerResult>;
92
+ }
93
+ /**
94
+ * This class is a wrapper over setTimeout and clearTimeout which
95
+ * makes it simpler to keep track of recurring timeouts with the
96
+ * same handlers and timeouts, while also providing a promise that
97
+ * resolves when it times out.
98
+ * @internal
99
+ */
100
+ export declare class PromiseTimer implements IPromiseTimer {
101
+ private deferred?;
102
+ private readonly timer;
103
+ /**
104
+ * {@inheritDoc Timer.hasTimer}
105
+ */
106
+ get hasTimer(): boolean;
107
+ constructor(defaultTimeout: number, defaultHandler: () => void);
108
+ /**
109
+ * {@inheritDoc IPromiseTimer.start}
110
+ */
111
+ start(ms?: number, handler?: () => void): Promise<IPromiseTimerResult>;
112
+ clear(): void;
113
+ protected wrapHandler(handler: () => void): void;
114
+ }
115
+ //# sourceMappingURL=timer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timer.d.ts","sourceRoot":"","sources":["../src/timer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH;;GAEG;AACH,MAAM,WAAW,MAAM;IACtB;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAE3B;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;IAEd;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;CACd;AAsCD;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC7B,SAAS,EAAE,MAAM,IAAI,EACrB,SAAS,EAAE,MAAM,EACjB,cAAc,CAAC,EAAE,CAAC,SAAS,EAAE,UAAU,C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E,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAO5E,KAAK,IAAI,IAAI;IAQpB,SAAS,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,IAAI,GAAG,IAAI;CAMhD"}
package/dist/timer.js ADDED
@@ -0,0 +1,189 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.PromiseTimer = exports.Timer = exports.setLongTimeout = void 0;
8
+ const assert_1 = require("./assert");
9
+ const promises_1 = require("./promises");
10
+ const maxSetTimeoutMs = 0x7fffffff; // setTimeout limit is MAX_INT32=(2^31-1).
11
+ /**
12
+ * Sets timeouts like the setTimeout function allowing timeouts to exceed the setTimeout's max timeout limit.
13
+ * Timeouts may not be exactly accurate due to browser implementations and the OS.
14
+ * https://stackoverflow.com/questions/21097421/what-is-the-reason-javascript-settimeout-is-so-inaccurate
15
+ * @param timeoutFn - Executed when the timeout expires
16
+ * @param timeoutMs - Duration of the timeout in milliseconds
17
+ * @param setTimeoutIdFn - Executed to update the timeout if multiple timeouts are required when
18
+ * timeoutMs greater than maxTimeout
19
+ * @returns The initial timeout
20
+ * @internal
21
+ */
22
+ function setLongTimeout(timeoutFn, timeoutMs, setTimeoutIdFn) {
23
+ // The setTimeout max is 24.8 days before looping occurs.
24
+ let timeoutId;
25
+ if (timeoutMs > maxSetTimeoutMs) {
26
+ const newTimeoutMs = timeoutMs - maxSetTimeoutMs;
27
+ timeoutId = setTimeout(() => setLongTimeout(timeoutFn, newTimeoutMs, setTimeoutIdFn), maxSetTimeoutMs);
28
+ }
29
+ else {
30
+ timeoutId = setTimeout(() => timeoutFn(), Math.max(timeoutMs, 0));
31
+ }
32
+ setTimeoutIdFn?.(timeoutId);
33
+ return timeoutId;
34
+ }
35
+ exports.setLongTimeout = setLongTimeout;
36
+ /**
37
+ * This class is a thin wrapper over setTimeout and clearTimeout which
38
+ * makes it simpler to keep track of recurring timeouts with the same
39
+ * or similar handlers and timeouts. This class supports long timeouts
40
+ * or timeouts exceeding (2^31)-1 ms or approximately 24.8 days.
41
+ * @internal
42
+ */
43
+ class Timer {
44
+ /**
45
+ * Returns true if the timer is running.
46
+ */
47
+ get hasTimer() {
48
+ return !!this.runningState;
49
+ }
50
+ constructor(defaultTimeout, defaultHandler, getCurrentTick = () => Date.now()) {
51
+ this.defaultTimeout = defaultTimeout;
52
+ this.defaultHandler = defaultHandler;
53
+ this.getCurrentTick = getCurrentTick;
54
+ }
55
+ /**
56
+ * Calls setTimeout and tracks the resulting timeout.
57
+ * @param ms - overrides default timeout in ms
58
+ * @param handler - overrides default handler
59
+ */
60
+ start(ms = this.defaultTimeout, handler = this.defaultHandler) {
61
+ this.startCore(ms, handler, ms);
62
+ }
63
+ /**
64
+ * Calls clearTimeout on the underlying timeout if running.
65
+ */
66
+ clear() {
67
+ if (!this.runningState) {
68
+ return;
69
+ }
70
+ clearTimeout(this.runningState.timeout);
71
+ this.runningState = undefined;
72
+ }
73
+ /**
74
+ * Restarts the timer with the new handler and duration.
75
+ * If a new handler is passed, the original handler may
76
+ * never execute.
77
+ * This is a potentially more efficient way to clear and start
78
+ * a new timer.
79
+ * @param ms - overrides previous or default timeout in ms
80
+ * @param handler - overrides previous or default handler
81
+ */
82
+ restart(ms, handler) {
83
+ if (this.runningState) {
84
+ const duration = ms ?? this.runningState.intendedDuration;
85
+ const handlerToUse = handler ?? this.runningState.restart?.handler ?? this.runningState.handler;
86
+ const remainingTime = this.calculateRemainingTime(this.runningState);
87
+ if (duration < remainingTime) {
88
+ // If remaining time exceeds restart duration, do a hard restart.
89
+ // The existing timeout time is too long.
90
+ this.start(duration, handlerToUse);
91
+ }
92
+ else if (duration === remainingTime) {
93
+ // The existing timeout time is perfect, just update handler and data.
94
+ this.runningState.handler = handlerToUse;
95
+ this.runningState.restart = undefined;
96
+ this.runningState.intendedDuration = duration;
97
+ }
98
+ else {
99
+ // If restart duration exceeds remaining time, set restart info.
100
+ // Existing timeout will start a new timeout for remaining time.
101
+ this.runningState.restart = {
102
+ startTick: this.getCurrentTick(),
103
+ duration,
104
+ handler: handlerToUse,
105
+ };
106
+ }
107
+ }
108
+ else {
109
+ // If restart is called first, it behaves as a call to start
110
+ this.start(ms, handler);
111
+ }
112
+ }
113
+ startCore(duration, handler, intendedDuration) {
114
+ this.clear();
115
+ this.runningState = {
116
+ startTick: this.getCurrentTick(),
117
+ duration,
118
+ intendedDuration,
119
+ handler,
120
+ timeout: setLongTimeout(() => this.handler(), duration, (timer) => {
121
+ if (this.runningState !== undefined) {
122
+ this.runningState.timeout = timer;
123
+ }
124
+ }),
125
+ };
126
+ }
127
+ handler() {
128
+ (0, assert_1.assert)(!!this.runningState, 0x764 /* Running timer missing handler */);
129
+ const restart = this.runningState.restart;
130
+ if (restart === undefined) {
131
+ // Run clear first, in case the handler decides to start again
132
+ const handler = this.runningState.handler;
133
+ this.clear();
134
+ handler();
135
+ }
136
+ else {
137
+ // Restart with remaining time
138
+ const remainingTime = this.calculateRemainingTime(restart);
139
+ this.startCore(remainingTime, () => restart.handler(), restart.duration);
140
+ }
141
+ }
142
+ calculateRemainingTime(runningTimeout) {
143
+ const elapsedTime = this.getCurrentTick() - runningTimeout.startTick;
144
+ return runningTimeout.duration - elapsedTime;
145
+ }
146
+ }
147
+ exports.Timer = Timer;
148
+ /**
149
+ * This class is a wrapper over setTimeout and clearTimeout which
150
+ * makes it simpler to keep track of recurring timeouts with the
151
+ * same handlers and timeouts, while also providing a promise that
152
+ * resolves when it times out.
153
+ * @internal
154
+ */
155
+ class PromiseTimer {
156
+ /**
157
+ * {@inheritDoc Timer.hasTimer}
158
+ */
159
+ get hasTimer() {
160
+ return this.timer.hasTimer;
161
+ }
162
+ constructor(defaultTimeout, defaultHandler) {
163
+ this.timer = new Timer(defaultTimeout, () => this.wrapHandler(defaultHandler));
164
+ }
165
+ /**
166
+ * {@inheritDoc IPromiseTimer.start}
167
+ */
168
+ async start(ms, handler) {
169
+ this.clear();
170
+ this.deferred = new promises_1.Deferred();
171
+ this.timer.start(ms, handler ? () => this.wrapHandler(handler) : undefined);
172
+ return this.deferred.promise;
173
+ }
174
+ clear() {
175
+ this.timer.clear();
176
+ if (this.deferred) {
177
+ this.deferred.resolve({ timerResult: "cancel" });
178
+ this.deferred = undefined;
179
+ }
180
+ }
181
+ wrapHandler(handler) {
182
+ handler();
183
+ (0, assert_1.assert)(!!this.deferred, 0x765 /* Handler executed without deferred */);
184
+ this.deferred.resolve({ timerResult: "timeout" });
185
+ this.deferred = undefined;
186
+ }
187
+ }
188
+ exports.PromiseTimer = PromiseTimer;
189
+ //# sourceMappingURL=timer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timer.js","sourceRoot":"","sources":["../src/timer.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,qCAAkC;AAClC,yCAAsC;AAwDtC,MAAM,eAAe,GAAG,UAAU,CAAC,CAAC,0CAA0C;AAE9E;;;;;;;;;;GAUG;AACH,SAAgB,cAAc,CAC7B,SAAqB,EACrB,SAAiB,EACjB,cAAmE;IAEnE,yDAAyD;IACzD,IAAI,SAAwC,CAAC;IAC7C,IAAI,SAAS,GAAG,eAAe,EAAE;QAChC,MAAM,YAAY,GAAG,SAAS,GAAG,eAAe,CAAC;QACjD,SAAS,GAAG,UAAU,CACrB,GAAG,EAAE,CAAC,cAAc,CAAC,SAAS,EAAE,YAAY,EAAE,cAAc,CAAC,EAC7D,eAAe,CACf,CAAC;KACF;SAAM;QACN,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,SAAS,EAAE,EAAE,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CAAC;KAClE;IAED,cAAc,EAAE,CAAC,SAAS,CAAC,CAAC;IAC5B,OAAO,SAAS,CAAC;AAClB,CAAC;AAnBD,wCAmBC;AAED;;;;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AAC,QAAQ,EAAE;YAClB,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,WAAW,EAAE,QAAQ,EAAE,CAAC,CAAC;YACjD,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;SAC1B;IACF,CAAC;IAES,WAAW,CAAC,OAAmB;QACxC,OAAO,EAAE,CAAC;QACV,IAAA,eAAM,EAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,uCAAuC,CAAC,CAAC;QACvE,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,CAAC;QAClD,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC3B,CAAC;CACD;AAvCD,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\n/**\n * @internal\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 * @internal\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 * @internal\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\n/**\n * @internal\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 * @internal\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 * @internal\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"]}
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.38.3"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,22 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * This function can be used to assert at compile time that a given value has type never.
7
+ * One common usage is in the default case of a switch block,
8
+ * to ensure that all cases are explicitly handled.
9
+ *
10
+ * Example:
11
+ * ```typescript
12
+ * const bool: true | false = ...;
13
+ * switch(bool) {
14
+ * case true: {...}
15
+ * case false: {...}
16
+ * default: unreachableCase(bool);
17
+ * }
18
+ * ```
19
+ * @internal
20
+ */
21
+ export declare function unreachableCase(_: never, message?: string): never;
22
+ //# sourceMappingURL=unreachable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unreachable.d.ts","sourceRoot":"","sources":["../src/unreachable.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,SAAqB,GAAG,KAAK,CAE7E"}