@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.
- package/.eslintrc.js +36 -0
- package/.mocharc.js +13 -0
- package/CHANGELOG.md +81 -0
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/api-extractor-lint.json +4 -0
- package/api-extractor.json +4 -0
- package/api-report/core-utils.api.md +147 -0
- package/dist/assert.d.ts +17 -0
- package/dist/assert.d.ts.map +1 -0
- package/dist/assert.js +25 -0
- package/dist/assert.js.map +1 -0
- package/dist/compare.d.ts +16 -0
- package/dist/compare.d.ts.map +1 -0
- package/dist/compare.js +28 -0
- package/dist/compare.js.map +1 -0
- package/dist/core-utils-alpha.d.ts +191 -0
- package/dist/core-utils-beta.d.ts +41 -0
- package/dist/core-utils-public.d.ts +41 -0
- package/dist/core-utils-untrimmed.d.ts +414 -0
- package/dist/delay.d.ts +11 -0
- package/dist/delay.d.ts.map +1 -0
- package/dist/delay.js +15 -0
- package/dist/delay.js.map +1 -0
- package/dist/heap.d.ts +86 -0
- package/dist/heap.d.ts.map +1 -0
- package/dist/heap.js +144 -0
- package/dist/heap.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/lazy.d.ts +44 -0
- package/dist/lazy.d.ts.map +1 -0
- package/dist/lazy.js +84 -0
- package/dist/lazy.js.map +1 -0
- package/dist/promiseCache.d.ts +89 -0
- package/dist/promiseCache.d.ts.map +1 -0
- package/dist/promiseCache.js +148 -0
- package/dist/promiseCache.js.map +1 -0
- package/dist/promises.d.ts +38 -0
- package/dist/promises.d.ts.map +1 -0
- package/dist/promises.js +60 -0
- package/dist/promises.js.map +1 -0
- package/dist/timer.d.ts +115 -0
- package/dist/timer.d.ts.map +1 -0
- package/dist/timer.js +189 -0
- package/dist/timer.js.map +1 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/dist/unreachable.d.ts +22 -0
- package/dist/unreachable.d.ts.map +1 -0
- package/dist/unreachable.js +28 -0
- package/dist/unreachable.js.map +1 -0
- package/lib/assert.d.ts +17 -0
- package/lib/assert.d.ts.map +1 -0
- package/lib/assert.js +25 -0
- package/lib/assert.js.map +1 -0
- package/lib/compare.d.ts +16 -0
- package/lib/compare.d.ts.map +1 -0
- package/lib/compare.js +28 -0
- package/lib/compare.js.map +1 -0
- package/lib/core-utils-alpha.d.ts +191 -0
- package/lib/core-utils-beta.d.ts +41 -0
- package/lib/core-utils-public.d.ts +41 -0
- package/lib/core-utils-untrimmed.d.ts +414 -0
- package/lib/delay.d.ts +11 -0
- package/lib/delay.d.ts.map +1 -0
- package/lib/delay.js +15 -0
- package/lib/delay.js.map +1 -0
- package/lib/heap.d.ts +86 -0
- package/lib/heap.d.ts.map +1 -0
- package/lib/heap.js +144 -0
- package/lib/heap.js.map +1 -0
- package/lib/index.d.ts +14 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +30 -0
- package/lib/index.js.map +1 -0
- package/lib/lazy.d.ts +44 -0
- package/lib/lazy.d.ts.map +1 -0
- package/lib/lazy.js +84 -0
- package/lib/lazy.js.map +1 -0
- package/lib/promiseCache.d.ts +89 -0
- package/lib/promiseCache.d.ts.map +1 -0
- package/lib/promiseCache.js +148 -0
- package/lib/promiseCache.js.map +1 -0
- package/lib/promises.d.ts +38 -0
- package/lib/promises.d.ts.map +1 -0
- package/lib/promises.js +60 -0
- package/lib/promises.js.map +1 -0
- package/lib/timer.d.ts +115 -0
- package/lib/timer.d.ts.map +1 -0
- package/lib/timer.js +189 -0
- package/lib/timer.js.map +1 -0
- package/lib/unreachable.d.ts +22 -0
- package/lib/unreachable.d.ts.map +1 -0
- package/lib/unreachable.js +28 -0
- package/lib/unreachable.js.map +1 -0
- package/package.json +111 -0
- package/prettier.config.cjs +8 -0
- package/src/assert.ts +23 -0
- package/src/compare.ts +33 -0
- package/src/delay.ts +12 -0
- package/src/heap.ts +182 -0
- package/src/index.ts +21 -0
- package/src/lazy.ts +88 -0
- package/src/promiseCache.ts +205 -0
- package/src/promises.ts +63 -0
- package/src/timer.ts +289 -0
- package/src/unreachable.ts +24 -0
- package/tsconfig.esnext.json +6 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,28 @@
|
|
|
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.unreachableCase = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* This function can be used to assert at compile time that a given value has type never.
|
|
10
|
+
* One common usage is in the default case of a switch block,
|
|
11
|
+
* to ensure that all cases are explicitly handled.
|
|
12
|
+
*
|
|
13
|
+
* Example:
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const bool: true | false = ...;
|
|
16
|
+
* switch(bool) {
|
|
17
|
+
* case true: {...}
|
|
18
|
+
* case false: {...}
|
|
19
|
+
* default: unreachableCase(bool);
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
function unreachableCase(_, message = "Unreachable Case") {
|
|
25
|
+
throw new Error(message);
|
|
26
|
+
}
|
|
27
|
+
exports.unreachableCase = unreachableCase;
|
|
28
|
+
//# sourceMappingURL=unreachable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unreachable.js","sourceRoot":"","sources":["../src/unreachable.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,eAAe,CAAC,CAAQ,EAAE,OAAO,GAAG,kBAAkB;IACrE,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;AAC1B,CAAC;AAFD,0CAEC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * This function can be used to assert at compile time that a given value has type never.\n * One common usage is in the default case of a switch block,\n * to ensure that all cases are explicitly handled.\n *\n * Example:\n * ```typescript\n * const bool: true | false = ...;\n * switch(bool) {\n * case true: {...}\n * case false: {...}\n * default: unreachableCase(bool);\n * }\n * ```\n * @internal\n */\nexport function unreachableCase(_: never, message = \"Unreachable Case\"): never {\n\tthrow new Error(message);\n}\n"]}
|
package/lib/assert.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* A browser friendly assert library.
|
|
7
|
+
* Use this instead of the 'assert' package, which has a big impact on bundle sizes.
|
|
8
|
+
* @param condition - The condition that should be true, if the condition is false an error will be thrown.
|
|
9
|
+
* Only use this API when `false` indicates a logic error in the problem and thus a bug that should be fixed.
|
|
10
|
+
* @param message - The message to include in the error when the condition does not hold.
|
|
11
|
+
* A number should not be specified manually: use a string.
|
|
12
|
+
* Before a release, policy-check should be run, which will convert any asserts still using strings to
|
|
13
|
+
* use numbered error codes instead.
|
|
14
|
+
* @alpha
|
|
15
|
+
*/
|
|
16
|
+
export declare function assert(condition: boolean, message: string | number): asserts condition;
|
|
17
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;GAUG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,SAAS,CAMtF"}
|
package/lib/assert.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
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.assert = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* A browser friendly assert library.
|
|
10
|
+
* Use this instead of the 'assert' package, which has a big impact on bundle sizes.
|
|
11
|
+
* @param condition - The condition that should be true, if the condition is false an error will be thrown.
|
|
12
|
+
* Only use this API when `false` indicates a logic error in the problem and thus a bug that should be fixed.
|
|
13
|
+
* @param message - The message to include in the error when the condition does not hold.
|
|
14
|
+
* A number should not be specified manually: use a string.
|
|
15
|
+
* Before a release, policy-check should be run, which will convert any asserts still using strings to
|
|
16
|
+
* use numbered error codes instead.
|
|
17
|
+
* @alpha
|
|
18
|
+
*/
|
|
19
|
+
function assert(condition, message) {
|
|
20
|
+
if (!condition) {
|
|
21
|
+
throw new Error(typeof message === "number" ? `0x${message.toString(16).padStart(3, "0")}` : message);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.assert = assert;
|
|
25
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.js","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;;;;;;;;GAUG;AACH,SAAgB,MAAM,CAAC,SAAkB,EAAE,OAAwB;IAClE,IAAI,CAAC,SAAS,EAAE;QACf,MAAM,IAAI,KAAK,CACd,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CACpF,CAAC;KACF;AACF,CAAC;AAND,wBAMC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * A browser friendly assert library.\n * Use this instead of the 'assert' package, which has a big impact on bundle sizes.\n * @param condition - The condition that should be true, if the condition is false an error will be thrown.\n * Only use this API when `false` indicates a logic error in the problem and thus a bug that should be fixed.\n * @param message - The message to include in the error when the condition does not hold.\n * A number should not be specified manually: use a string.\n * Before a release, policy-check should be run, which will convert any asserts still using strings to\n * use numbered error codes instead.\n * @alpha\n */\nexport function assert(condition: boolean, message: string | number): asserts condition {\n\tif (!condition) {\n\t\tthrow new Error(\n\t\t\ttypeof message === \"number\" ? `0x${message.toString(16).padStart(3, \"0\")}` : message,\n\t\t);\n\t}\n}\n"]}
|
package/lib/compare.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Compare two arrays. Returns true if their elements are equivalent and in the same order.
|
|
7
|
+
*
|
|
8
|
+
* @alpha
|
|
9
|
+
*
|
|
10
|
+
* @param left - The first array to compare
|
|
11
|
+
* @param right - The second array to compare
|
|
12
|
+
* @param comparator - The function used to check if two `T`s are equivalent.
|
|
13
|
+
* Defaults to `Object.is()` equality (a shallow compare where NaN = NaN and -0 ≠ 0)
|
|
14
|
+
*/
|
|
15
|
+
export declare const compareArrays: <T>(left: readonly T[], right: readonly T[], comparator?: (leftItem: T, rightItem: T, index: number) => boolean) => boolean;
|
|
16
|
+
//# sourceMappingURL=compare.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compare.d.ts","sourceRoot":"","sources":["../src/compare.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,+FAGsB,MAAM,KAAK,OAAO,KAI/D,OAUF,CAAC"}
|
package/lib/compare.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
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.compareArrays = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* Compare two arrays. Returns true if their elements are equivalent and in the same order.
|
|
10
|
+
*
|
|
11
|
+
* @alpha
|
|
12
|
+
*
|
|
13
|
+
* @param left - The first array to compare
|
|
14
|
+
* @param right - The second array to compare
|
|
15
|
+
* @param comparator - The function used to check if two `T`s are equivalent.
|
|
16
|
+
* Defaults to `Object.is()` equality (a shallow compare where NaN = NaN and -0 ≠ 0)
|
|
17
|
+
*/
|
|
18
|
+
const compareArrays = (left, right, comparator = (leftItem, rightItem) => Object.is(leftItem, rightItem)) => {
|
|
19
|
+
// PERF: 'for-loop' and 'Array.every()' tied.
|
|
20
|
+
// '===' and 'Object.is()' tied.
|
|
21
|
+
// Trivial acceptance adds no measurable overhead.
|
|
22
|
+
// 30% penalty vs. baseline for exported function [node 14 x64].
|
|
23
|
+
return (left === right || // Trivial acceptance: 'left' and 'right' are the same instance
|
|
24
|
+
(left.length === right.length && // Trivial rejection: 'left' and 'right' are different lengths
|
|
25
|
+
left.every((leftItem, index) => comparator(leftItem, right[index], index))));
|
|
26
|
+
};
|
|
27
|
+
exports.compareArrays = compareArrays;
|
|
28
|
+
//# sourceMappingURL=compare.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compare.js","sourceRoot":"","sources":["../src/compare.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;;;;;;;GASG;AACI,MAAM,aAAa,GAAG,CAC5B,IAAkB,EAClB,KAAmB,EACnB,aAAoE,CACnE,QAAW,EACX,SAAY,EACF,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,CAAC,EAClC,EAAE;IACZ,6CAA6C;IAC7C,sCAAsC;IACtC,wDAAwD;IACxD,sEAAsE;IACtE,OAAO,CACN,IAAI,KAAK,KAAK,IAAI,+DAA+D;QACjF,CAAC,IAAI,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,IAAI,8DAA8D;YAC9F,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAC5E,CAAC;AACH,CAAC,CAAC;AAjBW,QAAA,aAAa,iBAiBxB","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Compare two arrays. Returns true if their elements are equivalent and in the same order.\n *\n * @alpha\n *\n * @param left - The first array to compare\n * @param right - The second array to compare\n * @param comparator - The function used to check if two `T`s are equivalent.\n * Defaults to `Object.is()` equality (a shallow compare where NaN = NaN and -0 ≠ 0)\n */\nexport const compareArrays = <T>(\n\tleft: readonly T[],\n\tright: readonly T[],\n\tcomparator: (leftItem: T, rightItem: T, index: number) => boolean = (\n\t\tleftItem: T,\n\t\trightItem: T,\n\t): boolean => Object.is(leftItem, rightItem),\n): boolean => {\n\t// PERF: 'for-loop' and 'Array.every()' tied.\n\t// '===' and 'Object.is()' tied.\n\t// Trivial acceptance adds no measurable overhead.\n\t// 30% penalty vs. baseline for exported function [node 14 x64].\n\treturn (\n\t\tleft === right || // Trivial acceptance: 'left' and 'right' are the same instance\n\t\t(left.length === right.length && // Trivial rejection: 'left' and 'right' are different lengths\n\t\t\tleft.every((leftItem, index) => comparator(leftItem, right[index], index)))\n\t);\n};\n"]}
|
|
@@ -0,0 +1,191 @@
|
|
|
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
|
+
* @alpha
|
|
11
|
+
*/
|
|
12
|
+
export declare function assert(condition: boolean, message: string | number): asserts condition;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Compare two arrays. Returns true if their elements are equivalent and in the same order.
|
|
16
|
+
*
|
|
17
|
+
* @alpha
|
|
18
|
+
*
|
|
19
|
+
* @param left - The first array to compare
|
|
20
|
+
* @param right - The second array to compare
|
|
21
|
+
* @param comparator - The function used to check if two `T`s are equivalent.
|
|
22
|
+
* Defaults to `Object.is()` equality (a shallow compare where NaN = NaN and -0 ≠ 0)
|
|
23
|
+
*/
|
|
24
|
+
export declare const compareArrays: <T>(left: readonly T[], right: readonly T[], comparator?: (leftItem: T, rightItem: T, index: number) => boolean) => boolean;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A deferred creates a promise and the ability to resolve or reject it
|
|
28
|
+
* @alpha
|
|
29
|
+
*/
|
|
30
|
+
export declare class Deferred<T> {
|
|
31
|
+
private readonly p;
|
|
32
|
+
private res;
|
|
33
|
+
private rej;
|
|
34
|
+
private completed;
|
|
35
|
+
constructor();
|
|
36
|
+
/**
|
|
37
|
+
* Returns whether the underlying promise has been completed
|
|
38
|
+
*/
|
|
39
|
+
get isCompleted(): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Retrieves the underlying promise for the deferred
|
|
42
|
+
*
|
|
43
|
+
* @returns the underlying promise
|
|
44
|
+
*/
|
|
45
|
+
get promise(): Promise<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Resolves the promise
|
|
48
|
+
*
|
|
49
|
+
* @param value - the value to resolve the promise with
|
|
50
|
+
*/
|
|
51
|
+
resolve(value: T | PromiseLike<T>): void;
|
|
52
|
+
/**
|
|
53
|
+
* Rejects the promise
|
|
54
|
+
*
|
|
55
|
+
* @param value - the value to reject the promise with
|
|
56
|
+
*/
|
|
57
|
+
reject(error: any): void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* Excluded from this release type: delay */
|
|
61
|
+
|
|
62
|
+
/* Excluded from this release type: Heap */
|
|
63
|
+
|
|
64
|
+
/* Excluded from this release type: IComparer */
|
|
65
|
+
|
|
66
|
+
/* Excluded from this release type: IHeapNode */
|
|
67
|
+
|
|
68
|
+
/* Excluded from this release type: IPromiseTimer */
|
|
69
|
+
|
|
70
|
+
/* Excluded from this release type: IPromiseTimerResult */
|
|
71
|
+
|
|
72
|
+
/* Excluded from this release type: ITimer */
|
|
73
|
+
|
|
74
|
+
/* Excluded from this release type: Lazy */
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A lazy evaluated promise. The execute function is delayed until
|
|
78
|
+
* the promise is used, e.g. await, then, catch ...
|
|
79
|
+
* The execute function is only called once.
|
|
80
|
+
* All calls are then proxied to the promise returned by the execute method.
|
|
81
|
+
* @alpha
|
|
82
|
+
*/
|
|
83
|
+
export declare class LazyPromise<T> implements Promise<T> {
|
|
84
|
+
private readonly execute;
|
|
85
|
+
get [Symbol.toStringTag](): string;
|
|
86
|
+
private result;
|
|
87
|
+
constructor(execute: () => Promise<T>);
|
|
88
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
|
|
89
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<T | TResult>;
|
|
90
|
+
finally(onfinally?: (() => void) | null | undefined): Promise<T>;
|
|
91
|
+
private getPromise;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/* Excluded from this release type: NumberComparer */
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* A specialized cache for async work, allowing you to safely cache the promised result of some async work
|
|
98
|
+
* without fear of running it multiple times or losing track of errors.
|
|
99
|
+
* @alpha
|
|
100
|
+
*/
|
|
101
|
+
export declare class PromiseCache<TKey, TResult> {
|
|
102
|
+
private readonly cache;
|
|
103
|
+
private readonly gc;
|
|
104
|
+
private readonly removeOnError;
|
|
105
|
+
/**
|
|
106
|
+
* Create the PromiseCache with the given options, with the following defaults:
|
|
107
|
+
*
|
|
108
|
+
* expiry: indefinite, removeOnError: true for all errors
|
|
109
|
+
*/
|
|
110
|
+
constructor({ expiry, removeOnError, }?: PromiseCacheOptions);
|
|
111
|
+
/**
|
|
112
|
+
* Check if there's anything cached at the given key
|
|
113
|
+
*/
|
|
114
|
+
has(key: TKey): boolean;
|
|
115
|
+
/**
|
|
116
|
+
* Get the Promise for the given key, or undefined if it's not found.
|
|
117
|
+
* Extend expiry if applicable.
|
|
118
|
+
*/
|
|
119
|
+
get(key: TKey): Promise<TResult> | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* Remove the Promise for the given key, returning true if it was found and removed
|
|
122
|
+
*/
|
|
123
|
+
remove(key: TKey): boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
|
|
126
|
+
* Returns a Promise for the added or existing async work being done at that key.
|
|
127
|
+
* @param key - key name where to store the async work
|
|
128
|
+
* @param asyncFn - the async work to do and store, if not already in progress under the given key
|
|
129
|
+
*/
|
|
130
|
+
addOrGet(key: TKey, asyncFn: () => Promise<TResult>): Promise<TResult>;
|
|
131
|
+
/**
|
|
132
|
+
* Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
|
|
133
|
+
* Returns false if the cache already contained an entry at that key, and true otherwise.
|
|
134
|
+
* @param key - key name where to store the async work
|
|
135
|
+
* @param asyncFn - the async work to do and store, if not already in progress under the given key
|
|
136
|
+
*/
|
|
137
|
+
add(key: TKey, asyncFn: () => Promise<TResult>): boolean;
|
|
138
|
+
/**
|
|
139
|
+
* Try to add the given value, without overwriting an existing cache entry at that key.
|
|
140
|
+
* Returns a Promise for the added or existing async work being done at that key.
|
|
141
|
+
* @param key - key name where to store the async work
|
|
142
|
+
* @param value - value to store
|
|
143
|
+
*/
|
|
144
|
+
addValueOrGet(key: TKey, value: TResult): Promise<TResult>;
|
|
145
|
+
/**
|
|
146
|
+
* Try to add the given value, without overwriting an existing cache entry at that key.
|
|
147
|
+
* Returns false if the cache already contained an entry at that key, and true otherwise.
|
|
148
|
+
* @param key - key name where to store the value
|
|
149
|
+
* @param value - value to store
|
|
150
|
+
*/
|
|
151
|
+
addValue(key: TKey, value: TResult): boolean;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Three supported expiry policies:
|
|
156
|
+
* - indefinite: entries don't expire and must be explicitly removed
|
|
157
|
+
* - absolute: entries expire after the given duration in MS, even if accessed multiple times in the mean time
|
|
158
|
+
* - sliding: entries expire after the given duration in MS of inactivity (i.e. get resets the clock)
|
|
159
|
+
* @alpha
|
|
160
|
+
*/
|
|
161
|
+
export declare type PromiseCacheExpiry = {
|
|
162
|
+
policy: "indefinite";
|
|
163
|
+
} | {
|
|
164
|
+
policy: "absolute" | "sliding";
|
|
165
|
+
durationMs: number;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Options for configuring the {@link PromiseCache}
|
|
170
|
+
* @alpha
|
|
171
|
+
*/
|
|
172
|
+
export declare interface PromiseCacheOptions {
|
|
173
|
+
/**
|
|
174
|
+
* Common expiration policy for all items added to this cache
|
|
175
|
+
*/
|
|
176
|
+
expiry?: PromiseCacheExpiry;
|
|
177
|
+
/**
|
|
178
|
+
* If the stored Promise is rejected with a particular error, should the given key be removed?
|
|
179
|
+
*/
|
|
180
|
+
removeOnError?: (error: any) => boolean;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/* Excluded from this release type: PromiseTimer */
|
|
184
|
+
|
|
185
|
+
/* Excluded from this release type: setLongTimeout */
|
|
186
|
+
|
|
187
|
+
/* Excluded from this release type: Timer */
|
|
188
|
+
|
|
189
|
+
/* Excluded from this release type: unreachableCase */
|
|
190
|
+
|
|
191
|
+
export { }
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/* Excluded from this release type: assert */
|
|
2
|
+
|
|
3
|
+
/* Excluded from this release type: compareArrays */
|
|
4
|
+
|
|
5
|
+
/* Excluded from this release type: Deferred */
|
|
6
|
+
|
|
7
|
+
/* Excluded from this release type: delay */
|
|
8
|
+
|
|
9
|
+
/* Excluded from this release type: Heap */
|
|
10
|
+
|
|
11
|
+
/* Excluded from this release type: IComparer */
|
|
12
|
+
|
|
13
|
+
/* Excluded from this release type: IHeapNode */
|
|
14
|
+
|
|
15
|
+
/* Excluded from this release type: IPromiseTimer */
|
|
16
|
+
|
|
17
|
+
/* Excluded from this release type: IPromiseTimerResult */
|
|
18
|
+
|
|
19
|
+
/* Excluded from this release type: ITimer */
|
|
20
|
+
|
|
21
|
+
/* Excluded from this release type: Lazy */
|
|
22
|
+
|
|
23
|
+
/* Excluded from this release type: LazyPromise */
|
|
24
|
+
|
|
25
|
+
/* Excluded from this release type: NumberComparer */
|
|
26
|
+
|
|
27
|
+
/* Excluded from this release type: PromiseCache */
|
|
28
|
+
|
|
29
|
+
/* Excluded from this release type: PromiseCacheExpiry */
|
|
30
|
+
|
|
31
|
+
/* Excluded from this release type: PromiseCacheOptions */
|
|
32
|
+
|
|
33
|
+
/* Excluded from this release type: PromiseTimer */
|
|
34
|
+
|
|
35
|
+
/* Excluded from this release type: setLongTimeout */
|
|
36
|
+
|
|
37
|
+
/* Excluded from this release type: Timer */
|
|
38
|
+
|
|
39
|
+
/* Excluded from this release type: unreachableCase */
|
|
40
|
+
|
|
41
|
+
export { }
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/* Excluded from this release type: assert */
|
|
2
|
+
|
|
3
|
+
/* Excluded from this release type: compareArrays */
|
|
4
|
+
|
|
5
|
+
/* Excluded from this release type: Deferred */
|
|
6
|
+
|
|
7
|
+
/* Excluded from this release type: delay */
|
|
8
|
+
|
|
9
|
+
/* Excluded from this release type: Heap */
|
|
10
|
+
|
|
11
|
+
/* Excluded from this release type: IComparer */
|
|
12
|
+
|
|
13
|
+
/* Excluded from this release type: IHeapNode */
|
|
14
|
+
|
|
15
|
+
/* Excluded from this release type: IPromiseTimer */
|
|
16
|
+
|
|
17
|
+
/* Excluded from this release type: IPromiseTimerResult */
|
|
18
|
+
|
|
19
|
+
/* Excluded from this release type: ITimer */
|
|
20
|
+
|
|
21
|
+
/* Excluded from this release type: Lazy */
|
|
22
|
+
|
|
23
|
+
/* Excluded from this release type: LazyPromise */
|
|
24
|
+
|
|
25
|
+
/* Excluded from this release type: NumberComparer */
|
|
26
|
+
|
|
27
|
+
/* Excluded from this release type: PromiseCache */
|
|
28
|
+
|
|
29
|
+
/* Excluded from this release type: PromiseCacheExpiry */
|
|
30
|
+
|
|
31
|
+
/* Excluded from this release type: PromiseCacheOptions */
|
|
32
|
+
|
|
33
|
+
/* Excluded from this release type: PromiseTimer */
|
|
34
|
+
|
|
35
|
+
/* Excluded from this release type: setLongTimeout */
|
|
36
|
+
|
|
37
|
+
/* Excluded from this release type: Timer */
|
|
38
|
+
|
|
39
|
+
/* Excluded from this release type: unreachableCase */
|
|
40
|
+
|
|
41
|
+
export { }
|