@rhombus-toolkit/collections 3.0.0 → 3.1.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/dist/bundle/index.d.ts +27 -1
- package/dist/bundle/index.js +67 -1
- package/package.json +2 -2
package/dist/bundle/index.d.ts
CHANGED
|
@@ -103,4 +103,30 @@ declare class MultiKeyWeakMap<in out Keys extends readonly unknown[] = unknown[]
|
|
|
103
103
|
getOrInsertComputed(keys: Keys, compute: Func<[Keys], Value>): Value;
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
/**
|
|
107
|
+
* A `Map` whose values are held weakly, so an entry is removed once nothing else holds its value.
|
|
108
|
+
*
|
|
109
|
+
* @remarks
|
|
110
|
+
* Not a `Map` by `instanceof`.
|
|
111
|
+
*/
|
|
112
|
+
declare class WeakValuedMap<K, V extends WeakKey> implements Map<K, V> {
|
|
113
|
+
#private;
|
|
114
|
+
constructor();
|
|
115
|
+
constructor(entries: Iterable<readonly [K, V]> | null);
|
|
116
|
+
/** Re-setting an existing key leaves its place in iteration order unchanged, as with `Map`. */
|
|
117
|
+
set(key: K, value: V): this;
|
|
118
|
+
delete(key: K): boolean;
|
|
119
|
+
get(key: K): V | undefined;
|
|
120
|
+
has(key: K): boolean;
|
|
121
|
+
clear(): void;
|
|
122
|
+
forEach(callbackfn: (value: V, key: K, map: this) => void, thisArg?: any): void;
|
|
123
|
+
/** Computed by walking every entry and counting those whose value is still alive. */
|
|
124
|
+
get size(): number;
|
|
125
|
+
entries(): MapIterator<[K, V]>;
|
|
126
|
+
keys(): MapIterator<K>;
|
|
127
|
+
values(): MapIterator<V>;
|
|
128
|
+
[Symbol.iterator](): MapIterator<[K, V]>;
|
|
129
|
+
readonly [Symbol.toStringTag]: "WeakValuedMap";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export { AutoStack, ImmutableLinkedList, KindaWeakMap, MultiKeyWeakMap, WeakValuedMap };
|
package/dist/bundle/index.js
CHANGED
|
@@ -218,9 +218,75 @@ class MultiKeyWeakMap {
|
|
|
218
218
|
return (this.#ensureNodeAt(keys).entry ??= { value: compute(keys) }).value;
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
|
+
// src/WeakValuedMap.ts
|
|
222
|
+
class WeakValuedMap {
|
|
223
|
+
#map = new Map;
|
|
224
|
+
#registry = new FinalizationRegistry(({ key, ref }) => {
|
|
225
|
+
if (this.#map.get(key) === ref) {
|
|
226
|
+
this.#map.delete(key);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
constructor(entries) {
|
|
230
|
+
for (const [key, value] of entries ?? []) {
|
|
231
|
+
this.set(key, value);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
set(key, value) {
|
|
235
|
+
const held = this.#map.get(key);
|
|
236
|
+
if (held) {
|
|
237
|
+
this.#registry.unregister(held);
|
|
238
|
+
}
|
|
239
|
+
const ref = new WeakRef(value);
|
|
240
|
+
this.#registry.register(value, { key, ref }, ref);
|
|
241
|
+
this.#map.set(key, ref);
|
|
242
|
+
return this;
|
|
243
|
+
}
|
|
244
|
+
delete(key) {
|
|
245
|
+
const held = this.#map.get(key);
|
|
246
|
+
if (!held) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
this.#map.delete(key);
|
|
250
|
+
this.#registry.unregister(held);
|
|
251
|
+
return held.deref() !== undefined;
|
|
252
|
+
}
|
|
253
|
+
get(key) {
|
|
254
|
+
return this.#map.get(key)?.deref();
|
|
255
|
+
}
|
|
256
|
+
has(key) {
|
|
257
|
+
return this.get(key) !== undefined;
|
|
258
|
+
}
|
|
259
|
+
clear() {
|
|
260
|
+
this.#map.forEach((ref) => this.#registry.unregister(ref));
|
|
261
|
+
this.#map.clear();
|
|
262
|
+
}
|
|
263
|
+
forEach(callbackfn, thisArg) {
|
|
264
|
+
for (const [key, value] of this) {
|
|
265
|
+
callbackfn.call(thisArg, value, key, this);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
get size() {
|
|
269
|
+
return this.entries().reduce((count) => count + 1, 0);
|
|
270
|
+
}
|
|
271
|
+
entries() {
|
|
272
|
+
const held = this.#map.entries().map(([key, ref]) => [key, ref.deref()]);
|
|
273
|
+
return held.filter((entry) => entry[1] !== undefined);
|
|
274
|
+
}
|
|
275
|
+
keys() {
|
|
276
|
+
return this.entries().map(([key]) => key);
|
|
277
|
+
}
|
|
278
|
+
values() {
|
|
279
|
+
return this.entries().map(([, value]) => value);
|
|
280
|
+
}
|
|
281
|
+
[Symbol.iterator]() {
|
|
282
|
+
return this.entries();
|
|
283
|
+
}
|
|
284
|
+
[Symbol.toStringTag] = "WeakValuedMap";
|
|
285
|
+
}
|
|
221
286
|
export {
|
|
222
287
|
AutoStack,
|
|
223
288
|
ImmutableLinkedList,
|
|
224
289
|
KindaWeakMap,
|
|
225
|
-
MultiKeyWeakMap
|
|
290
|
+
MultiKeyWeakMap,
|
|
291
|
+
WeakValuedMap
|
|
226
292
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rhombus-toolkit/collections",
|
|
3
|
-
"version": "3.
|
|
4
|
-
"description": "Collection data structures: AutoStack (entries leave with their scope), ImmutableLinkedList (persistent, both ends known), KindaWeakMap (weak where it can, strong otherwise), MultiKeyWeakMap (a KindaWeakMap keyed by a tuple).",
|
|
3
|
+
"version": "3.1.0",
|
|
4
|
+
"description": "Collection data structures: AutoStack (entries leave with their scope), ImmutableLinkedList (persistent, both ends known), KindaWeakMap (weak where it can, strong otherwise), MultiKeyWeakMap (a KindaWeakMap keyed by a tuple), WeakValuedMap (values held weakly).",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/rhombus-toolkit/ts.git",
|