@rhombus-toolkit/collections 2.0.0 → 2.0.2
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 +6 -8
- package/dist/bundle/index.js +21 -16
- package/package.json +1 -1
package/dist/bundle/index.d.ts
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* exactly what it held however much is added or removed afterwards.
|
|
2
|
+
* A persistent singly-linked list — extending shares everything already there.
|
|
4
3
|
*
|
|
5
4
|
* @remarks
|
|
6
|
-
*
|
|
7
|
-
* leaves the tail the same link it already was — so a list knows what it ends with without ever
|
|
8
|
-
* walking to find out. {@link tailToHead} is settled once per list and answered from then on, which
|
|
9
|
-
* is what lets a list be read from its tail as often as wanted without paying to reverse it.
|
|
5
|
+
* {@link tail} is O(1); {@link tailToHead} reverses once and caches the result.
|
|
10
6
|
*/
|
|
11
7
|
declare class ImmutableLinkedList<T> implements Iterable<T> {
|
|
12
8
|
#private;
|
|
@@ -32,18 +28,20 @@ declare class ImmutableLinkedList<T> implements Iterable<T> {
|
|
|
32
28
|
tailToHead(): readonly T[];
|
|
33
29
|
}
|
|
34
30
|
|
|
35
|
-
declare class KindaWeakMap<K, V extends
|
|
31
|
+
declare class KindaWeakMap<K, V extends WeakKey> {
|
|
36
32
|
#private;
|
|
33
|
+
/** An existing key keeps its place in iteration order, as with `Map`. */
|
|
37
34
|
set(key: K, value: V): this;
|
|
38
35
|
delete(key: K): boolean;
|
|
39
36
|
get(key: K): V | undefined;
|
|
40
37
|
has(key: K): boolean;
|
|
41
38
|
clear(): void;
|
|
42
39
|
forEach(callbackfn: (value: V, key: K, map: this) => void, thisArg?: any): void;
|
|
40
|
+
/** Counts only entries whose value is still alive, so it walks every ref. */
|
|
43
41
|
get size(): number;
|
|
44
42
|
entries(): MapIterator<[K, V]>;
|
|
45
43
|
keys(): MapIterator<K>;
|
|
46
|
-
values(): MapIterator<V
|
|
44
|
+
values(): MapIterator<V>;
|
|
47
45
|
[Symbol.iterator](): MapIterator<[K, V]>;
|
|
48
46
|
readonly [Symbol.toStringTag]: "KindaWeakMap";
|
|
49
47
|
}
|
package/dist/bundle/index.js
CHANGED
|
@@ -60,34 +60,38 @@ function tailOf(link) {
|
|
|
60
60
|
// src/KindaWeakMap.ts
|
|
61
61
|
class KindaWeakMap {
|
|
62
62
|
#map = new Map;
|
|
63
|
-
#registry = new FinalizationRegistry((key) => {
|
|
64
|
-
if (this.#map.get(key)
|
|
63
|
+
#registry = new FinalizationRegistry(({ key, ref }) => {
|
|
64
|
+
if (this.#map.get(key) === ref) {
|
|
65
65
|
this.#map.delete(key);
|
|
66
66
|
}
|
|
67
67
|
});
|
|
68
68
|
set(key, value) {
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
const held = this.#map.get(key);
|
|
70
|
+
if (held) {
|
|
71
|
+
this.#registry.unregister(held);
|
|
71
72
|
}
|
|
72
|
-
|
|
73
|
-
this.#
|
|
73
|
+
const ref = new WeakRef(value);
|
|
74
|
+
this.#registry.register(value, { key, ref }, ref);
|
|
75
|
+
this.#map.set(key, ref);
|
|
74
76
|
return this;
|
|
75
77
|
}
|
|
76
78
|
delete(key) {
|
|
77
|
-
const
|
|
78
|
-
if (
|
|
79
|
-
|
|
79
|
+
const held = this.#map.get(key);
|
|
80
|
+
if (!held) {
|
|
81
|
+
return false;
|
|
80
82
|
}
|
|
81
|
-
|
|
83
|
+
this.#map.delete(key);
|
|
84
|
+
this.#registry.unregister(held);
|
|
85
|
+
return held.deref() !== undefined;
|
|
82
86
|
}
|
|
83
87
|
get(key) {
|
|
84
88
|
return this.#map.get(key)?.deref();
|
|
85
89
|
}
|
|
86
90
|
has(key) {
|
|
87
|
-
return this
|
|
91
|
+
return this.get(key) !== undefined;
|
|
88
92
|
}
|
|
89
93
|
clear() {
|
|
90
|
-
this.
|
|
94
|
+
this.#map.forEach((ref) => this.#registry.unregister(ref));
|
|
91
95
|
this.#map.clear();
|
|
92
96
|
}
|
|
93
97
|
forEach(callbackfn, thisArg) {
|
|
@@ -96,16 +100,17 @@ class KindaWeakMap {
|
|
|
96
100
|
}
|
|
97
101
|
}
|
|
98
102
|
get size() {
|
|
99
|
-
return this
|
|
103
|
+
return this.entries().reduce((count) => count + 1, 0);
|
|
100
104
|
}
|
|
101
105
|
entries() {
|
|
102
|
-
|
|
106
|
+
const held = this.#map.entries().map(([key, ref]) => [key, ref.deref()]);
|
|
107
|
+
return held.filter((entry) => entry[1] !== undefined);
|
|
103
108
|
}
|
|
104
109
|
keys() {
|
|
105
|
-
return this
|
|
110
|
+
return this.entries().map(([key]) => key);
|
|
106
111
|
}
|
|
107
112
|
values() {
|
|
108
|
-
return this
|
|
113
|
+
return this.entries().map(([, value]) => value);
|
|
109
114
|
}
|
|
110
115
|
[Symbol.iterator]() {
|
|
111
116
|
return this.entries();
|
package/package.json
CHANGED