-
Notifications
You must be signed in to change notification settings - Fork 545
/
Copy pathpromiseCache.ts
211 lines (188 loc) · 6.23 KB
/
promiseCache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
/**
* Three supported expiry policies:
* - indefinite: entries don't expire and must be explicitly removed
* - absolute: entries expire after the given duration in MS, even if accessed multiple times in the mean time
* - sliding: entries expire after the given duration in MS of inactivity (i.e. get resets the clock)
*
* @deprecated Moved to the `@fluidframework/core-utils` package.
* @internal
*/
export type PromiseCacheExpiry =
| {
policy: "indefinite";
}
| {
policy: "absolute" | "sliding";
durationMs: number;
};
/**
* Options for configuring the {@link PromiseCache}
*
* @deprecated Moved to the `@fluidframework/core-utils` package.
* @internal
*/
export interface PromiseCacheOptions {
/**
* Common expiration policy for all items added to this cache
*/
expiry?: PromiseCacheExpiry;
/**
* If the stored Promise is rejected with a particular error, should the given key be removed?
*/
removeOnError?: (e: any) => boolean;
}
/**
* Handles garbage collection of expiring cache entries.
* Not exported.
*
* @deprecated Only used internally by the {@link PromiseCache} class.
*/
class GarbageCollector<TKey> {
private readonly gcTimeouts = new Map<TKey, ReturnType<typeof setTimeout>>();
constructor(
private readonly expiry: PromiseCacheExpiry,
private readonly cleanup: (key: TKey) => void,
) {}
/**
* Schedule GC for the given key, as applicable
*/
public schedule(key: TKey): void {
if (this.expiry.policy !== "indefinite") {
this.gcTimeouts.set(
key,
setTimeout(() => {
this.cleanup(key);
this.cancel(key);
}, this.expiry.durationMs),
);
}
}
/**
* Cancel any pending GC for the given key
*/
public cancel(key: TKey): void {
const timeout = this.gcTimeouts.get(key);
if (timeout !== undefined) {
clearTimeout(timeout);
this.gcTimeouts.delete(key);
}
}
/**
* Update any pending GC for the given key, as applicable
*/
public update(key: TKey): void {
// Cancel/reschedule new GC if the policy is sliding
if (this.expiry.policy === "sliding") {
this.cancel(key);
this.schedule(key);
}
}
}
/**
* A specialized cache for async work, allowing you to safely cache the promised result of some async work
* without fear of running it multiple times or losing track of errors.
*
* @deprecated Moved to the `@fluidframework/core-utils` package.
* @internal
*/
export class PromiseCache<TKey, TResult> {
private readonly cache = new Map<TKey, Promise<TResult>>();
private readonly gc: GarbageCollector<TKey>;
private readonly removeOnError: (error: any) => boolean;
/**
* Create the PromiseCache with the given options, with the following defaults:
*
* expiry: indefinite, removeOnError: true for all errors
*/
constructor({
expiry = { policy: "indefinite" },
removeOnError = (): boolean => true,
}: PromiseCacheOptions = {}) {
this.removeOnError = removeOnError;
this.gc = new GarbageCollector<TKey>(expiry, (key) => this.remove(key));
}
/**
* Check if there's anything cached at the given key
*/
public has(key: TKey): boolean {
return this.cache.has(key);
}
/**
* Get the Promise for the given key, or undefined if it's not found.
* Extend expiry if applicable.
*/
public get(key: TKey): Promise<TResult> | undefined {
if (this.has(key)) {
this.gc.update(key);
}
return this.cache.get(key);
}
/**
* Remove the Promise for the given key, returning true if it was found and removed
*/
public remove(key: TKey): boolean {
this.gc.cancel(key);
return this.cache.delete(key);
}
/**
* Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
* Returns a Promise for the added or existing async work being done at that key.
* @param key - key name where to store the async work
* @param asyncFn - the async work to do and store, if not already in progress under the given key
*/
public async addOrGet(key: TKey, asyncFn: () => Promise<TResult>): Promise<TResult> {
// NOTE: Do not await the Promise returned by asyncFn!
// Let the caller do so once we return or after a subsequent call to get
let promise = this.get(key);
if (promise === undefined) {
// Wrap in an async lambda in case asyncFn disabled @typescript-eslint/promise-function-async
const safeAsyncFn = async (): Promise<TResult> => asyncFn();
// Start the async work and put the Promise in the cache
promise = safeAsyncFn();
this.cache.set(key, promise);
// If asyncFn throws, we may remove the Promise from the cache
promise.catch((error) => {
if (this.removeOnError(error)) {
this.remove(key);
}
});
this.gc.schedule(key);
}
return promise;
}
/**
* Try to add the result of the given asyncFn, without overwriting an existing cache entry at that key.
* Returns false if the cache already contained an entry at that key, and true otherwise.
* @param key - key name where to store the async work
* @param asyncFn - the async work to do and store, if not already in progress under the given key
*/
public add(key: TKey, asyncFn: () => Promise<TResult>): boolean {
const alreadyPresent = this.has(key);
// We are blindly adding the Promise to the cache here, which introduces a Promise in this scope.
// Swallow Promise rejections here, since whoever gets this out of the cache to use it will await/catch.
this.addOrGet(key, asyncFn).catch(() => {});
return !alreadyPresent;
}
/**
* Try to add the given value, without overwriting an existing cache entry at that key.
* Returns a Promise for the added or existing async work being done at that key.
* @param key - key name where to store the async work
* @param value - value to store
*/
public async addValueOrGet(key: TKey, value: TResult): Promise<TResult> {
return this.addOrGet(key, async () => value);
}
/**
* Try to add the given value, without overwriting an existing cache entry at that key.
* Returns false if the cache already contained an entry at that key, and true otherwise.
* @param key - key name where to store the value
* @param value - value to store
*/
public addValue(key: TKey, value: TResult): boolean {
return this.add(key, async () => value);
}
}