@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.
@@ -1,12 +1,8 @@
1
1
  /**
2
- * Extending answers a new list sharing everything already there, so whoever holds one reads
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
- * Both ends are known. {@link push} adds at the head and shares the whole list after it, which
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 object> {
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 | undefined>;
44
+ values(): MapIterator<V>;
47
45
  [Symbol.iterator](): MapIterator<[K, V]>;
48
46
  readonly [Symbol.toStringTag]: "KindaWeakMap";
49
47
  }
@@ -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)?.deref() === undefined) {
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
- if (this.has(key)) {
70
- this.delete(key);
69
+ const held = this.#map.get(key);
70
+ if (held) {
71
+ this.#registry.unregister(held);
71
72
  }
72
- this.#registry.register(value, key, value);
73
- this.#map.set(key, new WeakRef(value));
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 val = this.get(key);
78
- if (val !== undefined) {
79
- this.#registry.unregister(val);
79
+ const held = this.#map.get(key);
80
+ if (!held) {
81
+ return false;
80
82
  }
81
- return this.#map.delete(key);
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.#map.has(key);
91
+ return this.get(key) !== undefined;
88
92
  }
89
93
  clear() {
90
- this.values().filter(Boolean).forEach((p) => this.#registry.unregister(p));
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.#map.size;
103
+ return this.entries().reduce((count) => count + 1, 0);
100
104
  }
101
105
  entries() {
102
- return this.#map.entries().map(([key, ref]) => [key, ref.deref()]);
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.#map.keys();
110
+ return this.entries().map(([key]) => key);
106
111
  }
107
112
  values() {
108
- return this.#map.values().map((ref) => ref.deref());
113
+ return this.entries().map(([, value]) => value);
109
114
  }
110
115
  [Symbol.iterator]() {
111
116
  return this.entries();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhombus-toolkit/collections",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Collection data structures: ImmutableLinkedList (persistent, both ends known), KindaWeakMap (strong keys, weak values).",
5
5
  "repository": {
6
6
  "type": "git",