@zajno/common 2.8.9 → 2.8.10
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/README.md +1 -1
- package/cjs/lazy/promise.js +20 -4
- package/cjs/lazy/promise.js.map +1 -1
- package/cjs/structures/promiseCache/cache.js +112 -26
- package/cjs/structures/promiseCache/cache.js.map +1 -1
- package/cjs/structures/promiseCache/core.js +50 -13
- package/cjs/structures/promiseCache/core.js.map +1 -1
- package/esm/lazy/promise.js +20 -4
- package/esm/lazy/promise.js.map +1 -1
- package/esm/structures/promiseCache/cache.js +112 -26
- package/esm/structures/promiseCache/cache.js.map +1 -1
- package/esm/structures/promiseCache/core.js +50 -13
- package/esm/structures/promiseCache/core.js.map +1 -1
- package/package.json +1 -1
- package/types/lazy/promise.d.ts +2 -2
- package/types/lazy/types.d.ts +10 -4
- package/types/structures/promiseCache/cache.d.ts +54 -14
- package/types/structures/promiseCache/core.d.ts +50 -13
- package/types/structures/promiseCache/types.d.ts +17 -1
- package/types/structures/tempoCache.d.ts +1 -1
package/README.md
CHANGED
|
@@ -51,7 +51,7 @@ The motivation to have this – just to control and organize some shared code th
|
|
|
51
51
|
* [Structures](./src/structures)
|
|
52
52
|
- [`Path`](./src/structures/path/index.ts): a helper to build (almost) type-safe dynamic paths (useful for routing definitions);
|
|
53
53
|
- [`Queues`](./src/structures/queue/): [`ParallelQueue`](./src/structures/queue/parallel.ts) (helps run & track multiple prioritized async tasks), [`TasksQueue`](./src/structures/queue/tasks.ts) (a simple queue for async tasks with a limit for concurrent tasks);
|
|
54
|
-
- [`PromiseCache`](./src/structures/promiseCache.
|
|
54
|
+
- [`PromiseCache`](./src/structures/promiseCache/README.md): a cache for Promise'd values; supports TTL, invalidation, stale-while-revalidate refresh, custom keys, deferred getters and more;
|
|
55
55
|
- [`PromiseExtended`](./src/structures/promiseExtended.ts): a Promise wrapper which never rejects but provides `onSuccess` and `onError` callbacks;
|
|
56
56
|
- [`PromiseProxy`](./src/structures/promiseProxy.ts): a Promise wrapper to mimic (fake) a resolved value's properties until it's actually resolved;
|
|
57
57
|
- [`Enum helpers`](./src/structures/helpers/enum.ts): `EnumHelper`, `EnumStringHelper`, `EnumBitwiseHelper`;
|
package/cjs/lazy/promise.js
CHANGED
|
@@ -170,7 +170,9 @@ class LazyPromise {
|
|
|
170
170
|
}
|
|
171
171
|
if (nextState !== undefined) {
|
|
172
172
|
if (this._isAsyncStateChange) {
|
|
173
|
-
|
|
173
|
+
Promise.resolve().then(() => {
|
|
174
|
+
this.updateState(nextState);
|
|
175
|
+
});
|
|
174
176
|
}
|
|
175
177
|
else {
|
|
176
178
|
this.updateState(nextState);
|
|
@@ -182,7 +184,16 @@ class LazyPromise {
|
|
|
182
184
|
// Case when refreshing already is happening - we have an active promise
|
|
183
185
|
return;
|
|
184
186
|
}
|
|
185
|
-
|
|
187
|
+
let factoryResult;
|
|
188
|
+
try {
|
|
189
|
+
factoryResult = this._factory(refreshing);
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
// Re-throwing the original error from the synchronous factory call
|
|
193
|
+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
194
|
+
factoryResult = Promise.reject(err);
|
|
195
|
+
}
|
|
196
|
+
const factoryPromise = Promise.resolve(factoryResult)
|
|
186
197
|
.then(res => {
|
|
187
198
|
if (!this._activeFactoryPromise) {
|
|
188
199
|
// this promise was abandoned: was superseded or reset called
|
|
@@ -201,10 +212,15 @@ class LazyPromise {
|
|
|
201
212
|
return this._activeFactoryPromise;
|
|
202
213
|
})
|
|
203
214
|
.catch(err => {
|
|
204
|
-
if (!this._activeFactoryPromise
|
|
215
|
+
if (!this._activeFactoryPromise) {
|
|
216
|
+
// Abandoned (reset/dispose was called) — don't corrupt state
|
|
217
|
+
return this._instance ?? this._initial;
|
|
218
|
+
}
|
|
219
|
+
if (this._activeFactoryPromise === factoryPromise) {
|
|
205
220
|
return this.onRejected(err);
|
|
206
221
|
}
|
|
207
|
-
|
|
222
|
+
// Stale promise — delegate to the latest active promise instead of re-throwing
|
|
223
|
+
return this._activeFactoryPromise;
|
|
208
224
|
});
|
|
209
225
|
const hadActive = !!this._activeFactoryPromise;
|
|
210
226
|
// This is now the active promise - any previous one is superseded
|
package/cjs/lazy/promise.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise.js","sourceRoot":"","sources":["../../../src/lazy/promise.ts"],"names":[],"mappings":";;;AAAA,0DAAwE;AACxE,kDAAmD;AAKnD;;;;GAIG;AACH,MAAa,WAAW;IAEZ,QAAQ,CAAiB;IAChB,QAAQ,CAAW;IAE5B,SAAS,CAAe;IAEhC,gFAAgF;IACxE,MAAM,GAAmB,IAAI,CAAC;IAE9B,mBAAmB,GAAG,KAAK,CAAC;IAE5B,QAAQ,
|
|
1
|
+
{"version":3,"file":"promise.js","sourceRoot":"","sources":["../../../src/lazy/promise.ts"],"names":[],"mappings":";;;AAAA,0DAAwE;AACxE,kDAAmD;AAKnD;;;;GAIG;AACH,MAAa,WAAW;IAEZ,QAAQ,CAAiB;IAChB,QAAQ,CAAW;IAE5B,SAAS,CAAe;IAEhC,gFAAgF;IACxE,MAAM,GAAmB,IAAI,CAAC;IAE9B,mBAAmB,GAAG,KAAK,CAAC;IAE5B,QAAQ,CAAoC;IAC5C,cAAc,CAA6B;IAEnD,8DAA8D;IACtD,qBAAqB,GAAiC,IAAI,CAAC;IAC3D,MAAM,GAAY,IAAI,CAAC;IAEvB,YAAY,CAAc;IAElC,YACI,OAAuB,EACvB,OAAkB;QAElB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,QAAQ,GAAG,OAAmB,CAAC;QAEpC,IAAI,CAAC,SAAS,GAAG,OAAuB,CAAC,CAAC,8BAA8B;IAC5E,CAAC;IAED,gFAAgF;IAChF,IAAW,SAAS,KAAK,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9C,IAAW,QA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qB,CAAC;QACtC,CAAC,CAAC,CAAC;QAEP,MAAM,SAAS,GAAG,CAAC,CAAC,IAAI,CAAC,qBAAqB,CAAC;QAE/C,kEAAkE;QAClE,IAAI,CAAC,qBAAqB,GAAG,cAAc,CAAC;QAE5C,2DAA2D;QAC3D,yDAAyD;QACzD,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,CAAC,SAAS,EAAE,CAAC;YAC/B,IAAI,CAAC,QAAQ,GAAG,cAAc,CAAC;QACnC,CAAC;IACL,CAAC;IAES,UAAU,CAAC,CAAU;QAC3B,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QACxB,8DAA8D;QAC9D,kDAAkD;QAClD,MAAM,eAAe,GAAG,IAAI,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC;QACtF,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC;QACjD,IAAI,CAAC,qBAAqB,GAAG,IAAI,CAAC;QAClC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QACjB,OAAO,eAAe,CAAC;IAC3B,CAAC;IAES,WAAW,CAAC,SAAyB;QAC3C,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;IAC5B,CAAC;IAES,QAAQ,CAAC,GAAY;QAC3B,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;IACtB,CAAC;IAES,UAAU;QAChB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;IACvB,CAAC;CACJ;AArSD,kCAqSC"}
|
|
@@ -19,6 +19,7 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
19
19
|
_batch = null;
|
|
20
20
|
_invalidationConfig = null;
|
|
21
21
|
_onError = null;
|
|
22
|
+
_initialValueFactory = null;
|
|
22
23
|
/**
|
|
23
24
|
* Creates an instance of PromiseCache.
|
|
24
25
|
* @param fetcher Function to fetch data by key.
|
|
@@ -47,10 +48,12 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
47
48
|
* This is a convenience wrapper around {@link useInvalidation}.
|
|
48
49
|
*
|
|
49
50
|
* @param ms Time in milliseconds after which the item will be considered invalid. If null, auto-invalidation is disabled.
|
|
50
|
-
*
|
|
51
|
+
*
|
|
52
|
+
* @deprecated The `keepInstance` parameter is deprecated and ignored — stale values are now always kept during invalidation.
|
|
53
|
+
* Use `invalidate()` followed by `get()` if you need to clear the stale value before re-fetching.
|
|
51
54
|
*/
|
|
52
|
-
useInvalidationTime(ms,
|
|
53
|
-
return this.useInvalidation(ms != null ? { expirationMs: ms
|
|
55
|
+
useInvalidationTime(ms, _keepInstance) {
|
|
56
|
+
return this.useInvalidation(ms != null ? { expirationMs: ms } : null);
|
|
54
57
|
}
|
|
55
58
|
/**
|
|
56
59
|
* Configures advanced invalidation policy.
|
|
@@ -73,6 +76,25 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
73
76
|
this._onError = callback;
|
|
74
77
|
return this;
|
|
75
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Sets a default/initial value returned before the fetch completes or on error when no stale value exists.
|
|
81
|
+
*
|
|
82
|
+
* Accepts either a static value or a per-key factory function `(key: K) => TInitial`.
|
|
83
|
+
* The value is **not** stored in the cache — it's a synthetic default (same as `LazyPromise`'s initial value).
|
|
84
|
+
*
|
|
85
|
+
* **Note:** Functions are always interpreted as factories. If `T` is a function type,
|
|
86
|
+
* wrap it: `useInitialValue((key) => myFallbackFn)`.
|
|
87
|
+
*
|
|
88
|
+
* @param initial A value (non-function) or `(key: K) => TInitial` factory.
|
|
89
|
+
* @returns `this` for chaining.
|
|
90
|
+
*/
|
|
91
|
+
useInitialValue(initial) {
|
|
92
|
+
const self = this;
|
|
93
|
+
self._initialValueFactory = typeof initial === 'function'
|
|
94
|
+
? initial
|
|
95
|
+
: (_key) => initial;
|
|
96
|
+
return self;
|
|
97
|
+
}
|
|
76
98
|
// ─── Core implementation ─────────────────────────────────────────────
|
|
77
99
|
/**
|
|
78
100
|
* 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.
|
|
@@ -86,16 +108,37 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
86
108
|
const { item, key, isInvalid } = this._getCurrent(id);
|
|
87
109
|
// return cached item if it's not invalidated
|
|
88
110
|
if (item !== undefined && !isInvalid) {
|
|
89
|
-
this.logger.log(key, 'get: item resolved to', item
|
|
111
|
+
this.logger.log(key, 'get: item resolved to', item);
|
|
90
112
|
return Promise.resolve(item);
|
|
91
113
|
}
|
|
114
|
+
// Join an existing in-flight fetch/refresh if one exists
|
|
92
115
|
let promise = this._fetchCache.get(key);
|
|
93
116
|
if (promise != null) {
|
|
94
117
|
this.logger.log(key, 'get: item resolved to <promise>');
|
|
95
118
|
return promise;
|
|
96
119
|
}
|
|
97
120
|
this.setStatus(key, true);
|
|
98
|
-
promise = this._doFetchAsync(id, key);
|
|
121
|
+
promise = this._doFetchAsync(id, key, false);
|
|
122
|
+
this.setPromise(key, promise);
|
|
123
|
+
return promise;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Re-fetches the value for the specified key while keeping the stale cached value available.
|
|
127
|
+
*
|
|
128
|
+
* Does not change the loading status — consumers reading `getCurrent()` / `getLazy().value`
|
|
129
|
+
* continue to see the stale value as if nothing happened.
|
|
130
|
+
*
|
|
131
|
+
* Implements "latest wins" concurrency: if multiple refreshes are called concurrently,
|
|
132
|
+
* all promises resolve to the value from the latest refresh.
|
|
133
|
+
*
|
|
134
|
+
* On error, the stale value is preserved and the error is stored.
|
|
135
|
+
*
|
|
136
|
+
* @param id The key of the item to refresh.
|
|
137
|
+
* @returns A promise resolving to the refreshed value, or the stale value on error.
|
|
138
|
+
*/
|
|
139
|
+
refresh(id) {
|
|
140
|
+
const key = this._pk(id);
|
|
141
|
+
const promise = this._doFetchAsync(id, key, true);
|
|
99
142
|
this.setPromise(key, promise);
|
|
100
143
|
return promise;
|
|
101
144
|
}
|
|
@@ -105,17 +148,21 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
105
148
|
super.clear();
|
|
106
149
|
}
|
|
107
150
|
// ─── Protected overrides ─────────────────────────────────────────────
|
|
151
|
+
_getInitialValue(id) {
|
|
152
|
+
return this._initialValueFactory ? this._initialValueFactory(id) : undefined;
|
|
153
|
+
}
|
|
108
154
|
_getCurrent(id) {
|
|
109
155
|
const key = this._pk(id);
|
|
110
156
|
const isInvalid = this.getIsInvalidated(key);
|
|
111
157
|
// make sure current item is hooked here from the cache (required by observers)
|
|
112
158
|
const item = this._itemsCache.get(key);
|
|
113
|
-
const keepInstance = !!this._invalidationConfig?.keepInstance;
|
|
114
159
|
if (isInvalid) {
|
|
115
160
|
this.logger.log(key, 'item is invalidated');
|
|
116
161
|
}
|
|
117
162
|
return {
|
|
118
|
-
|
|
163
|
+
// Always keep the stale value visible — stale-while-revalidate by default.
|
|
164
|
+
// Use `invalidate()` + `get()` to clear the stale value before re-fetching.
|
|
165
|
+
item,
|
|
119
166
|
key,
|
|
120
167
|
isInvalid,
|
|
121
168
|
};
|
|
@@ -149,20 +196,29 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
149
196
|
}
|
|
150
197
|
// ─── Private ─────────────────────────────────────────────────────────
|
|
151
198
|
/**
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
199
|
+
* Unified fetch method with "latest wins" semantics.
|
|
200
|
+
*
|
|
201
|
+
* - Tracks the active factory promise per key via `_activeFetchPromises`.
|
|
202
|
+
* - If superseded by a newer fetch, delegates to the newer promise.
|
|
203
|
+
* - On error, preserves the stale cached value.
|
|
204
|
+
*
|
|
205
|
+
* @param id The original key.
|
|
206
|
+
* @param key The string cache key.
|
|
207
|
+
* @returns A promise resolving to the fetched/refreshed value, or the stale value on error.
|
|
156
208
|
*/
|
|
157
|
-
async _doFetchAsync(id, key) {
|
|
209
|
+
async _doFetchAsync(id, key, refreshing) {
|
|
158
210
|
let isInSameVersion = true;
|
|
211
|
+
let isLatest = false;
|
|
159
212
|
try {
|
|
160
213
|
this.onBeforeFetch(key);
|
|
161
214
|
const v = this._version;
|
|
215
|
+
// Create the factory promise and mark it as the active one for this key (latest wins)
|
|
216
|
+
const factoryPromise = this.tryFetchInBatch(id, refreshing);
|
|
217
|
+
this._activeFetchPromises.set(key, factoryPromise);
|
|
162
218
|
let res;
|
|
163
219
|
let fetchFailed = false;
|
|
164
220
|
try {
|
|
165
|
-
res = await
|
|
221
|
+
res = await factoryPromise;
|
|
166
222
|
}
|
|
167
223
|
catch (err) {
|
|
168
224
|
this._handleError(id, err);
|
|
@@ -171,31 +227,56 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
171
227
|
}
|
|
172
228
|
if (v !== this._version) {
|
|
173
229
|
isInSameVersion = false;
|
|
230
|
+
this._activeFetchPromises.delete(key);
|
|
174
231
|
// resolve with actual result but don't store it
|
|
175
|
-
return res;
|
|
232
|
+
return res ?? this._getInitialValue(id);
|
|
176
233
|
}
|
|
177
|
-
if
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
234
|
+
// Check if this is still the active (latest) fetch for this key
|
|
235
|
+
isLatest = this._activeFetchPromises.get(key) === factoryPromise;
|
|
236
|
+
if (!isLatest) {
|
|
237
|
+
// Superseded by a newer refresh/fetch — delegate to the latest public promise.
|
|
238
|
+
// This ensures anyone awaiting this old promise gets the fresh value,
|
|
239
|
+
// mirroring LazyPromise's "latest wins" behavior.
|
|
240
|
+
const newerPromise = this._fetchCache.get(key);
|
|
241
|
+
if (newerPromise) {
|
|
242
|
+
// Catch errors from the newer promise — if it fails, fall back to stale/initial value.
|
|
243
|
+
return newerPromise.catch(() => this._itemsCache.get(key) ?? this._getInitialValue(id));
|
|
182
244
|
}
|
|
245
|
+
// Fallback: return current cached value or initial
|
|
246
|
+
return this._itemsCache.get(key) ?? this._getInitialValue(id);
|
|
183
247
|
}
|
|
184
|
-
|
|
248
|
+
// We are the latest — clean up tracking
|
|
249
|
+
this._activeFetchPromises.delete(key);
|
|
250
|
+
if (!fetchFailed && res !== undefined) {
|
|
251
|
+
this.logger.log(key, 'item\'s <promise> resolved to', res);
|
|
252
|
+
res = this.prepareResult(res);
|
|
253
|
+
this.storeResult(key, res);
|
|
254
|
+
}
|
|
255
|
+
else if (fetchFailed) {
|
|
256
|
+
// Keep stale value — return whatever is in cache, or initial value
|
|
257
|
+
return this._itemsCache.get(key) ?? this._getInitialValue(id);
|
|
258
|
+
}
|
|
259
|
+
return res ?? this._getInitialValue(id);
|
|
185
260
|
}
|
|
186
261
|
finally {
|
|
187
|
-
if (isInSameVersion) {
|
|
262
|
+
if (!isInSameVersion) {
|
|
263
|
+
this.logger.log(key, 'skipping item\'s resolve due to version change ("clear()" has been called)');
|
|
264
|
+
}
|
|
265
|
+
else if (isLatest) {
|
|
266
|
+
// Only the latest fetch should clean up the fetch state.
|
|
267
|
+
// Superseded fetches delegate to the latest and should not
|
|
268
|
+
// prematurely clear the fetch cache or loading status.
|
|
188
269
|
this.onFetchComplete(key);
|
|
189
270
|
}
|
|
190
271
|
else {
|
|
191
|
-
this.
|
|
272
|
+
this.onFetchSuperseded(key);
|
|
192
273
|
}
|
|
193
274
|
}
|
|
194
275
|
}
|
|
195
276
|
/** Performs a fetch operation in batch mode if available, otherwise uses the regular fetch. Throws on error. */
|
|
196
|
-
async tryFetchInBatch(id) {
|
|
277
|
+
async tryFetchInBatch(id, refreshing) {
|
|
197
278
|
if (!this._batch) {
|
|
198
|
-
return this.fetcher(id);
|
|
279
|
+
return this.fetcher(id, refreshing);
|
|
199
280
|
}
|
|
200
281
|
const res = await this._batch.push(id)
|
|
201
282
|
.catch(err => {
|
|
@@ -204,7 +285,7 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
204
285
|
});
|
|
205
286
|
if (!res || !res.result || res.result[res.index] === undefined) {
|
|
206
287
|
// batch call failed or returned no result — fallback to the direct fetcher
|
|
207
|
-
return this.fetcher(id);
|
|
288
|
+
return this.fetcher(id, refreshing);
|
|
208
289
|
}
|
|
209
290
|
return res.result[res.index];
|
|
210
291
|
}
|
|
@@ -224,9 +305,12 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
224
305
|
}
|
|
225
306
|
/**
|
|
226
307
|
* Enforces the max items limit by removing items to make room.
|
|
227
|
-
* Strategy: first removes invalid items, then oldest valid items.
|
|
308
|
+
* Strategy: first removes invalid items, then oldest valid items by timestamp.
|
|
228
309
|
* Items currently being fetched (in-flight) are not evicted.
|
|
229
310
|
*
|
|
311
|
+
* Note: Phase 2 scans all timestamps linearly (O(n) per eviction).
|
|
312
|
+
* This is acceptable for typical `maxItems` values (up to ~1000).
|
|
313
|
+
*
|
|
230
314
|
* @param incomingKey The key of the item about to be stored (excluded from eviction).
|
|
231
315
|
*/
|
|
232
316
|
_enforceMaxItems(incomingKey) {
|
|
@@ -251,6 +335,7 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
251
335
|
this._set(key, undefined, undefined, undefined);
|
|
252
336
|
this._errorsMap.delete(key);
|
|
253
337
|
this._timestamps.delete(key);
|
|
338
|
+
this._activeFetchPromises.delete(key);
|
|
254
339
|
if (this._itemsCache.size < maxItems) {
|
|
255
340
|
return;
|
|
256
341
|
}
|
|
@@ -274,6 +359,7 @@ class PromiseCache extends core_js_1.PromiseCacheCore {
|
|
|
274
359
|
this._set(oldestKey, undefined, undefined, undefined);
|
|
275
360
|
this._timestamps.delete(oldestKey);
|
|
276
361
|
this._errorsMap.delete(oldestKey);
|
|
362
|
+
this._activeFetchPromises.delete(oldestKey);
|
|
277
363
|
}
|
|
278
364
|
else {
|
|
279
365
|
// No evictable items found (all are in-flight or incoming)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../../../src/structures/promiseCache/cache.ts"],"names":[],"mappings":";;;AAAA,6DAAgE;AAChE,uCAA6C;AAG7C,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B;;;;;;;;;EASE;AACF,MAAa,
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../../../src/structures/promiseCache/cache.ts"],"names":[],"mappings":";;;AAAA,6DAAgE;AAChE,uCAA6C;AAG7C,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B;;;;;;;;;EASE;AACF,MAAa,YAAwE,SAAQ,0BAAgC;IAcpG;IAZb,MAAM,GAAqC,IAAI,CAAC;IAChD,mBAAmB,GAAiC,IAAI,CAAC;IACzD,QAAQ,GAA4B,IAAI,CAAC;IACzC,oBAAoB,GAAkC,IAAI,CAAC;IAEnE;;;;;OAKG;IACH,YACqB,OAAkC,EACnD,UAAsC,EACtC,SAAoC;QAEpC,KAAK,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;QAJZ,YAAO,GAAP,OAAO,CAA2B;IAKvD,CAAC;IAED,wEAAwE;IAExE;;;;;;MAME;IACF,WAAW,CAAC,OAAmC,EAAE,KAAK,GAAG,cAAc;QACnE,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,+BAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QACrE,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;;;;;;;;MASE;IACF,mBAAmB,CAAC,EAAiB,EAAE,aAAuB;QAC1D,OAAO,IAAI,CAAC,eAAe,CAAC,EAAE,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC1E,CAAC;IAED;;;;;;;OAOG;IACH,eAAe,CAAC,MAAoC;QAChD,IAAI,CAAC,mBAAmB,GAAG,MAAM,CAAC;QAClC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAC,QAA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QAAQ,GAAG,EAAE,CAAC;oBACd,SAAS,GAAG,GAAG,CAAC;gBACpB,CAAC;YACL,CAAC;YAED,IAAI,SAAS,IAAI,IAAI,EAAE,CAAC;gBACpB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;gBACtD,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;gBACnC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;gBAClC,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YAChD,CAAC;iBAAM,CAAC;gBACJ,2DAA2D;gBAC3D,MAAM;YACV,CAAC;QACL,CAAC;IACL,CAAC;CACJ;AA/YD,oCA+YC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PromiseCacheCore = void 0;
|
|
4
|
+
const safe_js_1 = require("../../functions/safe.js");
|
|
4
5
|
const loggable_js_1 = require("../../logger/loggable.js");
|
|
5
6
|
const Model_js_1 = require("../../models/Model.js");
|
|
6
7
|
/**
|
|
@@ -12,7 +13,7 @@ const Model_js_1 = require("../../models/Model.js");
|
|
|
12
13
|
* - promise caching
|
|
13
14
|
* - error storage
|
|
14
15
|
* - timestamps for cached items
|
|
15
|
-
* - direct cache manipulation (invalidate,
|
|
16
|
+
* - direct cache manipulation (invalidate, set, clear)
|
|
16
17
|
* - keys iteration
|
|
17
18
|
*
|
|
18
19
|
* Subclasses are expected to implement fetching logic, invalidation policies, etc.
|
|
@@ -32,6 +33,11 @@ class PromiseCacheCore extends loggable_js_1.Loggable {
|
|
|
32
33
|
_errorsMap;
|
|
33
34
|
/** Stores items resolve timestamps (for expiration) in map by id. */
|
|
34
35
|
_timestamps = new Map();
|
|
36
|
+
/**
|
|
37
|
+
* Tracks the latest in-flight factory promise per key for "latest wins" refresh semantics.
|
|
38
|
+
* Separate from `_fetchCache` (which stores the public-facing promise returned to callers).
|
|
39
|
+
*/
|
|
40
|
+
_activeFetchPromises = new Map();
|
|
35
41
|
_version = 0;
|
|
36
42
|
constructor(keyAdapter, keyParser) {
|
|
37
43
|
super();
|
|
@@ -44,7 +50,14 @@ class PromiseCacheCore extends loggable_js_1.Loggable {
|
|
|
44
50
|
this._errorsMap = this.pure_createErrorsMap();
|
|
45
51
|
}
|
|
46
52
|
// ─── Counts ──────────────────────────────────────────────────────────
|
|
47
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* Returns the number of items currently being fetched (includes background refreshes).
|
|
55
|
+
*
|
|
56
|
+
* Note: `loadingCount` includes background refreshes started via `refresh()`,
|
|
57
|
+
* but `getIsLoading(key)` does **not** reflect background refreshes — it only
|
|
58
|
+
* tracks the per-key status set by `get()`. Use `loadingCount` for global
|
|
59
|
+
* "something is loading" indicators.
|
|
60
|
+
*/
|
|
48
61
|
get loadingCount() { return this._loadingCount.value; }
|
|
49
62
|
/** Returns the number of cached items (resolved values). */
|
|
50
63
|
get cachedCount() { return this._itemsCache.size; }
|
|
@@ -132,7 +145,7 @@ class PromiseCacheCore extends loggable_js_1.Loggable {
|
|
|
132
145
|
*
|
|
133
146
|
* - `value` / `promise` trigger a fetch if not started.
|
|
134
147
|
* - `currentValue` reads without triggering.
|
|
135
|
-
* - `refresh()`
|
|
148
|
+
* - `refresh()` re-fetches while keeping the stale value available.
|
|
136
149
|
*/
|
|
137
150
|
getLazy(key) {
|
|
138
151
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
@@ -142,18 +155,21 @@ class PromiseCacheCore extends loggable_js_1.Loggable {
|
|
|
142
155
|
get currentValue() { return self.getCurrent(key, false); },
|
|
143
156
|
get hasValue() {
|
|
144
157
|
const k = self._pk(key);
|
|
145
|
-
return self._itemsCache.has(k)
|
|
158
|
+
return self._itemsCache.has(k);
|
|
146
159
|
},
|
|
147
160
|
get error() { return self.getLastError(key); },
|
|
148
|
-
|
|
161
|
+
/** @deprecated Use {@link error} instead. */
|
|
162
|
+
get errorMessage() {
|
|
163
|
+
const err = self.getLastError(key);
|
|
164
|
+
return err != null ? (0, safe_js_1.formatError)(err) : null;
|
|
165
|
+
},
|
|
149
166
|
get isLoading() {
|
|
150
167
|
const v = self.getIsLoading(key);
|
|
151
168
|
return v === undefined ? null : v;
|
|
152
169
|
},
|
|
153
170
|
get promise() { return self.get(key); },
|
|
154
171
|
refresh() {
|
|
155
|
-
self.
|
|
156
|
-
return self.get(key);
|
|
172
|
+
return self.refresh(key);
|
|
157
173
|
},
|
|
158
174
|
};
|
|
159
175
|
}
|
|
@@ -178,7 +194,11 @@ class PromiseCacheCore extends loggable_js_1.Loggable {
|
|
|
178
194
|
/**
|
|
179
195
|
* Returns the loading state of an item.
|
|
180
196
|
*
|
|
181
|
-
*
|
|
197
|
+
* Note: background refreshes via `refresh()` do **not** update per-key loading status.
|
|
198
|
+
* This method only reflects fetches initiated by `get()`. Use `loadingCount` for
|
|
199
|
+
* a global indicator that includes background refreshes.
|
|
200
|
+
*
|
|
201
|
+
* @returns true if loading, false if loading completed, undefined if loading was not started yet (or invalidated).
|
|
182
202
|
*/
|
|
183
203
|
getIsLoading(id) {
|
|
184
204
|
const key = this._pk(id);
|
|
@@ -210,14 +230,15 @@ class PromiseCacheCore extends loggable_js_1.Loggable {
|
|
|
210
230
|
const key = this._pk(id);
|
|
211
231
|
return this._errorsMap.get(key) ?? null;
|
|
212
232
|
}
|
|
213
|
-
/** Returns the current cached value, optionally triggering a fetch. */
|
|
233
|
+
/** Returns the current cached value, optionally triggering a fetch. Falls back to the initial value if configured. */
|
|
214
234
|
getCurrent(id, initiateFetch = true) {
|
|
215
235
|
const { item, key } = this._getCurrent(id);
|
|
216
236
|
if (initiateFetch) {
|
|
217
237
|
this.get(id);
|
|
218
238
|
}
|
|
219
|
-
|
|
220
|
-
|
|
239
|
+
const result = item ?? this._getInitialValue(id);
|
|
240
|
+
this.logger.log(key, 'getCurrent: returns', result);
|
|
241
|
+
return result;
|
|
221
242
|
}
|
|
222
243
|
/** Returns true if the item is cached or fetching was initiated. Does not initiate fetching. */
|
|
223
244
|
hasKey(id) {
|
|
@@ -252,11 +273,21 @@ class PromiseCacheCore extends loggable_js_1.Loggable {
|
|
|
252
273
|
this._set(key, undefined, undefined, undefined);
|
|
253
274
|
this._errorsMap.delete(key);
|
|
254
275
|
this._timestamps.delete(key);
|
|
276
|
+
this._activeFetchPromises.delete(key);
|
|
255
277
|
}
|
|
256
|
-
/**
|
|
257
|
-
|
|
278
|
+
/** Injects a value into the cache for the specified key, as if it had been fetched. Sets the timestamp and clears any previous error. Cancels any in-flight fetch for this key. */
|
|
279
|
+
set(id, value) {
|
|
258
280
|
const key = this._pk(id);
|
|
259
281
|
this._set(key, value, undefined, undefined);
|
|
282
|
+
this._timestamps.set(key, Date.now());
|
|
283
|
+
this._errorsMap.delete(key);
|
|
284
|
+
this._activeFetchPromises.delete(key);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* @deprecated Use {@link set} instead.
|
|
288
|
+
*/
|
|
289
|
+
updateValueDirectly(id, value) {
|
|
290
|
+
return this.set(id, value);
|
|
260
291
|
}
|
|
261
292
|
/**
|
|
262
293
|
* Iterates over all cached items and removes those that are invalid (expired).
|
|
@@ -275,6 +306,7 @@ class PromiseCacheCore extends loggable_js_1.Loggable {
|
|
|
275
306
|
this._set(key, undefined, undefined, undefined);
|
|
276
307
|
this._errorsMap.delete(key);
|
|
277
308
|
this._timestamps.delete(key);
|
|
309
|
+
this._activeFetchPromises.delete(key);
|
|
278
310
|
removed++;
|
|
279
311
|
}
|
|
280
312
|
return removed;
|
|
@@ -288,6 +320,7 @@ class PromiseCacheCore extends loggable_js_1.Loggable {
|
|
|
288
320
|
this._fetchCache.clear();
|
|
289
321
|
this._errorsMap.clear();
|
|
290
322
|
this._timestamps.clear();
|
|
323
|
+
this._activeFetchPromises.clear();
|
|
291
324
|
}
|
|
292
325
|
/** @internal updates all caches states at once. */
|
|
293
326
|
_set(key, item, promise, isLoading) {
|
|
@@ -320,6 +353,10 @@ class PromiseCacheCore extends loggable_js_1.Loggable {
|
|
|
320
353
|
this._fetchCache.delete(key);
|
|
321
354
|
this._itemsStatus.set(key, false);
|
|
322
355
|
}
|
|
356
|
+
/** Hooks into the superseded fetch cleanup. Only decrements loading count — does not touch fetch cache or status. */
|
|
357
|
+
onFetchSuperseded(_key) {
|
|
358
|
+
this._loadingCount.value = this._loadingCount.value - 1;
|
|
359
|
+
}
|
|
323
360
|
/** Hooks into the result preparation process, before it's stored into the cache. */
|
|
324
361
|
prepareResult(res) {
|
|
325
362
|
return res;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.js","sourceRoot":"","sources":["../../../../src/structures/promiseCache/core.ts"],"names":[],"mappings":";;;AACA,0DAAoD;AACpD,oDAA8C;AAI9C;;;;;;;;;;;;;GAaG;AACH,MAAsB,
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../../../../src/structures/promiseCache/core.ts"],"names":[],"mappings":";;;AACA,qDAAsD;AACtD,0DAAoD;AACpD,oDAA8C;AAI9C;;;;;;;;;;;;;GAaG;AACH,MAAsB,gBAA4E,SAAQ,sBAAQ;IA6BvF;IACA;IA5BvB,0CAA0C;IACvB,WAAW,CAAmC;IAEjE,gEAAgE;IAC7C,YAAY,CAA6B;IAE5D,kCAAkC;IACf,aAAa,CAAsB;IAEtD,mEAAmE;IAChD,WAAW,CAA2C;IAEzE,oEAAoE;IACjD,UAAU,CAA6B;IAE1D,qEAAqE;IAClD,WAAW,GAAG,IAAI,GAAG,EAAkB,CAAC;IAE3D;;;OAGG;IACgB,oBAAoB,GAAG,IAAI,GAAG,EAAiC,CAAC;IAEzE,QAAQ,GAAG,CAAC,CAAC;IAEvB,YACuB,UAAsC,EACtC,SAAsC;QAEzD,KAAK,EAAE,CAAC;QAHW,eAAU,GAAV,UAAU,CAA4B;QACtC,c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
CHANGED
|
@@ -167,7 +167,9 @@ export class LazyPromise {
|
|
|
167
167
|
}
|
|
168
168
|
if (nextState !== undefined) {
|
|
169
169
|
if (this._isAsyncStateChange) {
|
|
170
|
-
|
|
170
|
+
Promise.resolve().then(() => {
|
|
171
|
+
this.updateState(nextState);
|
|
172
|
+
});
|
|
171
173
|
}
|
|
172
174
|
else {
|
|
173
175
|
this.updateState(nextState);
|
|
@@ -179,7 +181,16 @@ export class LazyPromise {
|
|
|
179
181
|
// Case when refreshing already is happening - we have an active promise
|
|
180
182
|
return;
|
|
181
183
|
}
|
|
182
|
-
|
|
184
|
+
let factoryResult;
|
|
185
|
+
try {
|
|
186
|
+
factoryResult = this._factory(refreshing);
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
// Re-throwing the original error from the synchronous factory call
|
|
190
|
+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
191
|
+
factoryResult = Promise.reject(err);
|
|
192
|
+
}
|
|
193
|
+
const factoryPromise = Promise.resolve(factoryResult)
|
|
183
194
|
.then(res => {
|
|
184
195
|
if (!this._activeFactoryPromise) {
|
|
185
196
|
// this promise was abandoned: was superseded or reset called
|
|
@@ -198,10 +209,15 @@ export class LazyPromise {
|
|
|
198
209
|
return this._activeFactoryPromise;
|
|
199
210
|
})
|
|
200
211
|
.catch(err => {
|
|
201
|
-
if (!this._activeFactoryPromise
|
|
212
|
+
if (!this._activeFactoryPromise) {
|
|
213
|
+
// Abandoned (reset/dispose was called) — don't corrupt state
|
|
214
|
+
return this._instance ?? this._initial;
|
|
215
|
+
}
|
|
216
|
+
if (this._activeFactoryPromise === factoryPromise) {
|
|
202
217
|
return this.onRejected(err);
|
|
203
218
|
}
|
|
204
|
-
|
|
219
|
+
// Stale promise — delegate to the latest active promise instead of re-throwing
|
|
220
|
+
return this._activeFactoryPromise;
|
|
205
221
|
});
|
|
206
222
|
const hadActive = !!this._activeFactoryPromise;
|
|
207
223
|
// This is now the active promise - any previous one is superseded
|