@zajno/common 2.8.7 → 2.8.9
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/cjs/functions/safe.js +16 -0
- package/cjs/functions/safe.js.map +1 -1
- package/cjs/lazy/lazy.js +6 -10
- package/cjs/lazy/lazy.js.map +1 -1
- package/cjs/lazy/light.js +4 -1
- package/cjs/lazy/light.js.map +1 -1
- package/cjs/lazy/promise.js +6 -10
- package/cjs/lazy/promise.js.map +1 -1
- package/cjs/structures/promiseCache/cache.js +286 -0
- package/cjs/structures/promiseCache/cache.js.map +1 -0
- package/cjs/structures/promiseCache/core.js +338 -0
- package/cjs/structures/promiseCache/core.js.map +1 -0
- package/cjs/structures/promiseCache/index.js +7 -0
- package/cjs/structures/promiseCache/index.js.map +1 -0
- package/cjs/structures/promiseCache/types.js +15 -0
- package/cjs/structures/promiseCache/types.js.map +1 -0
- package/esm/functions/safe.js +15 -0
- package/esm/functions/safe.js.map +1 -1
- package/esm/lazy/lazy.js +6 -10
- package/esm/lazy/lazy.js.map +1 -1
- package/esm/lazy/light.js +4 -1
- package/esm/lazy/light.js.map +1 -1
- package/esm/lazy/promise.js +6 -10
- package/esm/lazy/promise.js.map +1 -1
- package/esm/structures/promiseCache/cache.js +282 -0
- package/esm/structures/promiseCache/cache.js.map +1 -0
- package/esm/structures/promiseCache/core.js +334 -0
- package/esm/structures/promiseCache/core.js.map +1 -0
- package/esm/structures/promiseCache/index.js +4 -0
- package/esm/structures/promiseCache/index.js.map +1 -0
- package/esm/structures/promiseCache/types.js +12 -0
- package/esm/structures/promiseCache/types.js.map +1 -0
- package/package.json +7 -1
- package/tsconfig.cjs.tsbuildinfo +1 -1
- package/tsconfig.esm.tsbuildinfo +1 -1
- package/tsconfig.types.tsbuildinfo +1 -1
- package/types/functions/safe.d.ts +7 -0
- package/types/lazy/lazy.d.ts +3 -2
- package/types/lazy/promise.d.ts +3 -2
- package/types/lazy/types.d.ts +10 -4
- package/types/structures/promiseCache/cache.d.ts +95 -0
- package/types/structures/promiseCache/core.d.ts +173 -0
- package/types/structures/promiseCache/index.d.ts +3 -0
- package/types/structures/promiseCache/types.d.ts +58 -0
- package/cjs/structures/promiseCache.js +0 -399
- package/cjs/structures/promiseCache.js.map +0 -1
- package/esm/structures/promiseCache.js +0 -395
- package/esm/structures/promiseCache.js.map +0 -1
- package/types/structures/promiseCache.d.ts +0 -206
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
import { DebounceProcessor } from '../functions/debounce.js';
|
|
2
|
-
import { Loggable } from '../logger/loggable.js';
|
|
3
|
-
import { Model } from '../models/Model.js';
|
|
4
|
-
export var DeferredGetter;
|
|
5
|
-
(function (DeferredGetter) {
|
|
6
|
-
const _resolvedPromise = Promise.resolve(null);
|
|
7
|
-
/** Empty resolved value. */
|
|
8
|
-
DeferredGetter.Empty = {
|
|
9
|
-
get current() { return null; },
|
|
10
|
-
get promise() { return _resolvedPromise; },
|
|
11
|
-
get isLoading() { return false; },
|
|
12
|
-
};
|
|
13
|
-
})(DeferredGetter || (DeferredGetter = {}));
|
|
14
|
-
const BATCHING_DELAY = 200;
|
|
15
|
-
/**
|
|
16
|
-
* Caches items by a key (string or another type) which are resolved by an async fetcher (`Promise`).
|
|
17
|
-
*
|
|
18
|
-
* Supports:
|
|
19
|
-
* - custom key adapter and parser for non-string keys.
|
|
20
|
-
* - direct manual cache manipulation.
|
|
21
|
-
* - batching of fetches.
|
|
22
|
-
* - auto-invalidation of cached items.
|
|
23
|
-
*/
|
|
24
|
-
export class PromiseCache extends Loggable {
|
|
25
|
-
fetcher;
|
|
26
|
-
keyAdapter;
|
|
27
|
-
keyParser;
|
|
28
|
-
/** Stores resolved items in map by id. */
|
|
29
|
-
_itemsCache;
|
|
30
|
-
/** Stores items loading state (loading or not) in map by id. */
|
|
31
|
-
_itemsStatus;
|
|
32
|
-
/** Stores items loading count. */
|
|
33
|
-
_loadingCount;
|
|
34
|
-
/** Stores items Promises state (if still loading) in map by id. */
|
|
35
|
-
_fetchCache;
|
|
36
|
-
/** Stores items resolve timestamps (for expiration) in map by id. */
|
|
37
|
-
_timestamps = new Map();
|
|
38
|
-
_batch = null;
|
|
39
|
-
_invalidationTimeMs = null;
|
|
40
|
-
_keepInstanceDuringInvalidation = false;
|
|
41
|
-
_version = 0;
|
|
42
|
-
/**
|
|
43
|
-
* Creates an instance of PromiseCache.
|
|
44
|
-
* @param fetcher Function to fetch data by key.
|
|
45
|
-
* @param keyAdapter Optional function to adapt non-string keys to strings.
|
|
46
|
-
* @param keyParser Optional function to parse string keys back to their original type.
|
|
47
|
-
*/
|
|
48
|
-
constructor(fetcher, keyAdapter, keyParser) {
|
|
49
|
-
super();
|
|
50
|
-
this.fetcher = fetcher;
|
|
51
|
-
this.keyAdapter = keyAdapter;
|
|
52
|
-
this.keyParser = keyParser;
|
|
53
|
-
this._loadingCount = this.pure_createBusyCount();
|
|
54
|
-
this._itemsCache = this.pure_createItemsCache();
|
|
55
|
-
this._itemsStatus = this.pure_createItemsStatus();
|
|
56
|
-
this._fetchCache = this.pure_createFetchCache();
|
|
57
|
-
}
|
|
58
|
-
get busyCount() { return this._loadingCount.value; }
|
|
59
|
-
/**
|
|
60
|
-
* @pure @const
|
|
61
|
-
* Creates a model for tracking the loading state. Override to inject own instance, e.g. for observability.
|
|
62
|
-
*
|
|
63
|
-
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
64
|
-
*
|
|
65
|
-
* @returns A value model for the loading count.
|
|
66
|
-
*/
|
|
67
|
-
pure_createBusyCount() {
|
|
68
|
-
return new Model(0);
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* @pure @const
|
|
72
|
-
* Creates a map for caching resolved items by id. Override to inject own instance, e.g. for observability.
|
|
73
|
-
*
|
|
74
|
-
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
75
|
-
*
|
|
76
|
-
* @returns A map model for the items cache.
|
|
77
|
-
*/
|
|
78
|
-
pure_createItemsCache() {
|
|
79
|
-
return new Map();
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* @pure @const
|
|
83
|
-
* Creates a map for tracking the loading state of items by id. Override to inject own instance, e.g. for observability.
|
|
84
|
-
*
|
|
85
|
-
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
86
|
-
*
|
|
87
|
-
* @returns A map model for the items loading state.
|
|
88
|
-
*/
|
|
89
|
-
pure_createItemsStatus() {
|
|
90
|
-
return new Map();
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* @pure @const
|
|
94
|
-
* Creates a map for caching promises of items by id. Override to inject own instance, e.g. for observability.
|
|
95
|
-
*
|
|
96
|
-
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
97
|
-
*
|
|
98
|
-
* @returns A map model for the items promises cache.
|
|
99
|
-
*/
|
|
100
|
-
pure_createFetchCache() {
|
|
101
|
-
return new Map();
|
|
102
|
-
}
|
|
103
|
-
_pk(k) {
|
|
104
|
-
if (k == null) {
|
|
105
|
-
throw new Error('PromiseCache: null keys are not supported');
|
|
106
|
-
}
|
|
107
|
-
if (typeof k === 'string') {
|
|
108
|
-
return k;
|
|
109
|
-
}
|
|
110
|
-
if (!this.keyAdapter) {
|
|
111
|
-
throw new Error('Provide key adapter for non-string keys');
|
|
112
|
-
}
|
|
113
|
-
return this.keyAdapter(k);
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Creates a logger name for this instance.
|
|
117
|
-
* @param name The name of the cache instance.
|
|
118
|
-
* @returns The logger name.
|
|
119
|
-
*/
|
|
120
|
-
getLoggerName(name) {
|
|
121
|
-
return `[PromiseCache:${name || '?'}]`;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Provide a fetcher function that takes multiple ids and returns multiple results at once. Will be called with a slight delay to allow multiple ids to be collected.
|
|
125
|
-
*
|
|
126
|
-
* Warning: resolved array should have the same order as the input array.
|
|
127
|
-
*
|
|
128
|
-
* When provided, effectively replaces the main fetcher; but in case of fail, fallbacks to the main fetcher.
|
|
129
|
-
*/
|
|
130
|
-
useBatching(fetcher, delay = BATCHING_DELAY) {
|
|
131
|
-
this._batch = fetcher ? new DebounceProcessor(fetcher, delay) : null;
|
|
132
|
-
return this;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Enables auto-invalidation of cached items.
|
|
136
|
-
*
|
|
137
|
-
* @param ms Time in milliseconds after which the item will be considered invalid. If null, auto-invalidation is disabled.
|
|
138
|
-
* @param keepInstance If true, the cached item will not be removed during invalidation, but will be set to `undefined` instead. Defaults to false.
|
|
139
|
-
*/
|
|
140
|
-
useInvalidationTime(ms, keepInstance = false) {
|
|
141
|
-
this._invalidationTimeMs = ms;
|
|
142
|
-
this._keepInstanceDuringInvalidation = keepInstance;
|
|
143
|
-
return this;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Returns a {@link DeferredGetter} object for a specfied key.
|
|
147
|
-
*
|
|
148
|
-
* This can be used to access the current value, promise, and loading state of the item.
|
|
149
|
-
*
|
|
150
|
-
* @param key The key of the item.
|
|
151
|
-
*/
|
|
152
|
-
getDeferred(key) {
|
|
153
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
154
|
-
const self = this;
|
|
155
|
-
return {
|
|
156
|
-
get current() { return self.getCurrent(key); },
|
|
157
|
-
get promise() { return self.get(key); },
|
|
158
|
-
get isLoading() { return self.getIsBusy(key); },
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Returns the loading state of an item.
|
|
163
|
-
*
|
|
164
|
-
* @param id The id of the item.
|
|
165
|
-
* @returns The loading state of the item: true if loading, false if loading completed, undefined if loading was not started yet for the specified key.
|
|
166
|
-
*/
|
|
167
|
-
getIsBusy(id) {
|
|
168
|
-
const key = this._pk(id);
|
|
169
|
-
const res = this._itemsStatus.get(key);
|
|
170
|
-
if (res) {
|
|
171
|
-
return res;
|
|
172
|
-
}
|
|
173
|
-
const isInvalid = this.getIsInvalidated(key);
|
|
174
|
-
return isInvalid ? undefined : res;
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Returns the current cached value for the specified key, without triggering a fetch.
|
|
178
|
-
* @param id The id of the item.
|
|
179
|
-
* @returns The current cached value of the item.
|
|
180
|
-
*/
|
|
181
|
-
_getCurrent(id) {
|
|
182
|
-
const key = this._pk(id);
|
|
183
|
-
const isInvalid = this.getIsInvalidated(key);
|
|
184
|
-
// make sure current item is hooked here from the cache (required by observers)
|
|
185
|
-
const item = this._itemsCache.get(key);
|
|
186
|
-
if (isInvalid) {
|
|
187
|
-
this.logger.log(key, 'item is invalidated');
|
|
188
|
-
}
|
|
189
|
-
return {
|
|
190
|
-
item: (isInvalid && !this._keepInstanceDuringInvalidation) ? undefined : item,
|
|
191
|
-
key,
|
|
192
|
-
isInvalid,
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Returns the current cached value for the specified key, optionally triggering a fetch.
|
|
197
|
-
* @param id The id of the item.
|
|
198
|
-
* @param initiateFetch If true, will initiate a fetch if the item is not cached.
|
|
199
|
-
* @returns The current cached value of the item.
|
|
200
|
-
*/
|
|
201
|
-
getCurrent(id, initiateFetch = true) {
|
|
202
|
-
const { item, key } = this._getCurrent(id);
|
|
203
|
-
if (initiateFetch) {
|
|
204
|
-
// spin fetch
|
|
205
|
-
this.get(id);
|
|
206
|
-
}
|
|
207
|
-
this.logger.log(key, 'getCurrent: returns', item);
|
|
208
|
-
return item;
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Returns a promise that resolves to the cached value of the item if loaded already, otherwise starts fetching and the promise will be resolved to the final value.
|
|
212
|
-
*
|
|
213
|
-
* Consequent calls will return the same promise until it resolves.
|
|
214
|
-
*
|
|
215
|
-
* @param id The id of the item.
|
|
216
|
-
* @returns A promise that resolves to the result, whether it's cached or freshly fetched.
|
|
217
|
-
*/
|
|
218
|
-
get(id) {
|
|
219
|
-
const { item, key, isInvalid } = this._getCurrent(id);
|
|
220
|
-
// return cached item if it's not invalidated
|
|
221
|
-
if (item !== undefined && !isInvalid) {
|
|
222
|
-
this.logger.log(key, 'get: item resolved to', item, isInvalid ? '(invalidated)' : '');
|
|
223
|
-
return Promise.resolve(item);
|
|
224
|
-
}
|
|
225
|
-
let promise = this._fetchCache.get(key);
|
|
226
|
-
if (promise != null) {
|
|
227
|
-
this.logger.log(key, 'get: item resolved to <promise>');
|
|
228
|
-
return promise;
|
|
229
|
-
}
|
|
230
|
-
this.setStatus(key, true);
|
|
231
|
-
promise = this._doFetchAsync(id, key);
|
|
232
|
-
this.setPromise(key, promise);
|
|
233
|
-
return promise;
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Fetches the item asynchronously.
|
|
237
|
-
* @param id The id of the item.
|
|
238
|
-
* @param key The cache key.
|
|
239
|
-
* @returns A promise that resolves to the fetched item.
|
|
240
|
-
*/
|
|
241
|
-
_doFetchAsync = async (id, key) => {
|
|
242
|
-
let isInSameVersion = true;
|
|
243
|
-
try {
|
|
244
|
-
this.onBeforeFetch(key);
|
|
245
|
-
const v = this._version;
|
|
246
|
-
let res = await this.tryFetchInBatch(id);
|
|
247
|
-
if (v !== this._version) {
|
|
248
|
-
isInSameVersion = false;
|
|
249
|
-
// resolve with actual result but don't store it
|
|
250
|
-
return res;
|
|
251
|
-
}
|
|
252
|
-
if (this._fetchCache.get(key) != null) {
|
|
253
|
-
this.logger.log(key, 'item\'s <promise> resolved to', res);
|
|
254
|
-
res = this.prepareResult(res);
|
|
255
|
-
this.storeResult(key, res);
|
|
256
|
-
}
|
|
257
|
-
return res;
|
|
258
|
-
}
|
|
259
|
-
finally {
|
|
260
|
-
if (isInSameVersion) {
|
|
261
|
-
this.onFetchComplete(key);
|
|
262
|
-
}
|
|
263
|
-
else {
|
|
264
|
-
this.logger.log(key, 'skipping item\'s resolve due to version change ("clear()" has been called)');
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
/**
|
|
269
|
-
* Instantly invalidates the cached item for the specified id, like it was never fetched/accessed.
|
|
270
|
-
* @param id The id of the item.
|
|
271
|
-
*/
|
|
272
|
-
invalidate(id) {
|
|
273
|
-
const key = this._pk(id);
|
|
274
|
-
this._set(key, undefined, undefined, undefined);
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Updates the cached value for the specified id directly, like it was fetched already. Overrides existing value, if any.
|
|
278
|
-
* @param id The id of the item.
|
|
279
|
-
* @param value The new value to cache.
|
|
280
|
-
*/
|
|
281
|
-
updateValueDirectly(id, value) {
|
|
282
|
-
const key = this._pk(id);
|
|
283
|
-
this._set(key, value, undefined, undefined);
|
|
284
|
-
}
|
|
285
|
-
/** Returns true if the item is cached or fetching was initiated. Does not initiates fetching. */
|
|
286
|
-
hasKey(id) {
|
|
287
|
-
const key = this._pk(id);
|
|
288
|
-
return this._itemsCache.get(key) !== undefined || this._itemsStatus.get(key) !== undefined;
|
|
289
|
-
}
|
|
290
|
-
keys(iterate = false) {
|
|
291
|
-
const iterator = this._itemsCache.keys();
|
|
292
|
-
return iterate
|
|
293
|
-
? iterator
|
|
294
|
-
: Array.from(iterator);
|
|
295
|
-
}
|
|
296
|
-
keysParsed(iterate = false) {
|
|
297
|
-
const kp = this.keyParser;
|
|
298
|
-
if (!kp) {
|
|
299
|
-
return null;
|
|
300
|
-
}
|
|
301
|
-
const keysIterator = this.keys(true);
|
|
302
|
-
if (!iterate) {
|
|
303
|
-
return Array.from(keysIterator, key => kp(key));
|
|
304
|
-
}
|
|
305
|
-
return (function* () {
|
|
306
|
-
for (const key of keysIterator) {
|
|
307
|
-
yield kp(key);
|
|
308
|
-
}
|
|
309
|
-
})();
|
|
310
|
-
}
|
|
311
|
-
/** Clears the cache and resets the loading state. */
|
|
312
|
-
clear() {
|
|
313
|
-
++this._version;
|
|
314
|
-
this._loadingCount.value = 0;
|
|
315
|
-
this._batch?.clear();
|
|
316
|
-
this._itemsCache.clear();
|
|
317
|
-
this._itemsStatus.clear();
|
|
318
|
-
this._fetchCache.clear();
|
|
319
|
-
}
|
|
320
|
-
/** @internal updates all caches states at once. */
|
|
321
|
-
_set(key, item, promise, busy) {
|
|
322
|
-
_setX(key, this._fetchCache, promise);
|
|
323
|
-
_setX(key, this._itemsStatus, busy);
|
|
324
|
-
_setX(key, this._itemsCache, item);
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* Checks if the cached item for the specified key is invalidated (expired).
|
|
328
|
-
* @param key The cache key.
|
|
329
|
-
* @returns True if the item is invalidated, false otherwise.
|
|
330
|
-
*/
|
|
331
|
-
getIsInvalidated(key) {
|
|
332
|
-
if (!this._invalidationTimeMs) {
|
|
333
|
-
return false;
|
|
334
|
-
}
|
|
335
|
-
const ts = this._timestamps.get(key);
|
|
336
|
-
return ts != null && Date.now() - ts > this._invalidationTimeMs;
|
|
337
|
-
}
|
|
338
|
-
/** Updates the loading status for the specified key. Override to add a hook. */
|
|
339
|
-
setStatus(key, status) {
|
|
340
|
-
this.logger.log(key, 'status update:', status);
|
|
341
|
-
this._itemsStatus.set(key, status);
|
|
342
|
-
}
|
|
343
|
-
/** Updates the promise for the specified key. Override to add a hook. */
|
|
344
|
-
setPromise(key, promise) {
|
|
345
|
-
this._fetchCache.set(key, promise);
|
|
346
|
-
}
|
|
347
|
-
/** Stores the result for the specified key. Override to add a hook. */
|
|
348
|
-
storeResult(key, res) {
|
|
349
|
-
this._itemsCache.set(key, res);
|
|
350
|
-
this._timestamps.set(key, Date.now());
|
|
351
|
-
}
|
|
352
|
-
/** Hooks into the fetch process before it starts. */
|
|
353
|
-
onBeforeFetch(_key) {
|
|
354
|
-
this._loadingCount.value = this._loadingCount.value + 1;
|
|
355
|
-
}
|
|
356
|
-
/** Hooks into the fetch process after it completes. */
|
|
357
|
-
onFetchComplete(key) {
|
|
358
|
-
this._loadingCount.value = this._loadingCount.value - 1;
|
|
359
|
-
this._fetchCache.delete(key);
|
|
360
|
-
this._itemsStatus.set(key, false);
|
|
361
|
-
}
|
|
362
|
-
/** Hooks into the result preparation process, before it's stored into the cache. */
|
|
363
|
-
prepareResult(res) {
|
|
364
|
-
return res || null;
|
|
365
|
-
}
|
|
366
|
-
/** @pure Performs a fetch operation in batch mode if available, otherwise uses the regular fetch. */
|
|
367
|
-
async tryFetchInBatch(id) {
|
|
368
|
-
const fetchWrap = () => this.fetcher(id).catch(err => {
|
|
369
|
-
this.logger.warn('fetcher failed', id, err);
|
|
370
|
-
return null;
|
|
371
|
-
});
|
|
372
|
-
if (!this._batch) {
|
|
373
|
-
return fetchWrap();
|
|
374
|
-
}
|
|
375
|
-
const res = await this._batch.push(id)
|
|
376
|
-
.catch(err => {
|
|
377
|
-
this.logger.warn('batch fetch failed', id, err);
|
|
378
|
-
return null;
|
|
379
|
-
});
|
|
380
|
-
if (!res || !res.result || res.result[res.index] === undefined) {
|
|
381
|
-
// If we got to batch call, should we fallback to the direct one?
|
|
382
|
-
return fetchWrap();
|
|
383
|
-
}
|
|
384
|
-
return res.result[res.index];
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
function _setX(key, map, val) {
|
|
388
|
-
if (val === undefined) {
|
|
389
|
-
map.delete(key);
|
|
390
|
-
}
|
|
391
|
-
else {
|
|
392
|
-
map.set(key, val);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
//# sourceMappingURL=promiseCache.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"promiseCache.js","sourceRoot":"","sources":["../../../src/structures/promiseCache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAe3C,MAAM,KAAW,cAAc,CAS9B;AATD,WA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gB,EAAE,EAAE,EAAE,GAAG,CAAC,CAAC;YAC5C,OAAO,IAAI,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACf,OAAO,SAAS,EAAE,CAAC;QACvB,CAAC;QAED,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;aACjC,KAAK,CAAC,GAAG,CAAC,EAAE;YACT,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,EAAE,GAAG,CAAC,CAAC;YAChD,OAAO,IAAI,CAAC;QAChB,CAAC,CAAC,CAAC;QACP,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,SAAS,EAAE,CAAC;YAC7D,iEAAiE;YACjE,OAAO,SAAS,EAAE,CAAC;QACvB,CAAC;QAED,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;CACJ;AAED,SAAS,KAAK,CAAI,GAAW,EAAE,GAAyB,EAAE,GAAM;IAC5D,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACpB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC;SAAM,CAAC;QACJ,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACtB,CAAC;AACL,CAAC"}
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import { Loggable } from '../logger/loggable.js';
|
|
2
|
-
import type { IMapModel, IValueModel } from '../models/types.js';
|
|
3
|
-
/** Represents a state of a cached item. Holds a references to an actual state. */
|
|
4
|
-
export type DeferredGetter<T> = {
|
|
5
|
-
/** Get current resolved value, if any, or initiates fetching. */
|
|
6
|
-
readonly current: T | null | undefined;
|
|
7
|
-
/** Returns a promise that resolves to the current or fetching value. */
|
|
8
|
-
readonly promise: Promise<T | null>;
|
|
9
|
-
/** Returns true if the item is currently being fetched. Returns undefined if fetching has not started yet. */
|
|
10
|
-
readonly isLoading: boolean | undefined;
|
|
11
|
-
};
|
|
12
|
-
export declare namespace DeferredGetter {
|
|
13
|
-
/** Empty resolved value. */
|
|
14
|
-
const Empty: {
|
|
15
|
-
readonly current: null;
|
|
16
|
-
readonly promise: Promise<null>;
|
|
17
|
-
readonly isLoading: boolean;
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Caches items by a key (string or another type) which are resolved by an async fetcher (`Promise`).
|
|
22
|
-
*
|
|
23
|
-
* Supports:
|
|
24
|
-
* - custom key adapter and parser for non-string keys.
|
|
25
|
-
* - direct manual cache manipulation.
|
|
26
|
-
* - batching of fetches.
|
|
27
|
-
* - auto-invalidation of cached items.
|
|
28
|
-
*/
|
|
29
|
-
export declare class PromiseCache<T, K = string> extends Loggable {
|
|
30
|
-
private readonly fetcher;
|
|
31
|
-
private readonly keyAdapter?;
|
|
32
|
-
private readonly keyParser?;
|
|
33
|
-
/** Stores resolved items in map by id. */
|
|
34
|
-
protected readonly _itemsCache: IMapModel<string, T | null | undefined>;
|
|
35
|
-
/** Stores items loading state (loading or not) in map by id. */
|
|
36
|
-
protected readonly _itemsStatus: IMapModel<string, boolean>;
|
|
37
|
-
/** Stores items loading count. */
|
|
38
|
-
protected readonly _loadingCount: IValueModel<number>;
|
|
39
|
-
/** Stores items Promises state (if still loading) in map by id. */
|
|
40
|
-
private readonly _fetchCache;
|
|
41
|
-
/** Stores items resolve timestamps (for expiration) in map by id. */
|
|
42
|
-
private readonly _timestamps;
|
|
43
|
-
private _batch;
|
|
44
|
-
private _invalidationTimeMs;
|
|
45
|
-
private _keepInstanceDuringInvalidation;
|
|
46
|
-
private _version;
|
|
47
|
-
/**
|
|
48
|
-
* Creates an instance of PromiseCache.
|
|
49
|
-
* @param fetcher Function to fetch data by key.
|
|
50
|
-
* @param keyAdapter Optional function to adapt non-string keys to strings.
|
|
51
|
-
* @param keyParser Optional function to parse string keys back to their original type.
|
|
52
|
-
*/
|
|
53
|
-
constructor(fetcher: (id: K) => Promise<T>, keyAdapter?: (K extends string ? null : (k: K) => string) | undefined, keyParser?: (K extends string ? null : (id: string) => K) | undefined);
|
|
54
|
-
get busyCount(): number;
|
|
55
|
-
/**
|
|
56
|
-
* @pure @const
|
|
57
|
-
* Creates a model for tracking the loading state. Override to inject own instance, e.g. for observability.
|
|
58
|
-
*
|
|
59
|
-
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
60
|
-
*
|
|
61
|
-
* @returns A value model for the loading count.
|
|
62
|
-
*/
|
|
63
|
-
protected pure_createBusyCount(): IValueModel<number>;
|
|
64
|
-
/**
|
|
65
|
-
* @pure @const
|
|
66
|
-
* Creates a map for caching resolved items by id. Override to inject own instance, e.g. for observability.
|
|
67
|
-
*
|
|
68
|
-
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
69
|
-
*
|
|
70
|
-
* @returns A map model for the items cache.
|
|
71
|
-
*/
|
|
72
|
-
protected pure_createItemsCache(): IMapModel<string, T | null | undefined>;
|
|
73
|
-
/**
|
|
74
|
-
* @pure @const
|
|
75
|
-
* Creates a map for tracking the loading state of items by id. Override to inject own instance, e.g. for observability.
|
|
76
|
-
*
|
|
77
|
-
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
78
|
-
*
|
|
79
|
-
* @returns A map model for the items loading state.
|
|
80
|
-
*/
|
|
81
|
-
protected pure_createItemsStatus(): IMapModel<string, boolean>;
|
|
82
|
-
/**
|
|
83
|
-
* @pure @const
|
|
84
|
-
* Creates a map for caching promises of items by id. Override to inject own instance, e.g. for observability.
|
|
85
|
-
*
|
|
86
|
-
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
87
|
-
*
|
|
88
|
-
* @returns A map model for the items promises cache.
|
|
89
|
-
*/
|
|
90
|
-
protected pure_createFetchCache(): IMapModel<string, Promise<T | null>>;
|
|
91
|
-
private _pk;
|
|
92
|
-
/**
|
|
93
|
-
* Creates a logger name for this instance.
|
|
94
|
-
* @param name The name of the cache instance.
|
|
95
|
-
* @returns The logger name.
|
|
96
|
-
*/
|
|
97
|
-
protected getLoggerName(name: string | undefined): string;
|
|
98
|
-
/**
|
|
99
|
-
* Provide a fetcher function that takes multiple ids and returns multiple results at once. Will be called with a slight delay to allow multiple ids to be collected.
|
|
100
|
-
*
|
|
101
|
-
* Warning: resolved array should have the same order as the input array.
|
|
102
|
-
*
|
|
103
|
-
* When provided, effectively replaces the main fetcher; but in case of fail, fallbacks to the main fetcher.
|
|
104
|
-
*/
|
|
105
|
-
useBatching(fetcher: (ids: K[]) => Promise<T[]>, delay?: number): this;
|
|
106
|
-
/**
|
|
107
|
-
* Enables auto-invalidation of cached items.
|
|
108
|
-
*
|
|
109
|
-
* @param ms Time in milliseconds after which the item will be considered invalid. If null, auto-invalidation is disabled.
|
|
110
|
-
* @param keepInstance If true, the cached item will not be removed during invalidation, but will be set to `undefined` instead. Defaults to false.
|
|
111
|
-
*/
|
|
112
|
-
useInvalidationTime(ms: number | null, keepInstance?: boolean): this;
|
|
113
|
-
/**
|
|
114
|
-
* Returns a {@link DeferredGetter} object for a specfied key.
|
|
115
|
-
*
|
|
116
|
-
* This can be used to access the current value, promise, and loading state of the item.
|
|
117
|
-
*
|
|
118
|
-
* @param key The key of the item.
|
|
119
|
-
*/
|
|
120
|
-
getDeferred(key: K): DeferredGetter<T>;
|
|
121
|
-
/**
|
|
122
|
-
* Returns the loading state of an item.
|
|
123
|
-
*
|
|
124
|
-
* @param id The id of the item.
|
|
125
|
-
* @returns The loading state of the item: true if loading, false if loading completed, undefined if loading was not started yet for the specified key.
|
|
126
|
-
*/
|
|
127
|
-
getIsBusy(id: K): boolean | undefined;
|
|
128
|
-
/**
|
|
129
|
-
* Returns the current cached value for the specified key, without triggering a fetch.
|
|
130
|
-
* @param id The id of the item.
|
|
131
|
-
* @returns The current cached value of the item.
|
|
132
|
-
*/
|
|
133
|
-
protected _getCurrent(id: K): {
|
|
134
|
-
item: T | null | undefined;
|
|
135
|
-
key: string;
|
|
136
|
-
isInvalid: boolean;
|
|
137
|
-
};
|
|
138
|
-
/**
|
|
139
|
-
* Returns the current cached value for the specified key, optionally triggering a fetch.
|
|
140
|
-
* @param id The id of the item.
|
|
141
|
-
* @param initiateFetch If true, will initiate a fetch if the item is not cached.
|
|
142
|
-
* @returns The current cached value of the item.
|
|
143
|
-
*/
|
|
144
|
-
getCurrent(id: K, initiateFetch?: boolean): T | null | undefined;
|
|
145
|
-
/**
|
|
146
|
-
* Returns a promise that resolves to the cached value of the item if loaded already, otherwise starts fetching and the promise will be resolved to the final value.
|
|
147
|
-
*
|
|
148
|
-
* Consequent calls will return the same promise until it resolves.
|
|
149
|
-
*
|
|
150
|
-
* @param id The id of the item.
|
|
151
|
-
* @returns A promise that resolves to the result, whether it's cached or freshly fetched.
|
|
152
|
-
*/
|
|
153
|
-
get(id: K): Promise<T | null>;
|
|
154
|
-
/**
|
|
155
|
-
* Fetches the item asynchronously.
|
|
156
|
-
* @param id The id of the item.
|
|
157
|
-
* @param key The cache key.
|
|
158
|
-
* @returns A promise that resolves to the fetched item.
|
|
159
|
-
*/
|
|
160
|
-
protected _doFetchAsync: (id: K, key: string) => Promise<T | null>;
|
|
161
|
-
/**
|
|
162
|
-
* Instantly invalidates the cached item for the specified id, like it was never fetched/accessed.
|
|
163
|
-
* @param id The id of the item.
|
|
164
|
-
*/
|
|
165
|
-
invalidate(id: K): void;
|
|
166
|
-
/**
|
|
167
|
-
* Updates the cached value for the specified id directly, like it was fetched already. Overrides existing value, if any.
|
|
168
|
-
* @param id The id of the item.
|
|
169
|
-
* @param value The new value to cache.
|
|
170
|
-
*/
|
|
171
|
-
updateValueDirectly(id: K, value: T | null): void;
|
|
172
|
-
/** Returns true if the item is cached or fetching was initiated. Does not initiates fetching. */
|
|
173
|
-
hasKey(id: K): boolean;
|
|
174
|
-
/** Returns an iterator over the keys of the cached items. */
|
|
175
|
-
keys(iterate: true): MapIterator<string>;
|
|
176
|
-
/** Returns an array of the keys of the cached items. */
|
|
177
|
-
keys(): string[];
|
|
178
|
-
/** Returns an iterator over the parsed keys of the cached items. */
|
|
179
|
-
keysParsed(iterate: true): Generator<K> | null;
|
|
180
|
-
/** Returns an array of the parsed keys of the cached items. */
|
|
181
|
-
keysParsed(): K[] | null;
|
|
182
|
-
/** Clears the cache and resets the loading state. */
|
|
183
|
-
clear(): void;
|
|
184
|
-
/** @internal updates all caches states at once. */
|
|
185
|
-
protected _set(key: string, item: T | null | undefined, promise: Promise<T> | undefined, busy: boolean | undefined): void;
|
|
186
|
-
/**
|
|
187
|
-
* Checks if the cached item for the specified key is invalidated (expired).
|
|
188
|
-
* @param key The cache key.
|
|
189
|
-
* @returns True if the item is invalidated, false otherwise.
|
|
190
|
-
*/
|
|
191
|
-
protected getIsInvalidated(key: string): boolean;
|
|
192
|
-
/** Updates the loading status for the specified key. Override to add a hook. */
|
|
193
|
-
protected setStatus(key: string, status: boolean): void;
|
|
194
|
-
/** Updates the promise for the specified key. Override to add a hook. */
|
|
195
|
-
protected setPromise(key: string, promise: Promise<T | null>): void;
|
|
196
|
-
/** Stores the result for the specified key. Override to add a hook. */
|
|
197
|
-
protected storeResult(key: string, res: T | null): void;
|
|
198
|
-
/** Hooks into the fetch process before it starts. */
|
|
199
|
-
protected onBeforeFetch(_key: string): void;
|
|
200
|
-
/** Hooks into the fetch process after it completes. */
|
|
201
|
-
protected onFetchComplete(key: string): void;
|
|
202
|
-
/** Hooks into the result preparation process, before it's stored into the cache. */
|
|
203
|
-
protected prepareResult(res: T | null): NonNullable<T> | null;
|
|
204
|
-
/** @pure Performs a fetch operation in batch mode if available, otherwise uses the regular fetch. */
|
|
205
|
-
protected tryFetchInBatch(id: K): Promise<T | null>;
|
|
206
|
-
}
|