@streetsidesoftware/auto-resolve-map 0.0.0-stage → 0.0.1-alpha.0
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/LICENSE +21 -0
- package/README.md +154 -2
- package/dist/index.d.ts +629 -0
- package/dist/index.js +936 -0
- package/package.json +41 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,936 @@
|
|
|
1
|
+
// src/autoResolve.ts
|
|
2
|
+
/**
|
|
3
|
+
* Get the value for a key, or compute and store it when there is none.
|
|
4
|
+
*
|
|
5
|
+
* It works with a map you already have. To create a map with this built in, use {@link AutoResolveMap}.
|
|
6
|
+
*
|
|
7
|
+
* - A stored `undefined` counts as a value.
|
|
8
|
+
* - Nothing is stored when the callback throws.
|
|
9
|
+
*
|
|
10
|
+
* @param map - the map to look in and store to.
|
|
11
|
+
* @param key - the key to look up.
|
|
12
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
13
|
+
* @example
|
|
14
|
+
* const lengths = new Map<string, number>();
|
|
15
|
+
* autoResolve(lengths, 'apple', (key) => key.length); // 5, computed and stored
|
|
16
|
+
* autoResolve(lengths, 'apple', (key) => key.length); // 5, from the map
|
|
17
|
+
*/
|
|
18
|
+
function autoResolve(map, key, callback) {
|
|
19
|
+
const found = map.get(key);
|
|
20
|
+
if (found !== void 0 || map.has(key)) return found;
|
|
21
|
+
const value = callback(key);
|
|
22
|
+
map.set(key, value);
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
// src/checks.ts
|
|
26
|
+
function assertCallback(callback) {
|
|
27
|
+
if (typeof callback !== "function") throw new TypeError("The callback must be a function.");
|
|
28
|
+
}
|
|
29
|
+
/** Reject a key that a `WeakMap` can't store. The methods that only look up such a key find nothing instead. */
|
|
30
|
+
function assertWeakKey(key) {
|
|
31
|
+
if (Object(key) === key) return;
|
|
32
|
+
if (typeof key === "symbol" && Symbol.keyFor(key) === void 0) return;
|
|
33
|
+
throw new TypeError("The key must be an object or a non-registered symbol.");
|
|
34
|
+
}
|
|
35
|
+
/** Turn -0 into 0, as a `Map` does with its keys. */
|
|
36
|
+
function normalizeKey(key) {
|
|
37
|
+
return key === 0 ? 0 : key;
|
|
38
|
+
}
|
|
39
|
+
// src/AutoResolveMap.ts
|
|
40
|
+
/**
|
|
41
|
+
* A `Map` that can store a value the first time it's asked for.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const lengths = new AutoResolveMap<string, number>();
|
|
45
|
+
* lengths.getOrInsertComputed('apple', (key) => key.length); // 5, computed and stored
|
|
46
|
+
* lengths.get('apple'); // 5
|
|
47
|
+
*/
|
|
48
|
+
var AutoResolveMap = class extends Map {
|
|
49
|
+
/**
|
|
50
|
+
* Get the value for a key, or store the given one when there is none.
|
|
51
|
+
*
|
|
52
|
+
* @param key - the key to look up.
|
|
53
|
+
* @param value - the value to store when there is none.
|
|
54
|
+
*/
|
|
55
|
+
getOrInsert(key, value) {
|
|
56
|
+
const found = super.get(key);
|
|
57
|
+
if (found !== void 0 || super.has(key)) return found;
|
|
58
|
+
super.set(key, value);
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Get the value for a key, or compute and store it when there is none.
|
|
63
|
+
*
|
|
64
|
+
* - A stored `undefined` counts as a value.
|
|
65
|
+
* - Nothing is stored when the callback throws.
|
|
66
|
+
*
|
|
67
|
+
* @param key - the key to look up.
|
|
68
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
69
|
+
*/
|
|
70
|
+
getOrInsertComputed(key, callback) {
|
|
71
|
+
assertCallback(callback);
|
|
72
|
+
const found = super.get(key);
|
|
73
|
+
if (found !== void 0 || super.has(key)) return found;
|
|
74
|
+
const value = callback(normalizeKey(key));
|
|
75
|
+
super.set(key, value);
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
// src/AutoResolveWeakMap.ts
|
|
80
|
+
/**
|
|
81
|
+
* A `WeakMap` that can store a value the first time it's asked for, and can be emptied.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* const sizes = new AutoResolveWeakMap<object, number>();
|
|
85
|
+
* const point = { x: 1, y: 2 };
|
|
86
|
+
* sizes.getOrInsertComputed(point, (key) => Object.keys(key).length); // 2, computed and stored
|
|
87
|
+
* sizes.clear();
|
|
88
|
+
* sizes.has(point); // false
|
|
89
|
+
*/
|
|
90
|
+
var AutoResolveWeakMap = class {
|
|
91
|
+
#map;
|
|
92
|
+
/**
|
|
93
|
+
* Create a map.
|
|
94
|
+
*
|
|
95
|
+
* @param entries - the keys and values to start with.
|
|
96
|
+
*/
|
|
97
|
+
constructor(entries) {
|
|
98
|
+
this.#map = new WeakMap(entries ?? []);
|
|
99
|
+
}
|
|
100
|
+
/** Remove every entry, even when its key is still in use. */
|
|
101
|
+
clear() {
|
|
102
|
+
this.#map = /* @__PURE__ */ new WeakMap();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Remove the entry for a key.
|
|
106
|
+
*
|
|
107
|
+
* The result is `true` when there was one.
|
|
108
|
+
*
|
|
109
|
+
* @param key - the key to remove.
|
|
110
|
+
*/
|
|
111
|
+
delete(key) {
|
|
112
|
+
return this.#map.delete(key);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get the value for a key, or `undefined` if there is none.
|
|
116
|
+
*
|
|
117
|
+
* @param key - the key to look up.
|
|
118
|
+
*/
|
|
119
|
+
get(key) {
|
|
120
|
+
return this.#map.get(key);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get the value for a key, or store the given one when there is none.
|
|
124
|
+
*
|
|
125
|
+
* @param key - the key to look up.
|
|
126
|
+
* @param value - the value to store when there is none.
|
|
127
|
+
*/
|
|
128
|
+
getOrInsert(key, value) {
|
|
129
|
+
const found = this.#map.get(key);
|
|
130
|
+
if (found !== void 0 || this.#map.has(key)) return found;
|
|
131
|
+
this.#map.set(key, value);
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get the value for a key, or compute and store it when there is none.
|
|
136
|
+
*
|
|
137
|
+
* - A stored `undefined` counts as a value.
|
|
138
|
+
* - Nothing is stored when the callback throws.
|
|
139
|
+
*
|
|
140
|
+
* @param key - the key to look up.
|
|
141
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
142
|
+
*/
|
|
143
|
+
getOrInsertComputed(key, callback) {
|
|
144
|
+
assertWeakKey(key);
|
|
145
|
+
assertCallback(callback);
|
|
146
|
+
const found = this.#map.get(key);
|
|
147
|
+
if (found !== void 0 || this.#map.has(key)) return found;
|
|
148
|
+
const value = callback(key);
|
|
149
|
+
this.#map.set(key, value);
|
|
150
|
+
return value;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Check whether there is a value for a key.
|
|
154
|
+
*
|
|
155
|
+
* @param key - the key to look up.
|
|
156
|
+
*/
|
|
157
|
+
has(key) {
|
|
158
|
+
return this.#map.has(key);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Store a value for a key, replacing the one it had.
|
|
162
|
+
*
|
|
163
|
+
* @param key - the key to store the value for.
|
|
164
|
+
* @param value - the value to store.
|
|
165
|
+
*/
|
|
166
|
+
set(key, value) {
|
|
167
|
+
this.#map.set(key, value);
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
/** The name `Object.prototype.toString` shows. */
|
|
171
|
+
get [Symbol.toStringTag]() {
|
|
172
|
+
return "AutoResolveWeakMap";
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
// src/cacheStats.ts
|
|
176
|
+
var StatsTracker = class StatsTracker {
|
|
177
|
+
hits = 0;
|
|
178
|
+
misses = 0;
|
|
179
|
+
resolved = 0;
|
|
180
|
+
sets = 0;
|
|
181
|
+
deletes = 0;
|
|
182
|
+
clears = 0;
|
|
183
|
+
/** Count a lookup as a hit or a miss. */
|
|
184
|
+
lookup(found) {
|
|
185
|
+
if (found) ++this.hits;
|
|
186
|
+
else ++this.misses;
|
|
187
|
+
}
|
|
188
|
+
snapshot() {
|
|
189
|
+
return { ...this };
|
|
190
|
+
}
|
|
191
|
+
reset() {
|
|
192
|
+
Object.assign(this, new StatsTracker());
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
// src/StrongWeakMap.ts
|
|
196
|
+
/**
|
|
197
|
+
* A `Map` that doesn't keep its values alive.
|
|
198
|
+
*
|
|
199
|
+
* Once nothing else uses a value, it can be released (garbage collected). Its key is then as if it had never been
|
|
200
|
+
* there. The key itself is removed some time later, so `size` and `keys` may still include it.
|
|
201
|
+
*
|
|
202
|
+
* Any look at a value keeps it alive until the current turn of the event loop ends. That includes `has` and looping
|
|
203
|
+
* over the map.
|
|
204
|
+
*
|
|
205
|
+
* Each entry has a fixed cost on top of a normal map's, whatever the size of its value. It suits a small number of
|
|
206
|
+
* large objects.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* const dictionaries = new StrongWeakMap<string, { words: string[] }>();
|
|
210
|
+
* const colors = dictionaries.getOrInsertComputed('colors', () => ({ words: ['blue', 'red'] }));
|
|
211
|
+
* dictionaries.get('colors') === colors; // true, as long as something uses `colors`
|
|
212
|
+
*/
|
|
213
|
+
var StrongWeakMap = class {
|
|
214
|
+
#map = /* @__PURE__ */ new Map();
|
|
215
|
+
#registry = new FinalizationRegistry(({ key, ref }) => {
|
|
216
|
+
if (this.#map.get(key) === ref) this.#map.delete(key);
|
|
217
|
+
});
|
|
218
|
+
/**
|
|
219
|
+
* Create a map.
|
|
220
|
+
*
|
|
221
|
+
* @param entries - the keys and values to start with.
|
|
222
|
+
*/
|
|
223
|
+
constructor(entries) {
|
|
224
|
+
for (const [key, value] of entries ?? []) this.#store(key, value);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The number of keys.
|
|
228
|
+
*
|
|
229
|
+
* It may include keys whose value was released.
|
|
230
|
+
*/
|
|
231
|
+
get size() {
|
|
232
|
+
return this.#map.size;
|
|
233
|
+
}
|
|
234
|
+
/** Remove every entry. */
|
|
235
|
+
clear() {
|
|
236
|
+
for (const ref of this.#map.values()) this.#registry.unregister(ref);
|
|
237
|
+
this.#map.clear();
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Remove the entry for a key.
|
|
241
|
+
*
|
|
242
|
+
* The result is `true` when there was one whose value hadn't been released.
|
|
243
|
+
*
|
|
244
|
+
* @param key - the key to remove.
|
|
245
|
+
*/
|
|
246
|
+
delete(key) {
|
|
247
|
+
const ref = this.#map.get(key);
|
|
248
|
+
if (!ref) return false;
|
|
249
|
+
this.#registry.unregister(ref);
|
|
250
|
+
this.#map.delete(key);
|
|
251
|
+
return ref.deref() !== void 0;
|
|
252
|
+
}
|
|
253
|
+
/** Get the key and value of each entry whose value wasn't released. */
|
|
254
|
+
entries() {
|
|
255
|
+
return this.#iterate((key, value) => [key, value]);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Call a function for each entry whose value wasn't released.
|
|
259
|
+
*
|
|
260
|
+
* @param callback - called with the value, the key and the map.
|
|
261
|
+
* @param thisArg - the `this` of the callback.
|
|
262
|
+
*/
|
|
263
|
+
forEach(callback, thisArg) {
|
|
264
|
+
assertCallback(callback);
|
|
265
|
+
for (const [key, value] of this.entries()) callback.call(thisArg, value, key, this);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Get the value for a key, or `undefined` if there is none or it was released.
|
|
269
|
+
*
|
|
270
|
+
* @param key - the key to look up.
|
|
271
|
+
*/
|
|
272
|
+
get(key) {
|
|
273
|
+
return this.#find(key);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Get the value for a key, or store the given one when there is none or it was released.
|
|
277
|
+
*
|
|
278
|
+
* @param key - the key to look up.
|
|
279
|
+
* @param value - the value to store when there is none.
|
|
280
|
+
*/
|
|
281
|
+
getOrInsert(key, value) {
|
|
282
|
+
const found = this.#find(key);
|
|
283
|
+
if (found !== void 0) return found;
|
|
284
|
+
this.#store(key, value);
|
|
285
|
+
return value;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Get the value for a key, or compute and store it when there is none or it was released.
|
|
289
|
+
*
|
|
290
|
+
* Nothing is stored when the callback throws.
|
|
291
|
+
*
|
|
292
|
+
* @param key - the key to look up.
|
|
293
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
294
|
+
*/
|
|
295
|
+
getOrInsertComputed(key, callback) {
|
|
296
|
+
assertCallback(callback);
|
|
297
|
+
const found = this.#find(key);
|
|
298
|
+
if (found !== void 0) return found;
|
|
299
|
+
const value = callback(normalizeKey(key));
|
|
300
|
+
this.#store(key, value);
|
|
301
|
+
return value;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Check whether there is a value for a key.
|
|
305
|
+
*
|
|
306
|
+
* This keeps the value alive until the current turn of the event loop ends.
|
|
307
|
+
*
|
|
308
|
+
* @param key - the key to look up.
|
|
309
|
+
*/
|
|
310
|
+
has(key) {
|
|
311
|
+
return this.#find(key) !== void 0;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Get the keys.
|
|
315
|
+
*
|
|
316
|
+
* They may include keys whose value was released.
|
|
317
|
+
*/
|
|
318
|
+
keys() {
|
|
319
|
+
return this.#map.keys();
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Store a value for a key, replacing the one it had.
|
|
323
|
+
*
|
|
324
|
+
* @param key - the key to store the value for.
|
|
325
|
+
* @param value - the value to store.
|
|
326
|
+
*/
|
|
327
|
+
set(key, value) {
|
|
328
|
+
this.#store(key, value);
|
|
329
|
+
return this;
|
|
330
|
+
}
|
|
331
|
+
/** Get the values that weren't released. */
|
|
332
|
+
values() {
|
|
333
|
+
return this.#iterate((_, value) => value);
|
|
334
|
+
}
|
|
335
|
+
/** Get the key and value of each entry whose value wasn't released. */
|
|
336
|
+
[Symbol.iterator]() {
|
|
337
|
+
return this.entries();
|
|
338
|
+
}
|
|
339
|
+
/** The name `Object.prototype.toString` shows. */
|
|
340
|
+
get [Symbol.toStringTag]() {
|
|
341
|
+
return "StrongWeakMap";
|
|
342
|
+
}
|
|
343
|
+
#find(key) {
|
|
344
|
+
return this.#map.get(key)?.deref();
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Create an iterator over the entries whose value wasn't released.
|
|
348
|
+
* A generator would keep its last value alive while paused, and couldn't be continued after a `break`.
|
|
349
|
+
*/
|
|
350
|
+
#iterate(pick) {
|
|
351
|
+
const entries = this.#map.entries();
|
|
352
|
+
return Iterator.from({ next() {
|
|
353
|
+
for (const [key, ref] of entries) {
|
|
354
|
+
const value = ref.deref();
|
|
355
|
+
if (value !== void 0) return {
|
|
356
|
+
done: false,
|
|
357
|
+
value: pick(key, value)
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
return {
|
|
361
|
+
done: true,
|
|
362
|
+
value: void 0
|
|
363
|
+
};
|
|
364
|
+
} });
|
|
365
|
+
}
|
|
366
|
+
#store(key, value) {
|
|
367
|
+
const ref = new WeakRef(value);
|
|
368
|
+
const previous = this.#map.get(key);
|
|
369
|
+
if (previous) this.#registry.unregister(previous);
|
|
370
|
+
this.#map.set(key, ref);
|
|
371
|
+
this.#registry.register(value, {
|
|
372
|
+
key,
|
|
373
|
+
ref
|
|
374
|
+
}, ref);
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
// src/WeakWeakMap.ts
|
|
378
|
+
/**
|
|
379
|
+
* A `WeakMap` that doesn't keep its values alive either, and can be emptied.
|
|
380
|
+
*
|
|
381
|
+
* Once nothing else uses a value, it can be released (garbage collected). Its key is then as if it had never been
|
|
382
|
+
* there.
|
|
383
|
+
*
|
|
384
|
+
* Any look at a value keeps it alive until the current turn of the event loop ends. That includes `has`.
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* const parsed = new WeakWeakMap<URL, { hosts: string[] }>();
|
|
388
|
+
* const url = new URL('https://example.com');
|
|
389
|
+
* const result = parsed.getOrInsertComputed(url, (key) => ({ hosts: [key.host] }));
|
|
390
|
+
* parsed.get(url) === result; // true, as long as something uses `result`
|
|
391
|
+
*/
|
|
392
|
+
var WeakWeakMap = class {
|
|
393
|
+
#map = /* @__PURE__ */ new WeakMap();
|
|
394
|
+
/**
|
|
395
|
+
* Create a map.
|
|
396
|
+
*
|
|
397
|
+
* @param entries - the keys and values to start with.
|
|
398
|
+
*/
|
|
399
|
+
constructor(entries) {
|
|
400
|
+
for (const [key, value] of entries ?? []) this.#store(key, value);
|
|
401
|
+
}
|
|
402
|
+
/** Remove every entry, even when its key and value are still in use. */
|
|
403
|
+
clear() {
|
|
404
|
+
this.#map = /* @__PURE__ */ new WeakMap();
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Remove the entry for a key.
|
|
408
|
+
*
|
|
409
|
+
* The result is `true` when there was one whose value hadn't been released.
|
|
410
|
+
*
|
|
411
|
+
* @param key - the key to remove.
|
|
412
|
+
*/
|
|
413
|
+
delete(key) {
|
|
414
|
+
const found = this.#find(key);
|
|
415
|
+
this.#map.delete(key);
|
|
416
|
+
return found !== void 0;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Get the value for a key, or `undefined` if there is none or it was released.
|
|
420
|
+
*
|
|
421
|
+
* @param key - the key to look up.
|
|
422
|
+
*/
|
|
423
|
+
get(key) {
|
|
424
|
+
return this.#find(key);
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Get the value for a key, or store the given one when there is none or it was released.
|
|
428
|
+
*
|
|
429
|
+
* @param key - the key to look up.
|
|
430
|
+
* @param value - the value to store when there is none.
|
|
431
|
+
*/
|
|
432
|
+
getOrInsert(key, value) {
|
|
433
|
+
const found = this.#find(key);
|
|
434
|
+
if (found !== void 0) return found;
|
|
435
|
+
this.#store(key, value);
|
|
436
|
+
return value;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Get the value for a key, or compute and store it when there is none or it was released.
|
|
440
|
+
*
|
|
441
|
+
* Nothing is stored when the callback throws.
|
|
442
|
+
*
|
|
443
|
+
* @param key - the key to look up.
|
|
444
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
445
|
+
*/
|
|
446
|
+
getOrInsertComputed(key, callback) {
|
|
447
|
+
assertWeakKey(key);
|
|
448
|
+
assertCallback(callback);
|
|
449
|
+
const found = this.#find(key);
|
|
450
|
+
if (found !== void 0) return found;
|
|
451
|
+
const value = callback(key);
|
|
452
|
+
this.#store(key, value);
|
|
453
|
+
return value;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Check whether there is a value for a key.
|
|
457
|
+
*
|
|
458
|
+
* This keeps the value alive until the current turn of the event loop ends.
|
|
459
|
+
*
|
|
460
|
+
* @param key - the key to look up.
|
|
461
|
+
*/
|
|
462
|
+
has(key) {
|
|
463
|
+
return this.#find(key) !== void 0;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Store a value for a key, replacing the one it had.
|
|
467
|
+
*
|
|
468
|
+
* @param key - the key to store the value for.
|
|
469
|
+
* @param value - the value to store.
|
|
470
|
+
*/
|
|
471
|
+
set(key, value) {
|
|
472
|
+
this.#store(key, value);
|
|
473
|
+
return this;
|
|
474
|
+
}
|
|
475
|
+
/** The name `Object.prototype.toString` shows. */
|
|
476
|
+
get [Symbol.toStringTag]() {
|
|
477
|
+
return "WeakWeakMap";
|
|
478
|
+
}
|
|
479
|
+
#find(key) {
|
|
480
|
+
return this.#map.get(key)?.deref();
|
|
481
|
+
}
|
|
482
|
+
#store(key, value) {
|
|
483
|
+
this.#map.set(key, new WeakRef(value));
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
// src/caches.ts
|
|
487
|
+
/** The same as {@link AutoResolveMap}, with stats on how often it's used. */
|
|
488
|
+
var AutoResolveCache = class extends AutoResolveMap {
|
|
489
|
+
#stats = new StatsTracker();
|
|
490
|
+
/**
|
|
491
|
+
* Create a cache.
|
|
492
|
+
*
|
|
493
|
+
* The entries it starts with don't count as sets.
|
|
494
|
+
*
|
|
495
|
+
* @param entries - the keys and values to start with.
|
|
496
|
+
*/
|
|
497
|
+
constructor(entries) {
|
|
498
|
+
super();
|
|
499
|
+
for (const [key, value] of entries ?? []) super.set(key, value);
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Remove every entry.
|
|
503
|
+
*
|
|
504
|
+
* Counts a call to `clear`. The other counts stay as they are.
|
|
505
|
+
*/
|
|
506
|
+
clear() {
|
|
507
|
+
super.clear();
|
|
508
|
+
++this.#stats.clears;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Remove the entry for a key.
|
|
512
|
+
*
|
|
513
|
+
* The result is `true` when there was one.
|
|
514
|
+
*
|
|
515
|
+
* Counts a call, also when there was no entry.
|
|
516
|
+
*
|
|
517
|
+
* @param key - the key to remove.
|
|
518
|
+
*/
|
|
519
|
+
delete(key) {
|
|
520
|
+
++this.#stats.deletes;
|
|
521
|
+
return super.delete(key);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Get the value for a key, or `undefined` if there is none.
|
|
525
|
+
*
|
|
526
|
+
* Counts a hit or a miss.
|
|
527
|
+
*
|
|
528
|
+
* @param key - the key to look up.
|
|
529
|
+
*/
|
|
530
|
+
get(key) {
|
|
531
|
+
const found = super.get(key);
|
|
532
|
+
this.#stats.lookup(found !== void 0 || super.has(key));
|
|
533
|
+
return found;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Get the value for a key, or store the given one when there is none.
|
|
537
|
+
*
|
|
538
|
+
* Counts a hit, or a miss and a set.
|
|
539
|
+
*
|
|
540
|
+
* @param key - the key to look up.
|
|
541
|
+
* @param value - the value to store when there is none.
|
|
542
|
+
*/
|
|
543
|
+
getOrInsert(key, value) {
|
|
544
|
+
const found = super.get(key);
|
|
545
|
+
if (found !== void 0 || super.has(key)) {
|
|
546
|
+
++this.#stats.hits;
|
|
547
|
+
return found;
|
|
548
|
+
}
|
|
549
|
+
super.set(key, value);
|
|
550
|
+
++this.#stats.misses;
|
|
551
|
+
++this.#stats.sets;
|
|
552
|
+
return value;
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Get the value for a key, or compute and store it when there is none.
|
|
556
|
+
*
|
|
557
|
+
* - A stored `undefined` counts as a value.
|
|
558
|
+
* - Nothing is stored when the callback throws.
|
|
559
|
+
*
|
|
560
|
+
* Counts a hit, or a miss. A stored result counts as resolved.
|
|
561
|
+
*
|
|
562
|
+
* @param key - the key to look up.
|
|
563
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
564
|
+
*/
|
|
565
|
+
getOrInsertComputed(key, callback) {
|
|
566
|
+
assertCallback(callback);
|
|
567
|
+
const found = super.get(key);
|
|
568
|
+
if (found !== void 0 || super.has(key)) {
|
|
569
|
+
++this.#stats.hits;
|
|
570
|
+
return found;
|
|
571
|
+
}
|
|
572
|
+
++this.#stats.misses;
|
|
573
|
+
const value = super.getOrInsertComputed(key, callback);
|
|
574
|
+
++this.#stats.resolved;
|
|
575
|
+
return value;
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Store a value for a key, replacing the one it had.
|
|
579
|
+
*
|
|
580
|
+
* Counts a set.
|
|
581
|
+
*
|
|
582
|
+
* @param key - the key to store the value for.
|
|
583
|
+
* @param value - the value to store.
|
|
584
|
+
*/
|
|
585
|
+
set(key, value) {
|
|
586
|
+
super.set(key, value);
|
|
587
|
+
++this.#stats.sets;
|
|
588
|
+
return this;
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Get the stats as they are now.
|
|
592
|
+
*
|
|
593
|
+
* Later use of the cache doesn't change the result.
|
|
594
|
+
*/
|
|
595
|
+
stats() {
|
|
596
|
+
return this.#stats.snapshot();
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Set every count in the stats to 0.
|
|
600
|
+
*
|
|
601
|
+
* The entries stay.
|
|
602
|
+
*/
|
|
603
|
+
resetStats() {
|
|
604
|
+
this.#stats.reset();
|
|
605
|
+
}
|
|
606
|
+
};
|
|
607
|
+
/** The same as {@link AutoResolveWeakMap}, with stats on how often it's used. */
|
|
608
|
+
var AutoResolveWeakCache = class extends AutoResolveWeakMap {
|
|
609
|
+
#stats = new StatsTracker();
|
|
610
|
+
/**
|
|
611
|
+
* Remove every entry, even when its key is still in use.
|
|
612
|
+
*
|
|
613
|
+
* Counts a call to `clear`. The other counts stay as they are.
|
|
614
|
+
*/
|
|
615
|
+
clear() {
|
|
616
|
+
super.clear();
|
|
617
|
+
++this.#stats.clears;
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Remove the entry for a key.
|
|
621
|
+
*
|
|
622
|
+
* The result is `true` when there was one.
|
|
623
|
+
*
|
|
624
|
+
* Counts a call, also when there was no entry.
|
|
625
|
+
*
|
|
626
|
+
* @param key - the key to remove.
|
|
627
|
+
*/
|
|
628
|
+
delete(key) {
|
|
629
|
+
++this.#stats.deletes;
|
|
630
|
+
return super.delete(key);
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Get the value for a key, or `undefined` if there is none.
|
|
634
|
+
*
|
|
635
|
+
* Counts a hit or a miss.
|
|
636
|
+
*
|
|
637
|
+
* @param key - the key to look up.
|
|
638
|
+
*/
|
|
639
|
+
get(key) {
|
|
640
|
+
const found = super.get(key);
|
|
641
|
+
this.#stats.lookup(found !== void 0 || super.has(key));
|
|
642
|
+
return found;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Get the value for a key, or store the given one when there is none.
|
|
646
|
+
*
|
|
647
|
+
* Counts a hit, or a miss and a set.
|
|
648
|
+
*
|
|
649
|
+
* @param key - the key to look up.
|
|
650
|
+
* @param value - the value to store when there is none.
|
|
651
|
+
*/
|
|
652
|
+
getOrInsert(key, value) {
|
|
653
|
+
const found = super.get(key);
|
|
654
|
+
if (found !== void 0 || super.has(key)) {
|
|
655
|
+
++this.#stats.hits;
|
|
656
|
+
return found;
|
|
657
|
+
}
|
|
658
|
+
super.set(key, value);
|
|
659
|
+
++this.#stats.misses;
|
|
660
|
+
++this.#stats.sets;
|
|
661
|
+
return value;
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Get the value for a key, or compute and store it when there is none.
|
|
665
|
+
*
|
|
666
|
+
* - A stored `undefined` counts as a value.
|
|
667
|
+
* - Nothing is stored when the callback throws.
|
|
668
|
+
*
|
|
669
|
+
* Counts a hit, or a miss. A stored result counts as resolved.
|
|
670
|
+
*
|
|
671
|
+
* @param key - the key to look up.
|
|
672
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
673
|
+
*/
|
|
674
|
+
getOrInsertComputed(key, callback) {
|
|
675
|
+
assertWeakKey(key);
|
|
676
|
+
assertCallback(callback);
|
|
677
|
+
const found = super.get(key);
|
|
678
|
+
if (found !== void 0 || super.has(key)) {
|
|
679
|
+
++this.#stats.hits;
|
|
680
|
+
return found;
|
|
681
|
+
}
|
|
682
|
+
++this.#stats.misses;
|
|
683
|
+
const value = super.getOrInsertComputed(key, callback);
|
|
684
|
+
++this.#stats.resolved;
|
|
685
|
+
return value;
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Store a value for a key, replacing the one it had.
|
|
689
|
+
*
|
|
690
|
+
* Counts a set.
|
|
691
|
+
*
|
|
692
|
+
* @param key - the key to store the value for.
|
|
693
|
+
* @param value - the value to store.
|
|
694
|
+
*/
|
|
695
|
+
set(key, value) {
|
|
696
|
+
super.set(key, value);
|
|
697
|
+
++this.#stats.sets;
|
|
698
|
+
return this;
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Get the stats as they are now.
|
|
702
|
+
*
|
|
703
|
+
* Later use of the cache doesn't change the result.
|
|
704
|
+
*/
|
|
705
|
+
stats() {
|
|
706
|
+
return this.#stats.snapshot();
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Set every count in the stats to 0.
|
|
710
|
+
*
|
|
711
|
+
* The entries stay.
|
|
712
|
+
*/
|
|
713
|
+
resetStats() {
|
|
714
|
+
this.#stats.reset();
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
/** The same as {@link StrongWeakMap}, with stats on how often it's used. */
|
|
718
|
+
var StrongWeakCache = class extends StrongWeakMap {
|
|
719
|
+
#stats = new StatsTracker();
|
|
720
|
+
/**
|
|
721
|
+
* Remove every entry.
|
|
722
|
+
*
|
|
723
|
+
* Counts a call to `clear`. The other counts stay as they are.
|
|
724
|
+
*/
|
|
725
|
+
clear() {
|
|
726
|
+
super.clear();
|
|
727
|
+
++this.#stats.clears;
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Remove the entry for a key.
|
|
731
|
+
*
|
|
732
|
+
* The result is `true` when there was one whose value hadn't been released.
|
|
733
|
+
*
|
|
734
|
+
* Counts a call, also when there was no entry.
|
|
735
|
+
*
|
|
736
|
+
* @param key - the key to remove.
|
|
737
|
+
*/
|
|
738
|
+
delete(key) {
|
|
739
|
+
++this.#stats.deletes;
|
|
740
|
+
return super.delete(key);
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Get the value for a key, or `undefined` if there is none or it was released.
|
|
744
|
+
*
|
|
745
|
+
* Counts a hit or a miss.
|
|
746
|
+
*
|
|
747
|
+
* @param key - the key to look up.
|
|
748
|
+
*/
|
|
749
|
+
get(key) {
|
|
750
|
+
const found = super.get(key);
|
|
751
|
+
this.#stats.lookup(found !== void 0);
|
|
752
|
+
return found;
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Get the value for a key, or store the given one when there is none or it was released.
|
|
756
|
+
*
|
|
757
|
+
* Counts a hit, or a miss and a set.
|
|
758
|
+
*
|
|
759
|
+
* @param key - the key to look up.
|
|
760
|
+
* @param value - the value to store when there is none.
|
|
761
|
+
*/
|
|
762
|
+
getOrInsert(key, value) {
|
|
763
|
+
const found = super.get(key);
|
|
764
|
+
if (found !== void 0) {
|
|
765
|
+
++this.#stats.hits;
|
|
766
|
+
return found;
|
|
767
|
+
}
|
|
768
|
+
super.set(key, value);
|
|
769
|
+
++this.#stats.misses;
|
|
770
|
+
++this.#stats.sets;
|
|
771
|
+
return value;
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Get the value for a key, or compute and store it when there is none or it was released.
|
|
775
|
+
*
|
|
776
|
+
* Nothing is stored when the callback throws.
|
|
777
|
+
*
|
|
778
|
+
* Counts a hit, or a miss. A stored result counts as resolved.
|
|
779
|
+
*
|
|
780
|
+
* @param key - the key to look up.
|
|
781
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
782
|
+
*/
|
|
783
|
+
getOrInsertComputed(key, callback) {
|
|
784
|
+
assertCallback(callback);
|
|
785
|
+
const found = super.get(key);
|
|
786
|
+
if (found !== void 0) {
|
|
787
|
+
++this.#stats.hits;
|
|
788
|
+
return found;
|
|
789
|
+
}
|
|
790
|
+
++this.#stats.misses;
|
|
791
|
+
const value = super.getOrInsertComputed(key, callback);
|
|
792
|
+
++this.#stats.resolved;
|
|
793
|
+
return value;
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Store a value for a key, replacing the one it had.
|
|
797
|
+
*
|
|
798
|
+
* Counts a set.
|
|
799
|
+
*
|
|
800
|
+
* @param key - the key to store the value for.
|
|
801
|
+
* @param value - the value to store.
|
|
802
|
+
*/
|
|
803
|
+
set(key, value) {
|
|
804
|
+
super.set(key, value);
|
|
805
|
+
++this.#stats.sets;
|
|
806
|
+
return this;
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* Get the stats as they are now.
|
|
810
|
+
*
|
|
811
|
+
* Later use of the cache doesn't change the result.
|
|
812
|
+
*/
|
|
813
|
+
stats() {
|
|
814
|
+
return this.#stats.snapshot();
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Set every count in the stats to 0.
|
|
818
|
+
*
|
|
819
|
+
* The entries stay.
|
|
820
|
+
*/
|
|
821
|
+
resetStats() {
|
|
822
|
+
this.#stats.reset();
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
/** The same as {@link WeakWeakMap}, with stats on how often it's used. */
|
|
826
|
+
var WeakWeakCache = class extends WeakWeakMap {
|
|
827
|
+
#stats = new StatsTracker();
|
|
828
|
+
/**
|
|
829
|
+
* Remove every entry, even when its key and value are still in use.
|
|
830
|
+
*
|
|
831
|
+
* Counts a call to `clear`. The other counts stay as they are.
|
|
832
|
+
*/
|
|
833
|
+
clear() {
|
|
834
|
+
super.clear();
|
|
835
|
+
++this.#stats.clears;
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Remove the entry for a key.
|
|
839
|
+
*
|
|
840
|
+
* The result is `true` when there was one whose value hadn't been released.
|
|
841
|
+
*
|
|
842
|
+
* Counts a call, also when there was no entry.
|
|
843
|
+
*
|
|
844
|
+
* @param key - the key to remove.
|
|
845
|
+
*/
|
|
846
|
+
delete(key) {
|
|
847
|
+
++this.#stats.deletes;
|
|
848
|
+
return super.delete(key);
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* Get the value for a key, or `undefined` if there is none or it was released.
|
|
852
|
+
*
|
|
853
|
+
* Counts a hit or a miss.
|
|
854
|
+
*
|
|
855
|
+
* @param key - the key to look up.
|
|
856
|
+
*/
|
|
857
|
+
get(key) {
|
|
858
|
+
const found = super.get(key);
|
|
859
|
+
this.#stats.lookup(found !== void 0);
|
|
860
|
+
return found;
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* Get the value for a key, or store the given one when there is none or it was released.
|
|
864
|
+
*
|
|
865
|
+
* Counts a hit, or a miss and a set.
|
|
866
|
+
*
|
|
867
|
+
* @param key - the key to look up.
|
|
868
|
+
* @param value - the value to store when there is none.
|
|
869
|
+
*/
|
|
870
|
+
getOrInsert(key, value) {
|
|
871
|
+
const found = super.get(key);
|
|
872
|
+
if (found !== void 0) {
|
|
873
|
+
++this.#stats.hits;
|
|
874
|
+
return found;
|
|
875
|
+
}
|
|
876
|
+
super.set(key, value);
|
|
877
|
+
++this.#stats.misses;
|
|
878
|
+
++this.#stats.sets;
|
|
879
|
+
return value;
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* Get the value for a key, or compute and store it when there is none or it was released.
|
|
883
|
+
*
|
|
884
|
+
* Nothing is stored when the callback throws.
|
|
885
|
+
*
|
|
886
|
+
* Counts a hit, or a miss. A stored result counts as resolved.
|
|
887
|
+
*
|
|
888
|
+
* @param key - the key to look up.
|
|
889
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
890
|
+
*/
|
|
891
|
+
getOrInsertComputed(key, callback) {
|
|
892
|
+
assertWeakKey(key);
|
|
893
|
+
assertCallback(callback);
|
|
894
|
+
const found = super.get(key);
|
|
895
|
+
if (found !== void 0) {
|
|
896
|
+
++this.#stats.hits;
|
|
897
|
+
return found;
|
|
898
|
+
}
|
|
899
|
+
++this.#stats.misses;
|
|
900
|
+
const value = super.getOrInsertComputed(key, callback);
|
|
901
|
+
++this.#stats.resolved;
|
|
902
|
+
return value;
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Store a value for a key, replacing the one it had.
|
|
906
|
+
*
|
|
907
|
+
* Counts a set.
|
|
908
|
+
*
|
|
909
|
+
* @param key - the key to store the value for.
|
|
910
|
+
* @param value - the value to store.
|
|
911
|
+
*/
|
|
912
|
+
set(key, value) {
|
|
913
|
+
super.set(key, value);
|
|
914
|
+
++this.#stats.sets;
|
|
915
|
+
return this;
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* Get the stats as they are now.
|
|
919
|
+
*
|
|
920
|
+
* Later use of the cache doesn't change the result.
|
|
921
|
+
*/
|
|
922
|
+
stats() {
|
|
923
|
+
return this.#stats.snapshot();
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* Set every count in the stats to 0.
|
|
927
|
+
*
|
|
928
|
+
* The entries stay.
|
|
929
|
+
*/
|
|
930
|
+
resetStats() {
|
|
931
|
+
this.#stats.reset();
|
|
932
|
+
}
|
|
933
|
+
};
|
|
934
|
+
export { AutoResolveCache, AutoResolveMap, AutoResolveWeakCache, AutoResolveWeakMap, StrongWeakCache, StrongWeakMap, WeakWeakCache, WeakWeakMap, autoResolve };
|
|
935
|
+
|
|
936
|
+
//# sourceMappingURL=index.js.map
|