@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.d.ts
ADDED
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
/** Any object that can look up and store values by key, such as a `Map` or a `WeakMap`. */
|
|
2
|
+
interface MapLike<K, V> {
|
|
3
|
+
/**
|
|
4
|
+
* Get the value for a key, or `undefined` if there is none.
|
|
5
|
+
*
|
|
6
|
+
* @param key - the key to look up.
|
|
7
|
+
*/
|
|
8
|
+
get(key: K): V | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Check whether there is a value for a key.
|
|
11
|
+
*
|
|
12
|
+
* @param key - the key to look up.
|
|
13
|
+
*/
|
|
14
|
+
has(key: K): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Store a value for a key.
|
|
17
|
+
*
|
|
18
|
+
* @param key - the key to store the value for.
|
|
19
|
+
* @param value - the value to store.
|
|
20
|
+
*/
|
|
21
|
+
set(key: K, value: V): unknown;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get the value for a key, or compute and store it when there is none.
|
|
25
|
+
*
|
|
26
|
+
* It works with a map you already have. To create a map with this built in, use {@link AutoResolveMap}.
|
|
27
|
+
*
|
|
28
|
+
* - A stored `undefined` counts as a value.
|
|
29
|
+
* - Nothing is stored when the callback throws.
|
|
30
|
+
*
|
|
31
|
+
* @param map - the map to look in and store to.
|
|
32
|
+
* @param key - the key to look up.
|
|
33
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
34
|
+
* @example
|
|
35
|
+
* const lengths = new Map<string, number>();
|
|
36
|
+
* autoResolve(lengths, 'apple', (key) => key.length); // 5, computed and stored
|
|
37
|
+
* autoResolve(lengths, 'apple', (key) => key.length); // 5, from the map
|
|
38
|
+
*/
|
|
39
|
+
export declare function autoResolve<K, V>(map: MapLike<K, V>, key: NoInfer<K>, callback: (key: NoInfer<K>) => NoInfer<V>): V;
|
|
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
|
+
export declare class AutoResolveMap<K, V> extends Map<K, V> {
|
|
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: K, value: V): V;
|
|
56
|
+
/**
|
|
57
|
+
* Get the value for a key, or compute and store it when there is none.
|
|
58
|
+
*
|
|
59
|
+
* - A stored `undefined` counts as a value.
|
|
60
|
+
* - Nothing is stored when the callback throws.
|
|
61
|
+
*
|
|
62
|
+
* @param key - the key to look up.
|
|
63
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
64
|
+
*/
|
|
65
|
+
getOrInsertComputed(key: K, callback: (key: K) => V): V;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* A `WeakMap` that can store a value the first time it's asked for, and can be emptied.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const sizes = new AutoResolveWeakMap<object, number>();
|
|
72
|
+
* const point = { x: 1, y: 2 };
|
|
73
|
+
* sizes.getOrInsertComputed(point, (key) => Object.keys(key).length); // 2, computed and stored
|
|
74
|
+
* sizes.clear();
|
|
75
|
+
* sizes.has(point); // false
|
|
76
|
+
*/
|
|
77
|
+
export declare class AutoResolveWeakMap<K extends WeakKey, V> implements WeakMap<K, V> {
|
|
78
|
+
#private;
|
|
79
|
+
/**
|
|
80
|
+
* Create a map.
|
|
81
|
+
*
|
|
82
|
+
* @param entries - the keys and values to start with.
|
|
83
|
+
*/
|
|
84
|
+
constructor(entries?: Iterable<readonly [K, V]> | null);
|
|
85
|
+
/** Remove every entry, even when its key is still in use. */
|
|
86
|
+
clear(): void;
|
|
87
|
+
/**
|
|
88
|
+
* Remove the entry for a key.
|
|
89
|
+
*
|
|
90
|
+
* The result is `true` when there was one.
|
|
91
|
+
*
|
|
92
|
+
* @param key - the key to remove.
|
|
93
|
+
*/
|
|
94
|
+
delete(key: K): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Get the value for a key, or `undefined` if there is none.
|
|
97
|
+
*
|
|
98
|
+
* @param key - the key to look up.
|
|
99
|
+
*/
|
|
100
|
+
get(key: K): V | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Get the value for a key, or store the given one when there is none.
|
|
103
|
+
*
|
|
104
|
+
* @param key - the key to look up.
|
|
105
|
+
* @param value - the value to store when there is none.
|
|
106
|
+
*/
|
|
107
|
+
getOrInsert(key: K, value: V): V;
|
|
108
|
+
/**
|
|
109
|
+
* Get the value for a key, or compute and store it when there is none.
|
|
110
|
+
*
|
|
111
|
+
* - A stored `undefined` counts as a value.
|
|
112
|
+
* - Nothing is stored when the callback throws.
|
|
113
|
+
*
|
|
114
|
+
* @param key - the key to look up.
|
|
115
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
116
|
+
*/
|
|
117
|
+
getOrInsertComputed(key: K, callback: (key: K) => V): V;
|
|
118
|
+
/**
|
|
119
|
+
* Check whether there is a value for a key.
|
|
120
|
+
*
|
|
121
|
+
* @param key - the key to look up.
|
|
122
|
+
*/
|
|
123
|
+
has(key: K): boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Store a value for a key, replacing the one it had.
|
|
126
|
+
*
|
|
127
|
+
* @param key - the key to store the value for.
|
|
128
|
+
* @param value - the value to store.
|
|
129
|
+
*/
|
|
130
|
+
set(key: K, value: V): this;
|
|
131
|
+
/** The name `Object.prototype.toString` shows. */
|
|
132
|
+
get [Symbol.toStringTag](): string;
|
|
133
|
+
}
|
|
134
|
+
/** How often a cache was used, since it was created or its stats were reset. */
|
|
135
|
+
interface CacheStats {
|
|
136
|
+
/**
|
|
137
|
+
* The number of lookups that found a value.
|
|
138
|
+
*
|
|
139
|
+
* A lookup is `get`, `getOrInsert` or `getOrInsertComputed`, not `has`.
|
|
140
|
+
*/
|
|
141
|
+
hits: number;
|
|
142
|
+
/** The number of lookups that found no value. */
|
|
143
|
+
misses: number;
|
|
144
|
+
/** The number of values a callback computed and the cache stored. */
|
|
145
|
+
resolved: number;
|
|
146
|
+
/**
|
|
147
|
+
* The number of values stored without a callback, by `set` or `getOrInsert`.
|
|
148
|
+
*
|
|
149
|
+
* The entries a cache starts with aren't included.
|
|
150
|
+
*/
|
|
151
|
+
sets: number;
|
|
152
|
+
/** The number of calls to `delete`. */
|
|
153
|
+
deletes: number;
|
|
154
|
+
/** The number of calls to `clear`. */
|
|
155
|
+
clears: number;
|
|
156
|
+
}
|
|
157
|
+
interface CacheMethods {
|
|
158
|
+
/**
|
|
159
|
+
* Get the stats as they are now.
|
|
160
|
+
*
|
|
161
|
+
* Later use of the cache doesn't change the result.
|
|
162
|
+
*/
|
|
163
|
+
stats(): Readonly<CacheStats>;
|
|
164
|
+
/**
|
|
165
|
+
* Set every count in the stats to 0.
|
|
166
|
+
*
|
|
167
|
+
* The entries stay.
|
|
168
|
+
*/
|
|
169
|
+
resetStats(): void;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* A `Map` that doesn't keep its values alive.
|
|
173
|
+
*
|
|
174
|
+
* Once nothing else uses a value, it can be released (garbage collected). Its key is then as if it had never been
|
|
175
|
+
* there. The key itself is removed some time later, so `size` and `keys` may still include it.
|
|
176
|
+
*
|
|
177
|
+
* Any look at a value keeps it alive until the current turn of the event loop ends. That includes `has` and looping
|
|
178
|
+
* over the map.
|
|
179
|
+
*
|
|
180
|
+
* 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
|
|
181
|
+
* large objects.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* const dictionaries = new StrongWeakMap<string, { words: string[] }>();
|
|
185
|
+
* const colors = dictionaries.getOrInsertComputed('colors', () => ({ words: ['blue', 'red'] }));
|
|
186
|
+
* dictionaries.get('colors') === colors; // true, as long as something uses `colors`
|
|
187
|
+
*/
|
|
188
|
+
export declare class StrongWeakMap<K, V extends object> implements Map<K, V> {
|
|
189
|
+
#private;
|
|
190
|
+
/**
|
|
191
|
+
* Create a map.
|
|
192
|
+
*
|
|
193
|
+
* @param entries - the keys and values to start with.
|
|
194
|
+
*/
|
|
195
|
+
constructor(entries?: Iterable<readonly [K, V]> | null);
|
|
196
|
+
/**
|
|
197
|
+
* The number of keys.
|
|
198
|
+
*
|
|
199
|
+
* It may include keys whose value was released.
|
|
200
|
+
*/
|
|
201
|
+
get size(): number;
|
|
202
|
+
/** Remove every entry. */
|
|
203
|
+
clear(): void;
|
|
204
|
+
/**
|
|
205
|
+
* Remove the entry for a key.
|
|
206
|
+
*
|
|
207
|
+
* The result is `true` when there was one whose value hadn't been released.
|
|
208
|
+
*
|
|
209
|
+
* @param key - the key to remove.
|
|
210
|
+
*/
|
|
211
|
+
delete(key: K): boolean;
|
|
212
|
+
/** Get the key and value of each entry whose value wasn't released. */
|
|
213
|
+
entries(): MapIterator<[K, V]>;
|
|
214
|
+
/**
|
|
215
|
+
* Call a function for each entry whose value wasn't released.
|
|
216
|
+
*
|
|
217
|
+
* @param callback - called with the value, the key and the map.
|
|
218
|
+
* @param thisArg - the `this` of the callback.
|
|
219
|
+
*/
|
|
220
|
+
forEach(callback: (value: V, key: K, map: Map<K, V>) => void, thisArg?: unknown): void;
|
|
221
|
+
/**
|
|
222
|
+
* Get the value for a key, or `undefined` if there is none or it was released.
|
|
223
|
+
*
|
|
224
|
+
* @param key - the key to look up.
|
|
225
|
+
*/
|
|
226
|
+
get(key: K): V | undefined;
|
|
227
|
+
/**
|
|
228
|
+
* Get the value for a key, or store the given one when there is none or it was released.
|
|
229
|
+
*
|
|
230
|
+
* @param key - the key to look up.
|
|
231
|
+
* @param value - the value to store when there is none.
|
|
232
|
+
*/
|
|
233
|
+
getOrInsert(key: K, value: V): V;
|
|
234
|
+
/**
|
|
235
|
+
* Get the value for a key, or compute and store it when there is none or it was released.
|
|
236
|
+
*
|
|
237
|
+
* Nothing is stored when the callback throws.
|
|
238
|
+
*
|
|
239
|
+
* @param key - the key to look up.
|
|
240
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
241
|
+
*/
|
|
242
|
+
getOrInsertComputed(key: K, callback: (key: K) => V): V;
|
|
243
|
+
/**
|
|
244
|
+
* Check whether there is a value for a key.
|
|
245
|
+
*
|
|
246
|
+
* This keeps the value alive until the current turn of the event loop ends.
|
|
247
|
+
*
|
|
248
|
+
* @param key - the key to look up.
|
|
249
|
+
*/
|
|
250
|
+
has(key: K): boolean;
|
|
251
|
+
/**
|
|
252
|
+
* Get the keys.
|
|
253
|
+
*
|
|
254
|
+
* They may include keys whose value was released.
|
|
255
|
+
*/
|
|
256
|
+
keys(): MapIterator<K>;
|
|
257
|
+
/**
|
|
258
|
+
* Store a value for a key, replacing the one it had.
|
|
259
|
+
*
|
|
260
|
+
* @param key - the key to store the value for.
|
|
261
|
+
* @param value - the value to store.
|
|
262
|
+
*/
|
|
263
|
+
set(key: K, value: V): this;
|
|
264
|
+
/** Get the values that weren't released. */
|
|
265
|
+
values(): MapIterator<V>;
|
|
266
|
+
/** Get the key and value of each entry whose value wasn't released. */
|
|
267
|
+
[Symbol.iterator](): MapIterator<[K, V]>;
|
|
268
|
+
/** The name `Object.prototype.toString` shows. */
|
|
269
|
+
get [Symbol.toStringTag](): string;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* A `WeakMap` that doesn't keep its values alive either, and can be emptied.
|
|
273
|
+
*
|
|
274
|
+
* Once nothing else uses a value, it can be released (garbage collected). Its key is then as if it had never been
|
|
275
|
+
* there.
|
|
276
|
+
*
|
|
277
|
+
* Any look at a value keeps it alive until the current turn of the event loop ends. That includes `has`.
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* const parsed = new WeakWeakMap<URL, { hosts: string[] }>();
|
|
281
|
+
* const url = new URL('https://example.com');
|
|
282
|
+
* const result = parsed.getOrInsertComputed(url, (key) => ({ hosts: [key.host] }));
|
|
283
|
+
* parsed.get(url) === result; // true, as long as something uses `result`
|
|
284
|
+
*/
|
|
285
|
+
export declare class WeakWeakMap<K extends WeakKey, V extends object> implements WeakMap<K, V> {
|
|
286
|
+
#private;
|
|
287
|
+
/**
|
|
288
|
+
* Create a map.
|
|
289
|
+
*
|
|
290
|
+
* @param entries - the keys and values to start with.
|
|
291
|
+
*/
|
|
292
|
+
constructor(entries?: Iterable<readonly [K, V]> | null);
|
|
293
|
+
/** Remove every entry, even when its key and value are still in use. */
|
|
294
|
+
clear(): void;
|
|
295
|
+
/**
|
|
296
|
+
* Remove the entry for a key.
|
|
297
|
+
*
|
|
298
|
+
* The result is `true` when there was one whose value hadn't been released.
|
|
299
|
+
*
|
|
300
|
+
* @param key - the key to remove.
|
|
301
|
+
*/
|
|
302
|
+
delete(key: K): boolean;
|
|
303
|
+
/**
|
|
304
|
+
* Get the value for a key, or `undefined` if there is none or it was released.
|
|
305
|
+
*
|
|
306
|
+
* @param key - the key to look up.
|
|
307
|
+
*/
|
|
308
|
+
get(key: K): V | undefined;
|
|
309
|
+
/**
|
|
310
|
+
* Get the value for a key, or store the given one when there is none or it was released.
|
|
311
|
+
*
|
|
312
|
+
* @param key - the key to look up.
|
|
313
|
+
* @param value - the value to store when there is none.
|
|
314
|
+
*/
|
|
315
|
+
getOrInsert(key: K, value: V): V;
|
|
316
|
+
/**
|
|
317
|
+
* Get the value for a key, or compute and store it when there is none or it was released.
|
|
318
|
+
*
|
|
319
|
+
* Nothing is stored when the callback throws.
|
|
320
|
+
*
|
|
321
|
+
* @param key - the key to look up.
|
|
322
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
323
|
+
*/
|
|
324
|
+
getOrInsertComputed(key: K, callback: (key: K) => V): V;
|
|
325
|
+
/**
|
|
326
|
+
* Check whether there is a value for a key.
|
|
327
|
+
*
|
|
328
|
+
* This keeps the value alive until the current turn of the event loop ends.
|
|
329
|
+
*
|
|
330
|
+
* @param key - the key to look up.
|
|
331
|
+
*/
|
|
332
|
+
has(key: K): boolean;
|
|
333
|
+
/**
|
|
334
|
+
* Store a value for a key, replacing the one it had.
|
|
335
|
+
*
|
|
336
|
+
* @param key - the key to store the value for.
|
|
337
|
+
* @param value - the value to store.
|
|
338
|
+
*/
|
|
339
|
+
set(key: K, value: V): this;
|
|
340
|
+
/** The name `Object.prototype.toString` shows. */
|
|
341
|
+
get [Symbol.toStringTag](): string;
|
|
342
|
+
}
|
|
343
|
+
/** The same as {@link AutoResolveMap}, with stats on how often it's used. */
|
|
344
|
+
export declare class AutoResolveCache<K, V> extends AutoResolveMap<K, V> implements CacheMethods {
|
|
345
|
+
#private;
|
|
346
|
+
/**
|
|
347
|
+
* Create a cache.
|
|
348
|
+
*
|
|
349
|
+
* The entries it starts with don't count as sets.
|
|
350
|
+
*
|
|
351
|
+
* @param entries - the keys and values to start with.
|
|
352
|
+
*/
|
|
353
|
+
constructor(entries?: Iterable<readonly [K, V]> | null);
|
|
354
|
+
/**
|
|
355
|
+
* Remove every entry.
|
|
356
|
+
*
|
|
357
|
+
* Counts a call to `clear`. The other counts stay as they are.
|
|
358
|
+
*/
|
|
359
|
+
override clear(): void;
|
|
360
|
+
/**
|
|
361
|
+
* Remove the entry for a key.
|
|
362
|
+
*
|
|
363
|
+
* The result is `true` when there was one.
|
|
364
|
+
*
|
|
365
|
+
* Counts a call, also when there was no entry.
|
|
366
|
+
*
|
|
367
|
+
* @param key - the key to remove.
|
|
368
|
+
*/
|
|
369
|
+
override delete(key: K): boolean;
|
|
370
|
+
/**
|
|
371
|
+
* Get the value for a key, or `undefined` if there is none.
|
|
372
|
+
*
|
|
373
|
+
* Counts a hit or a miss.
|
|
374
|
+
*
|
|
375
|
+
* @param key - the key to look up.
|
|
376
|
+
*/
|
|
377
|
+
override get(key: K): V | undefined;
|
|
378
|
+
/**
|
|
379
|
+
* Get the value for a key, or store the given one when there is none.
|
|
380
|
+
*
|
|
381
|
+
* Counts a hit, or a miss and a set.
|
|
382
|
+
*
|
|
383
|
+
* @param key - the key to look up.
|
|
384
|
+
* @param value - the value to store when there is none.
|
|
385
|
+
*/
|
|
386
|
+
override getOrInsert(key: K, value: V): V;
|
|
387
|
+
/**
|
|
388
|
+
* Get the value for a key, or compute and store it when there is none.
|
|
389
|
+
*
|
|
390
|
+
* - A stored `undefined` counts as a value.
|
|
391
|
+
* - Nothing is stored when the callback throws.
|
|
392
|
+
*
|
|
393
|
+
* Counts a hit, or a miss. A stored result counts as resolved.
|
|
394
|
+
*
|
|
395
|
+
* @param key - the key to look up.
|
|
396
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
397
|
+
*/
|
|
398
|
+
override getOrInsertComputed(key: K, callback: (key: K) => V): V;
|
|
399
|
+
/**
|
|
400
|
+
* Store a value for a key, replacing the one it had.
|
|
401
|
+
*
|
|
402
|
+
* Counts a set.
|
|
403
|
+
*
|
|
404
|
+
* @param key - the key to store the value for.
|
|
405
|
+
* @param value - the value to store.
|
|
406
|
+
*/
|
|
407
|
+
override set(key: K, value: V): this;
|
|
408
|
+
/**
|
|
409
|
+
* Get the stats as they are now.
|
|
410
|
+
*
|
|
411
|
+
* Later use of the cache doesn't change the result.
|
|
412
|
+
*/
|
|
413
|
+
stats(): Readonly<CacheStats>;
|
|
414
|
+
/**
|
|
415
|
+
* Set every count in the stats to 0.
|
|
416
|
+
*
|
|
417
|
+
* The entries stay.
|
|
418
|
+
*/
|
|
419
|
+
resetStats(): void;
|
|
420
|
+
}
|
|
421
|
+
/** The same as {@link AutoResolveWeakMap}, with stats on how often it's used. */
|
|
422
|
+
export declare class AutoResolveWeakCache<K extends WeakKey, V> extends AutoResolveWeakMap<K, V> implements CacheMethods {
|
|
423
|
+
#private;
|
|
424
|
+
/**
|
|
425
|
+
* Remove every entry, even when its key is still in use.
|
|
426
|
+
*
|
|
427
|
+
* Counts a call to `clear`. The other counts stay as they are.
|
|
428
|
+
*/
|
|
429
|
+
override clear(): void;
|
|
430
|
+
/**
|
|
431
|
+
* Remove the entry for a key.
|
|
432
|
+
*
|
|
433
|
+
* The result is `true` when there was one.
|
|
434
|
+
*
|
|
435
|
+
* Counts a call, also when there was no entry.
|
|
436
|
+
*
|
|
437
|
+
* @param key - the key to remove.
|
|
438
|
+
*/
|
|
439
|
+
override delete(key: K): boolean;
|
|
440
|
+
/**
|
|
441
|
+
* Get the value for a key, or `undefined` if there is none.
|
|
442
|
+
*
|
|
443
|
+
* Counts a hit or a miss.
|
|
444
|
+
*
|
|
445
|
+
* @param key - the key to look up.
|
|
446
|
+
*/
|
|
447
|
+
override get(key: K): V | undefined;
|
|
448
|
+
/**
|
|
449
|
+
* Get the value for a key, or store the given one when there is none.
|
|
450
|
+
*
|
|
451
|
+
* Counts a hit, or a miss and a set.
|
|
452
|
+
*
|
|
453
|
+
* @param key - the key to look up.
|
|
454
|
+
* @param value - the value to store when there is none.
|
|
455
|
+
*/
|
|
456
|
+
override getOrInsert(key: K, value: V): V;
|
|
457
|
+
/**
|
|
458
|
+
* Get the value for a key, or compute and store it when there is none.
|
|
459
|
+
*
|
|
460
|
+
* - A stored `undefined` counts as a value.
|
|
461
|
+
* - Nothing is stored when the callback throws.
|
|
462
|
+
*
|
|
463
|
+
* Counts a hit, or a miss. A stored result counts as resolved.
|
|
464
|
+
*
|
|
465
|
+
* @param key - the key to look up.
|
|
466
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
467
|
+
*/
|
|
468
|
+
override getOrInsertComputed(key: K, callback: (key: K) => V): V;
|
|
469
|
+
/**
|
|
470
|
+
* Store a value for a key, replacing the one it had.
|
|
471
|
+
*
|
|
472
|
+
* Counts a set.
|
|
473
|
+
*
|
|
474
|
+
* @param key - the key to store the value for.
|
|
475
|
+
* @param value - the value to store.
|
|
476
|
+
*/
|
|
477
|
+
override set(key: K, value: V): this;
|
|
478
|
+
/**
|
|
479
|
+
* Get the stats as they are now.
|
|
480
|
+
*
|
|
481
|
+
* Later use of the cache doesn't change the result.
|
|
482
|
+
*/
|
|
483
|
+
stats(): Readonly<CacheStats>;
|
|
484
|
+
/**
|
|
485
|
+
* Set every count in the stats to 0.
|
|
486
|
+
*
|
|
487
|
+
* The entries stay.
|
|
488
|
+
*/
|
|
489
|
+
resetStats(): void;
|
|
490
|
+
}
|
|
491
|
+
/** The same as {@link StrongWeakMap}, with stats on how often it's used. */
|
|
492
|
+
export declare class StrongWeakCache<K, V extends object> extends StrongWeakMap<K, V> implements CacheMethods {
|
|
493
|
+
#private;
|
|
494
|
+
/**
|
|
495
|
+
* Remove every entry.
|
|
496
|
+
*
|
|
497
|
+
* Counts a call to `clear`. The other counts stay as they are.
|
|
498
|
+
*/
|
|
499
|
+
override clear(): void;
|
|
500
|
+
/**
|
|
501
|
+
* Remove the entry for a key.
|
|
502
|
+
*
|
|
503
|
+
* The result is `true` when there was one whose value hadn't been released.
|
|
504
|
+
*
|
|
505
|
+
* Counts a call, also when there was no entry.
|
|
506
|
+
*
|
|
507
|
+
* @param key - the key to remove.
|
|
508
|
+
*/
|
|
509
|
+
override delete(key: K): boolean;
|
|
510
|
+
/**
|
|
511
|
+
* Get the value for a key, or `undefined` if there is none or it was released.
|
|
512
|
+
*
|
|
513
|
+
* Counts a hit or a miss.
|
|
514
|
+
*
|
|
515
|
+
* @param key - the key to look up.
|
|
516
|
+
*/
|
|
517
|
+
override get(key: K): V | undefined;
|
|
518
|
+
/**
|
|
519
|
+
* Get the value for a key, or store the given one when there is none or it was released.
|
|
520
|
+
*
|
|
521
|
+
* Counts a hit, or a miss and a set.
|
|
522
|
+
*
|
|
523
|
+
* @param key - the key to look up.
|
|
524
|
+
* @param value - the value to store when there is none.
|
|
525
|
+
*/
|
|
526
|
+
override getOrInsert(key: K, value: V): V;
|
|
527
|
+
/**
|
|
528
|
+
* Get the value for a key, or compute and store it when there is none or it was released.
|
|
529
|
+
*
|
|
530
|
+
* Nothing is stored when the callback throws.
|
|
531
|
+
*
|
|
532
|
+
* Counts a hit, or a miss. A stored result counts as resolved.
|
|
533
|
+
*
|
|
534
|
+
* @param key - the key to look up.
|
|
535
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
536
|
+
*/
|
|
537
|
+
override getOrInsertComputed(key: K, callback: (key: K) => V): V;
|
|
538
|
+
/**
|
|
539
|
+
* Store a value for a key, replacing the one it had.
|
|
540
|
+
*
|
|
541
|
+
* Counts a set.
|
|
542
|
+
*
|
|
543
|
+
* @param key - the key to store the value for.
|
|
544
|
+
* @param value - the value to store.
|
|
545
|
+
*/
|
|
546
|
+
override set(key: K, value: V): this;
|
|
547
|
+
/**
|
|
548
|
+
* Get the stats as they are now.
|
|
549
|
+
*
|
|
550
|
+
* Later use of the cache doesn't change the result.
|
|
551
|
+
*/
|
|
552
|
+
stats(): Readonly<CacheStats>;
|
|
553
|
+
/**
|
|
554
|
+
* Set every count in the stats to 0.
|
|
555
|
+
*
|
|
556
|
+
* The entries stay.
|
|
557
|
+
*/
|
|
558
|
+
resetStats(): void;
|
|
559
|
+
}
|
|
560
|
+
/** The same as {@link WeakWeakMap}, with stats on how often it's used. */
|
|
561
|
+
export declare class WeakWeakCache<K extends WeakKey, V extends object> extends WeakWeakMap<K, V> implements CacheMethods {
|
|
562
|
+
#private;
|
|
563
|
+
/**
|
|
564
|
+
* Remove every entry, even when its key and value are still in use.
|
|
565
|
+
*
|
|
566
|
+
* Counts a call to `clear`. The other counts stay as they are.
|
|
567
|
+
*/
|
|
568
|
+
override clear(): void;
|
|
569
|
+
/**
|
|
570
|
+
* Remove the entry for a key.
|
|
571
|
+
*
|
|
572
|
+
* The result is `true` when there was one whose value hadn't been released.
|
|
573
|
+
*
|
|
574
|
+
* Counts a call, also when there was no entry.
|
|
575
|
+
*
|
|
576
|
+
* @param key - the key to remove.
|
|
577
|
+
*/
|
|
578
|
+
override delete(key: K): boolean;
|
|
579
|
+
/**
|
|
580
|
+
* Get the value for a key, or `undefined` if there is none or it was released.
|
|
581
|
+
*
|
|
582
|
+
* Counts a hit or a miss.
|
|
583
|
+
*
|
|
584
|
+
* @param key - the key to look up.
|
|
585
|
+
*/
|
|
586
|
+
override get(key: K): V | undefined;
|
|
587
|
+
/**
|
|
588
|
+
* Get the value for a key, or store the given one when there is none or it was released.
|
|
589
|
+
*
|
|
590
|
+
* Counts a hit, or a miss and a set.
|
|
591
|
+
*
|
|
592
|
+
* @param key - the key to look up.
|
|
593
|
+
* @param value - the value to store when there is none.
|
|
594
|
+
*/
|
|
595
|
+
override getOrInsert(key: K, value: V): V;
|
|
596
|
+
/**
|
|
597
|
+
* Get the value for a key, or compute and store it when there is none or it was released.
|
|
598
|
+
*
|
|
599
|
+
* Nothing is stored when the callback throws.
|
|
600
|
+
*
|
|
601
|
+
* Counts a hit, or a miss. A stored result counts as resolved.
|
|
602
|
+
*
|
|
603
|
+
* @param key - the key to look up.
|
|
604
|
+
* @param callback - computes the value. It's called with the key, only when there is no value.
|
|
605
|
+
*/
|
|
606
|
+
override getOrInsertComputed(key: K, callback: (key: K) => V): V;
|
|
607
|
+
/**
|
|
608
|
+
* Store a value for a key, replacing the one it had.
|
|
609
|
+
*
|
|
610
|
+
* Counts a set.
|
|
611
|
+
*
|
|
612
|
+
* @param key - the key to store the value for.
|
|
613
|
+
* @param value - the value to store.
|
|
614
|
+
*/
|
|
615
|
+
override set(key: K, value: V): this;
|
|
616
|
+
/**
|
|
617
|
+
* Get the stats as they are now.
|
|
618
|
+
*
|
|
619
|
+
* Later use of the cache doesn't change the result.
|
|
620
|
+
*/
|
|
621
|
+
stats(): Readonly<CacheStats>;
|
|
622
|
+
/**
|
|
623
|
+
* Set every count in the stats to 0.
|
|
624
|
+
*
|
|
625
|
+
* The entries stay.
|
|
626
|
+
*/
|
|
627
|
+
resetStats(): void;
|
|
628
|
+
}
|
|
629
|
+
export type { CacheMethods, CacheStats, MapLike };
|