@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
package/dist/delay.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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.delay = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* Returns a promise that resolves after `timeMs`.
|
|
10
|
+
* @param timeMs - Time in milliseconds to wait.
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
const delay = async (timeMs) => new Promise((resolve) => setTimeout(() => resolve(), timeMs));
|
|
14
|
+
exports.delay = delay;
|
|
15
|
+
//# sourceMappingURL=delay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delay.js","sourceRoot":"","sources":["../src/delay.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;;GAIG;AACI,MAAM,KAAK,GAAG,KAAK,EAAE,MAAc,EAAiB,EAAE,CAC5D,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC;AADlD,QAAA,KAAK,SAC6C","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Returns a promise that resolves after `timeMs`.\n * @param timeMs - Time in milliseconds to wait.\n * @internal\n */\nexport const delay = async (timeMs: number): Promise<void> =>\n\tnew Promise((resolve) => setTimeout(() => resolve(), timeMs));\n"]}
|
package/dist/heap.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Interface for a comparer.
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export interface IComparer<T> {
|
|
10
|
+
/**
|
|
11
|
+
* The minimum value of type T.
|
|
12
|
+
*/
|
|
13
|
+
min: T;
|
|
14
|
+
/**
|
|
15
|
+
* Compare the two value
|
|
16
|
+
*
|
|
17
|
+
* @returns 0 if the value is equal, negative number if a is smaller then b, positive number otherwise
|
|
18
|
+
*/
|
|
19
|
+
compare(a: T, b: T): number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A comparer for numbers.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export declare const NumberComparer: IComparer<number>;
|
|
26
|
+
/**
|
|
27
|
+
* Interface to a node in {@link Heap}.
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
export interface IHeapNode<T> {
|
|
31
|
+
value: T;
|
|
32
|
+
position: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Ordered {@link https://en.wikipedia.org/wiki/Heap_(data_structure) | Heap} data structure implementation.
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
export declare class Heap<T> {
|
|
39
|
+
comp: IComparer<T>;
|
|
40
|
+
private L;
|
|
41
|
+
/**
|
|
42
|
+
* Creates an instance of `Heap` with comparer.
|
|
43
|
+
* @param comp - A comparer that specify how elements are ordered.
|
|
44
|
+
*/
|
|
45
|
+
constructor(comp: IComparer<T>);
|
|
46
|
+
/**
|
|
47
|
+
* Return the smallest element in the heap as determined by the order of the comparer
|
|
48
|
+
*
|
|
49
|
+
* @returns Heap node containing the smallest element
|
|
50
|
+
*/
|
|
51
|
+
peek(): IHeapNode<T> | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Get and remove the smallest element in the heap as determined by the order of the comparer
|
|
54
|
+
*
|
|
55
|
+
* @returns The smallest value in the heap
|
|
56
|
+
*/
|
|
57
|
+
get(): T | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Add a value to the heap
|
|
60
|
+
*
|
|
61
|
+
* @param x - value to add
|
|
62
|
+
* @returns The heap node that contains the value
|
|
63
|
+
*/
|
|
64
|
+
add(x: T): IHeapNode<T>;
|
|
65
|
+
/**
|
|
66
|
+
* Allows for the Heap to be updated after a node's value changes.
|
|
67
|
+
*/
|
|
68
|
+
update(node: IHeapNode<T>): void;
|
|
69
|
+
/**
|
|
70
|
+
* Removes the given node from the heap.
|
|
71
|
+
*
|
|
72
|
+
* @param node - The node to remove from the heap.
|
|
73
|
+
*/
|
|
74
|
+
remove(node: IHeapNode<T>): void;
|
|
75
|
+
/**
|
|
76
|
+
* Get the number of elements in the Heap.
|
|
77
|
+
*
|
|
78
|
+
* @returns The number of elements in the Heap.
|
|
79
|
+
*/
|
|
80
|
+
count(): number;
|
|
81
|
+
private fixup;
|
|
82
|
+
private isGreaterThanParent;
|
|
83
|
+
private fixdown;
|
|
84
|
+
private swap;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=heap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heap.d.ts","sourceRoot":"","sources":["../src/heap.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,MAAM,WAAW,SAAS,CAAC,CAAC;IAC3B;;OAEG;IACH,GAAG,EAAE,CAAC,CAAC;IAEP;;;;OAIG;IACH,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC;CAC5B;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,EAAE,SAAS,CAAC,MAAM,CAW5C,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,SAAS,CAAC,CAAC;IAC3B,KAAK,EAAE,CAAC,CAAC;IACT,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,qBAAa,IAAI,CAAC,CAAC;IAOC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC;IANrC,OAAO,CAAC,CAAC,CAAiB;IAE1B;;;OAGG;gBACgB,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC;IAIrC;;;;OAIG;IACI,IAAI,IAAI,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS;IAIvC;;;;OAIG;IACI,GAAG,IAAI,CAAC,GAAG,SAAS;IAY3B;;;;;OAKG;IACI,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC;IAQ9B;;OAEG;IACI,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI;IASvC;;;;OAIG;IACI,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI;IAYvC;;;;OAIG;IACI,KAAK,IAAI,MAAM;IAItB,OAAO,CAAC,KAAK;IAUb,OAAO,CAAC,mBAAmB;IAK3B,OAAO,CAAC,OAAO;IAiBf,OAAO,CAAC,IAAI;CAOZ"}
|
package/dist/heap.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
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.Heap = exports.NumberComparer = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* A comparer for numbers.
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
exports.NumberComparer = {
|
|
13
|
+
/**
|
|
14
|
+
* The compare function for numbers.
|
|
15
|
+
* @returns The difference of the two numbers.
|
|
16
|
+
*/
|
|
17
|
+
compare: (a, b) => a - b,
|
|
18
|
+
/**
|
|
19
|
+
* The minimum value of a JavaScript number, which is `Number.MIN_VALUE`.
|
|
20
|
+
*/
|
|
21
|
+
min: Number.MIN_VALUE,
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Ordered {@link https://en.wikipedia.org/wiki/Heap_(data_structure) | Heap} data structure implementation.
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
class Heap {
|
|
28
|
+
/**
|
|
29
|
+
* Creates an instance of `Heap` with comparer.
|
|
30
|
+
* @param comp - A comparer that specify how elements are ordered.
|
|
31
|
+
*/
|
|
32
|
+
constructor(comp) {
|
|
33
|
+
this.comp = comp;
|
|
34
|
+
this.L = [{ value: comp.min, position: 0 }];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Return the smallest element in the heap as determined by the order of the comparer
|
|
38
|
+
*
|
|
39
|
+
* @returns Heap node containing the smallest element
|
|
40
|
+
*/
|
|
41
|
+
peek() {
|
|
42
|
+
return this.L[1];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get and remove the smallest element in the heap as determined by the order of the comparer
|
|
46
|
+
*
|
|
47
|
+
* @returns The smallest value in the heap
|
|
48
|
+
*/
|
|
49
|
+
get() {
|
|
50
|
+
if (this.L.length === 0) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
this.swap(1, this.count());
|
|
54
|
+
const x = this.L.pop();
|
|
55
|
+
this.fixdown(1);
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
57
|
+
return x.value;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Add a value to the heap
|
|
61
|
+
*
|
|
62
|
+
* @param x - value to add
|
|
63
|
+
* @returns The heap node that contains the value
|
|
64
|
+
*/
|
|
65
|
+
add(x) {
|
|
66
|
+
const node = { value: x, position: this.L.length };
|
|
67
|
+
this.L.push(node);
|
|
68
|
+
this.fixup(this.count());
|
|
69
|
+
return node;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Allows for the Heap to be updated after a node's value changes.
|
|
73
|
+
*/
|
|
74
|
+
update(node) {
|
|
75
|
+
const k = node.position;
|
|
76
|
+
if (this.isGreaterThanParent(k)) {
|
|
77
|
+
this.fixup(k);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
this.fixdown(k);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Removes the given node from the heap.
|
|
85
|
+
*
|
|
86
|
+
* @param node - The node to remove from the heap.
|
|
87
|
+
*/
|
|
88
|
+
remove(node) {
|
|
89
|
+
// Move the node we want to remove to the end of the array
|
|
90
|
+
const position = node.position;
|
|
91
|
+
this.swap(node.position, this.L.length - 1);
|
|
92
|
+
this.L.splice(-1);
|
|
93
|
+
// Update the swapped node assuming we didn't remove the end of the list
|
|
94
|
+
if (position !== this.L.length) {
|
|
95
|
+
this.update(this.L[position]);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Get the number of elements in the Heap.
|
|
100
|
+
*
|
|
101
|
+
* @returns The number of elements in the Heap.
|
|
102
|
+
*/
|
|
103
|
+
count() {
|
|
104
|
+
return this.L.length - 1;
|
|
105
|
+
}
|
|
106
|
+
fixup(pos) {
|
|
107
|
+
let k = pos;
|
|
108
|
+
while (this.isGreaterThanParent(k)) {
|
|
109
|
+
// eslint-disable-next-line no-bitwise
|
|
110
|
+
const parent = k >> 1;
|
|
111
|
+
this.swap(k, parent);
|
|
112
|
+
k = parent;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
isGreaterThanParent(k) {
|
|
116
|
+
// eslint-disable-next-line no-bitwise
|
|
117
|
+
return k > 1 && this.comp.compare(this.L[k >> 1].value, this.L[k].value) > 0;
|
|
118
|
+
}
|
|
119
|
+
fixdown(pos) {
|
|
120
|
+
let k = pos;
|
|
121
|
+
// eslint-disable-next-line no-bitwise
|
|
122
|
+
while (k << 1 <= this.count()) {
|
|
123
|
+
// eslint-disable-next-line no-bitwise
|
|
124
|
+
let j = k << 1;
|
|
125
|
+
if (j < this.count() && this.comp.compare(this.L[j].value, this.L[j + 1].value) > 0) {
|
|
126
|
+
j++;
|
|
127
|
+
}
|
|
128
|
+
if (this.comp.compare(this.L[k].value, this.L[j].value) <= 0) {
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
this.swap(k, j);
|
|
132
|
+
k = j;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
swap(k, j) {
|
|
136
|
+
const tmp = this.L[k];
|
|
137
|
+
this.L[k] = this.L[j];
|
|
138
|
+
this.L[k].position = k;
|
|
139
|
+
this.L[j] = tmp;
|
|
140
|
+
this.L[j].position = j;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.Heap = Heap;
|
|
144
|
+
//# sourceMappingURL=heap.js.map
|
package/dist/heap.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"heap.js","sourceRoot":"","sources":["../src/heap.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAoBH;;;GAGG;AACU,QAAA,cAAc,GAAsB;IAChD;;;OAGG;IACH,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,EAAU,EAAE,CAAC,CAAC,GAAG,CAAC;IAEhC;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC,SAAS;CACrB,CAAC;AAWF;;;GAGG;AACH,MAAa,IAAI;IAGhB;;;OAGG;IACH,YAAmB,IAAkB;QAAlB,SAAI,GAAJ,IAAI,CAAc;QACpC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,GAAG,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED;;;;OAIG;IACI,IAAI;QACV,OAAO,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IAED;;;;OAIG;IACI,GAAG;QACT,IA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EAAE,CAAC;aACJ;YACD,IAAI,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;gBAC7D,MAAM;aACN;YACD,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YAChB,CAAC,GAAG,CAAC,CAAC;SACN;IACF,CAAC;IAEO,IAAI,CAAC,CAAS,EAAE,CAAS;QAChC,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACtB,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACtB,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC;QACvB,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC;IACxB,CAAC;CACD;AAhID,oBAgIC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Interface for a comparer.\n * @internal\n */\nexport interface IComparer<T> {\n\t/**\n\t * The minimum value of type T.\n\t */\n\tmin: T;\n\n\t/**\n\t * Compare the two value\n\t *\n\t * @returns 0 if the value is equal, negative number if a is smaller then b, positive number otherwise\n\t */\n\tcompare(a: T, b: T): number;\n}\n\n/**\n * A comparer for numbers.\n * @internal\n */\nexport const NumberComparer: IComparer<number> = {\n\t/**\n\t * The compare function for numbers.\n\t * @returns The difference of the two numbers.\n\t */\n\tcompare: (a, b): number => a - b,\n\n\t/**\n\t * The minimum value of a JavaScript number, which is `Number.MIN_VALUE`.\n\t */\n\tmin: Number.MIN_VALUE,\n};\n\n/**\n * Interface to a node in {@link Heap}.\n * @internal\n */\nexport interface IHeapNode<T> {\n\tvalue: T;\n\tposition: number;\n}\n\n/**\n * Ordered {@link https://en.wikipedia.org/wiki/Heap_(data_structure) | Heap} data structure implementation.\n * @internal\n */\nexport class Heap<T> {\n\tprivate L: IHeapNode<T>[];\n\n\t/**\n\t * Creates an instance of `Heap` with comparer.\n\t * @param comp - A comparer that specify how elements are ordered.\n\t */\n\tconstructor(public comp: IComparer<T>) {\n\t\tthis.L = [{ value: comp.min, position: 0 }];\n\t}\n\n\t/**\n\t * Return the smallest element in the heap as determined by the order of the comparer\n\t *\n\t * @returns Heap node containing the smallest element\n\t */\n\tpublic peek(): IHeapNode<T> | undefined {\n\t\treturn this.L[1];\n\t}\n\n\t/**\n\t * Get and remove the smallest element in the heap as determined by the order of the comparer\n\t *\n\t * @returns The smallest value in the heap\n\t */\n\tpublic get(): T | undefined {\n\t\tif (this.L.length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tthis.swap(1, this.count());\n\t\tconst x = this.L.pop();\n\t\tthis.fixdown(1);\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\treturn x!.value;\n\t}\n\n\t/**\n\t * Add a value to the heap\n\t *\n\t * @param x - value to add\n\t * @returns The heap node that contains the value\n\t */\n\tpublic add(x: T): IHeapNode<T> {\n\t\tconst node = { value: x, position: this.L.length };\n\t\tthis.L.push(node);\n\t\tthis.fixup(this.count());\n\n\t\treturn node;\n\t}\n\n\t/**\n\t * Allows for the Heap to be updated after a node's value changes.\n\t */\n\tpublic update(node: IHeapNode<T>): void {\n\t\tconst k = node.position;\n\t\tif (this.isGreaterThanParent(k)) {\n\t\t\tthis.fixup(k);\n\t\t} else {\n\t\t\tthis.fixdown(k);\n\t\t}\n\t}\n\n\t/**\n\t * Removes the given node from the heap.\n\t *\n\t * @param node - The node to remove from the heap.\n\t */\n\tpublic remove(node: IHeapNode<T>): void {\n\t\t// Move the node we want to remove to the end of the array\n\t\tconst position = node.position;\n\t\tthis.swap(node.position, this.L.length - 1);\n\t\tthis.L.splice(-1);\n\n\t\t// Update the swapped node assuming we didn't remove the end of the list\n\t\tif (position !== this.L.length) {\n\t\t\tthis.update(this.L[position]);\n\t\t}\n\t}\n\n\t/**\n\t * Get the number of elements in the Heap.\n\t *\n\t * @returns The number of elements in the Heap.\n\t */\n\tpublic count(): number {\n\t\treturn this.L.length - 1;\n\t}\n\n\tprivate fixup(pos: number): void {\n\t\tlet k = pos;\n\t\twhile (this.isGreaterThanParent(k)) {\n\t\t\t// eslint-disable-next-line no-bitwise\n\t\t\tconst parent = k >> 1;\n\t\t\tthis.swap(k, parent);\n\t\t\tk = parent;\n\t\t}\n\t}\n\n\tprivate isGreaterThanParent(k: number): boolean {\n\t\t// eslint-disable-next-line no-bitwise\n\t\treturn k > 1 && this.comp.compare(this.L[k >> 1].value, this.L[k].value) > 0;\n\t}\n\n\tprivate fixdown(pos: number): void {\n\t\tlet k = pos;\n\t\t// eslint-disable-next-line no-bitwise\n\t\twhile (k << 1 <= this.count()) {\n\t\t\t// eslint-disable-next-line no-bitwise\n\t\t\tlet j = k << 1;\n\t\t\tif (j < this.count() && this.comp.compare(this.L[j].value, this.L[j + 1].value) > 0) {\n\t\t\t\tj++;\n\t\t\t}\n\t\t\tif (this.comp.compare(this.L[k].value, this.L[j].value) <= 0) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tthis.swap(k, j);\n\t\t\tk = j;\n\t\t}\n\t}\n\n\tprivate swap(k: number, j: number): void {\n\t\tconst tmp = this.L[k];\n\t\tthis.L[k] = this.L[j];\n\t\tthis.L[k].position = k;\n\t\tthis.L[j] = tmp;\n\t\tthis.L[j].position = j;\n\t}\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
export { assert } from "./assert";
|
|
6
|
+
export { compareArrays } from "./compare";
|
|
7
|
+
export { delay } from "./delay";
|
|
8
|
+
export { Heap, IComparer, IHeapNode, NumberComparer } from "./heap";
|
|
9
|
+
export { Lazy, LazyPromise } from "./lazy";
|
|
10
|
+
export { PromiseCache, PromiseCacheExpiry, PromiseCacheOptions } from "./promiseCache";
|
|
11
|
+
export { Deferred } from "./promises";
|
|
12
|
+
export { IPromiseTimer, IPromiseTimerResult, ITimer, PromiseTimer, setLongTimeout, Timer, } from "./timer";
|
|
13
|
+
export { unreachableCase } from "./unreachable";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AACpE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACvF,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EACN,aAAa,EACb,mBAAmB,EACnB,MAAM,EACN,YAAY,EACZ,cAAc,EACd,KAAK,GACL,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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 = exports.Timer = exports.setLongTimeout = exports.PromiseTimer = exports.Deferred = exports.PromiseCache = exports.LazyPromise = exports.Lazy = exports.NumberComparer = exports.Heap = exports.delay = exports.compareArrays = exports.assert = void 0;
|
|
8
|
+
var assert_1 = require("./assert");
|
|
9
|
+
Object.defineProperty(exports, "assert", { enumerable: true, get: function () { return assert_1.assert; } });
|
|
10
|
+
var compare_1 = require("./compare");
|
|
11
|
+
Object.defineProperty(exports, "compareArrays", { enumerable: true, get: function () { return compare_1.compareArrays; } });
|
|
12
|
+
var delay_1 = require("./delay");
|
|
13
|
+
Object.defineProperty(exports, "delay", { enumerable: true, get: function () { return delay_1.delay; } });
|
|
14
|
+
var heap_1 = require("./heap");
|
|
15
|
+
Object.defineProperty(exports, "Heap", { enumerable: true, get: function () { return heap_1.Heap; } });
|
|
16
|
+
Object.defineProperty(exports, "NumberComparer", { enumerable: true, get: function () { return heap_1.NumberComparer; } });
|
|
17
|
+
var lazy_1 = require("./lazy");
|
|
18
|
+
Object.defineProperty(exports, "Lazy", { enumerable: true, get: function () { return lazy_1.Lazy; } });
|
|
19
|
+
Object.defineProperty(exports, "LazyPromise", { enumerable: true, get: function () { return lazy_1.LazyPromise; } });
|
|
20
|
+
var promiseCache_1 = require("./promiseCache");
|
|
21
|
+
Object.defineProperty(exports, "PromiseCache", { enumerable: true, get: function () { return promiseCache_1.PromiseCache; } });
|
|
22
|
+
var promises_1 = require("./promises");
|
|
23
|
+
Object.defineProperty(exports, "Deferred", { enumerable: true, get: function () { return promises_1.Deferred; } });
|
|
24
|
+
var timer_1 = require("./timer");
|
|
25
|
+
Object.defineProperty(exports, "PromiseTimer", { enumerable: true, get: function () { return timer_1.PromiseTimer; } });
|
|
26
|
+
Object.defineProperty(exports, "setLongTimeout", { enumerable: true, get: function () { return timer_1.setLongTimeout; } });
|
|
27
|
+
Object.defineProperty(exports, "Timer", { enumerable: true, get: function () { return timer_1.Timer; } });
|
|
28
|
+
var unreachable_1 = require("./unreachable");
|
|
29
|
+
Object.defineProperty(exports, "unreachableCase", { enumerable: true, get: function () { return unreachable_1.unreachableCase; } });
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AACf,qCAA0C;AAAjC,wGAAA,aAAa,OAAA;AACtB,iCAAgC;AAAvB,8FAAA,KAAK,OAAA;AACd,+BAAoE;AAA3D,4FAAA,IAAI,OAAA;AAAwB,sGAAA,cAAc,OAAA;AACnD,+BAA2C;AAAlC,4FAAA,IAAI,OAAA;AAAE,mGAAA,WAAW,OAAA;AAC1B,+CAAuF;AAA9E,4GAAA,YAAY,OAAA;AACrB,uCAAsC;AAA7B,oGAAA,QAAQ,OAAA;AACjB,iCAOiB;AAHhB,qGAAA,YAAY,OAAA;AACZ,uGAAA,cAAc,OAAA;AACd,8FAAA,KAAK,OAAA;AAEN,6CAAgD;AAAvC,8GAAA,eAAe,OAAA","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport { assert } from \"./assert\";\nexport { compareArrays } from \"./compare\";\nexport { delay } from \"./delay\";\nexport { Heap, IComparer, IHeapNode, NumberComparer } from \"./heap\";\nexport { Lazy, LazyPromise } from \"./lazy\";\nexport { PromiseCache, PromiseCacheExpiry, PromiseCacheOptions } from \"./promiseCache\";\nexport { Deferred } from \"./promises\";\nexport {\n\tIPromiseTimer,\n\tIPromiseTimerResult,\n\tITimer,\n\tPromiseTimer,\n\tsetLongTimeout,\n\tTimer,\n} from \"./timer\";\nexport { unreachableCase } from \"./unreachable\";\n"]}
|
package/dist/lazy.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Helper class for lazy initialized values. Ensures the value is only generated once, and remain immutable.
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export declare class Lazy<T> {
|
|
10
|
+
private readonly valueGenerator;
|
|
11
|
+
private _value;
|
|
12
|
+
private _evaluated;
|
|
13
|
+
/**
|
|
14
|
+
* Instantiates an instance of Lazy<T>.
|
|
15
|
+
* @param valueGenerator - The function that will generate the value when value is accessed the first time.
|
|
16
|
+
*/
|
|
17
|
+
constructor(valueGenerator: () => T);
|
|
18
|
+
/**
|
|
19
|
+
* Return true if the value as been generated, otherwise false.
|
|
20
|
+
*/
|
|
21
|
+
get evaluated(): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Get the value. If this is the first call the value will be generated.
|
|
24
|
+
*/
|
|
25
|
+
get value(): T;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A lazy evaluated promise. The execute function is delayed until
|
|
29
|
+
* the promise is used, e.g. await, then, catch ...
|
|
30
|
+
* The execute function is only called once.
|
|
31
|
+
* All calls are then proxied to the promise returned by the execute method.
|
|
32
|
+
* @alpha
|
|
33
|
+
*/
|
|
34
|
+
export declare class LazyPromise<T> implements Promise<T> {
|
|
35
|
+
private readonly execute;
|
|
36
|
+
get [Symbol.toStringTag](): string;
|
|
37
|
+
private result;
|
|
38
|
+
constructor(execute: () => Promise<T>);
|
|
39
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
|
|
40
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<T | TResult>;
|
|
41
|
+
finally(onfinally?: (() => void) | null | undefined): Promise<T>;
|
|
42
|
+
private getPromise;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=lazy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,qBAAa,IAAI,CAAC,CAAC;IAOC,OAAO,CAAC,QAAQ,CAAC,cAAc;IANlD,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,UAAU,CAAkB;IACpC;;;OAGG;gBACiC,cAAc,EAAE,MAAM,CAAC;IAE3D;;OAEG;IACH,IAAW,SAAS,IAAI,OAAO,CAE9B;IAED;;OAEG;IACH,IAAW,KAAK,IAAI,CAAC,CAOpB;CACD;AAED;;;;;;GAMG;AACH,qBAAa,WAAW,CAAC,CAAC,CAAE,YAAW,OAAO,CAAC,CAAC,CAAC;IAO7B,OAAO,CAAC,QAAQ,CAAC,OAAO;IAN3C,IAAW,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,MAAM,CAExC;IAED,OAAO,CAAC,MAAM,CAAyB;gBAEH,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IAGhD,IAAI,CAAC,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,KAAK,EAE/C,WAAW,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,KAAK,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,EAGjF,UAAU,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,GACjF,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAKlB,KAAK,CAAC,OAAO,GAAG,KAAK,EAGjC,UAAU,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,GAC/E,OAAO,CAAC,CAAC,GAAG,OAAO,CAAC;IAMV,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAAC,CAAC,CAAC;YAK/D,UAAU;CAMxB"}
|
package/dist/lazy.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
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.LazyPromise = exports.Lazy = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* Helper class for lazy initialized values. Ensures the value is only generated once, and remain immutable.
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
class Lazy {
|
|
13
|
+
/**
|
|
14
|
+
* Instantiates an instance of Lazy<T>.
|
|
15
|
+
* @param valueGenerator - The function that will generate the value when value is accessed the first time.
|
|
16
|
+
*/
|
|
17
|
+
constructor(valueGenerator) {
|
|
18
|
+
this.valueGenerator = valueGenerator;
|
|
19
|
+
this._evaluated = false;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Return true if the value as been generated, otherwise false.
|
|
23
|
+
*/
|
|
24
|
+
get evaluated() {
|
|
25
|
+
return this._evaluated;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Get the value. If this is the first call the value will be generated.
|
|
29
|
+
*/
|
|
30
|
+
get value() {
|
|
31
|
+
if (!this._evaluated) {
|
|
32
|
+
this._evaluated = true;
|
|
33
|
+
this._value = this.valueGenerator();
|
|
34
|
+
}
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
36
|
+
return this._value;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.Lazy = Lazy;
|
|
40
|
+
/**
|
|
41
|
+
* A lazy evaluated promise. The execute function is delayed until
|
|
42
|
+
* the promise is used, e.g. await, then, catch ...
|
|
43
|
+
* The execute function is only called once.
|
|
44
|
+
* All calls are then proxied to the promise returned by the execute method.
|
|
45
|
+
* @alpha
|
|
46
|
+
*/
|
|
47
|
+
class LazyPromise {
|
|
48
|
+
get [Symbol.toStringTag]() {
|
|
49
|
+
return this.getPromise()[Symbol.toStringTag];
|
|
50
|
+
}
|
|
51
|
+
constructor(execute) {
|
|
52
|
+
this.execute = execute;
|
|
53
|
+
}
|
|
54
|
+
// eslint-disable-next-line unicorn/no-thenable
|
|
55
|
+
async then(
|
|
56
|
+
// eslint-disable-next-line @rushstack/no-new-null
|
|
57
|
+
onfulfilled,
|
|
58
|
+
// TODO: Use `unknown` instead (API breaking)
|
|
59
|
+
// eslint-disable-next-line @rushstack/no-new-null, @typescript-eslint/no-explicit-any
|
|
60
|
+
onrejected) {
|
|
61
|
+
// eslint-disable-next-line prefer-rest-params
|
|
62
|
+
return this.getPromise().then(...arguments);
|
|
63
|
+
}
|
|
64
|
+
async catch(
|
|
65
|
+
// TODO: Use `unknown` instead (API breaking)
|
|
66
|
+
// eslint-disable-next-line @rushstack/no-new-null, @typescript-eslint/no-explicit-any
|
|
67
|
+
onrejected) {
|
|
68
|
+
// eslint-disable-next-line prefer-rest-params
|
|
69
|
+
return this.getPromise().catch(...arguments);
|
|
70
|
+
}
|
|
71
|
+
// eslint-disable-next-line @rushstack/no-new-null
|
|
72
|
+
async finally(onfinally) {
|
|
73
|
+
// eslint-disable-next-line prefer-rest-params
|
|
74
|
+
return this.getPromise().finally(...arguments);
|
|
75
|
+
}
|
|
76
|
+
async getPromise() {
|
|
77
|
+
if (this.result === undefined) {
|
|
78
|
+
this.result = this.execute();
|
|
79
|
+
}
|
|
80
|
+
return this.result;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.LazyPromise = LazyPromise;
|
|
84
|
+
//# sourceMappingURL=lazy.js.map
|
package/dist/lazy.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazy.js","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;GAGG;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;;;;;;GAMG;AACH,MAAa,WAAW;IACvB,IAAW,CAAC,MAAM,CAAC,WAAW,CAAC;QAC9B,OAAO,IAAI,CAAC,UAAU,EAAE,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAC9C,CAAC;IAID,YAAoC,OAAyB;QAAzB,YAAO,GAAP,OAAO,CAAkB;IAAG,CAAC;IAEjE,+CAA+C;IACxC,KAAK,CAAC,IAAI;IAChB,kDAAkD;IAClD,WAAiF;IACjF,6CAA6C;IAC7C,sFAAsF;IACtF,UAAmF;QAEnF,8CAA8C;QAC9C,OAAO,IAAI,CAAC,UAAU,EAAE,CAAC,IAAI,CAAqB,GAAG,SAAS,CAAC,CAAC;IACjE,CAAC;IAEM,KAAK,CAAC,KAAK;IACjB,6CAA6C;IAC7C,sFAAsF;IACtF,UAAiF;QAEjF,8CAA8C;QAC9C,OAAO,IAAI,CAAC,UAAU,EAAE,CAAC,KAAK,CAAU,GAAG,SAAS,CAAC,CAAC;IACvD,CAAC;IAED,kDAAkD;IAC3C,KAAK,CAAC,OAAO,CAAC,SAA2C;QAC/D,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;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 * @internal\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 * @alpha\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"]}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Three supported expiry policies:
|
|
7
|
+
* - indefinite: entries don't expire and must be explicitly removed
|
|
8
|
+
* - absolute: entries expire after the given duration in MS, even if accessed multiple times in the mean time
|
|
9
|
+
* - sliding: entries expire after the given duration in MS of inactivity (i.e. get resets the clock)
|
|
10
|
+
* @alpha
|
|
11
|
+
*/
|
|
12
|
+
export type PromiseCacheExpiry = {
|
|
13
|
+
policy: "indefinite";
|
|
14
|
+
} | {
|
|
15
|
+
policy: "absolute" | "sliding";
|
|
16
|
+
durationMs: number;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Options for configuring the {@link PromiseCache}
|
|
20
|
+
* @alpha
|
|
21
|
+
*/
|
|
22
|
+
export interface PromiseCacheOptions {
|
|
23
|
+
/**
|
|
24
|
+
* Common expiration policy for all items added to this cache
|
|
25
|
+
*/
|
|
26
|
+
expiry?: PromiseCacheExpiry;
|
|
27
|
+
/**
|
|
28
|
+
* If the stored Promise is rejected with a particular error, should the given key be removed?
|
|
29
|
+
*/
|
|
30
|
+
removeOnError?: (error: any) => boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A specialized cache for async work, allowing you to safely cache the promised result of some async work
|
|
34
|
+
* without fear of running it multiple times or losing track of errors.
|
|
35
|
+
* @alpha
|
|
36
|
+
*/
|
|
37
|
+
export declare class PromiseCache<TKey, TResult> {
|
|
38
|
+
private readonly cache;
|
|
39
|
+
private readonly gc;
|
|
40
|
+
private readonly removeOnError;
|
|
41
|
+
/**
|
|
42
|
+
* Create the PromiseCache with the given options, with the following defaults:
|
|
43
|
+
*
|
|
44
|
+
* expiry: indefinite, removeOnError: true for all errors
|
|
45
|
+
*/
|
|
46
|
+
constructor({ expiry, removeOnError, }?: PromiseCacheOptions);
|
|
47
|
+
/**
|
|
48
|
+
* Check if there's anything cached at the given key
|
|
49
|
+
*/
|
|
50
|
+
has(key: TKey): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Get the Promise for the given key, or undefined if it's not found.
|
|
53
|
+
* Extend expiry if applicable.
|
|
54
|
+
*/
|
|
55
|
+
get(key: TKey): Promise<TResult> | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Remove the Promise for the given key, returning true if it was found and removed
|
|
58
|
+
*/
|
|
59
|
+
remove(key: TKey): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
|
|
62
|
+
* Returns a Promise for the added or existing async work being done at that key.
|
|
63
|
+
* @param key - key name where to store the async work
|
|
64
|
+
* @param asyncFn - the async work to do and store, if not already in progress under the given key
|
|
65
|
+
*/
|
|
66
|
+
addOrGet(key: TKey, asyncFn: () => Promise<TResult>): Promise<TResult>;
|
|
67
|
+
/**
|
|
68
|
+
* Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
|
|
69
|
+
* Returns false if the cache already contained an entry at that key, and true otherwise.
|
|
70
|
+
* @param key - key name where to store the async work
|
|
71
|
+
* @param asyncFn - the async work to do and store, if not already in progress under the given key
|
|
72
|
+
*/
|
|
73
|
+
add(key: TKey, asyncFn: () => Promise<TResult>): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Try to add the given value, without overwriting an existing cache entry at that key.
|
|
76
|
+
* Returns a Promise for the added or existing async work being done at that key.
|
|
77
|
+
* @param key - key name where to store the async work
|
|
78
|
+
* @param value - value to store
|
|
79
|
+
*/
|
|
80
|
+
addValueOrGet(key: TKey, value: TResult): Promise<TResult>;
|
|
81
|
+
/**
|
|
82
|
+
* Try to add the given value, without overwriting an existing cache entry at that key.
|
|
83
|
+
* Returns false if the cache already contained an entry at that key, and true otherwise.
|
|
84
|
+
* @param key - key name where to store the value
|
|
85
|
+
* @param value - value to store
|
|
86
|
+
*/
|
|
87
|
+
addValue(key: TKey, value: TResult): boolean;
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=promiseCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promiseCache.d.ts","sourceRoot":"","sources":["../src/promiseCache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;GAMG;AACH,MAAM,MAAM,kBAAkB,GAC3B;IACA,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