mild-set 1.1.2 → 1.2.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/index.d.ts CHANGED
@@ -5,5 +5,10 @@ declare class MildSet<V> {
5
5
  add(value: V): this;
6
6
  delete(value: V): boolean;
7
7
  has(value: V): boolean;
8
+ [Symbol.iterator](): IterableIterator<V>;
9
+ keys(): IterableIterator<V>;
10
+ values(): IterableIterator<V>;
11
+ entries(): IterableIterator<[V, V]>;
12
+ forEach(callback: (value: V, key: V, set: MildSet<V>) => void, thisArg?: any): undefined;
8
13
  }
9
14
  export default MildSet;
package/dist/index.js CHANGED
@@ -5,9 +5,14 @@ class MildSet {
5
5
  /* VARIABLES */
6
6
  #strong = new Set();
7
7
  #weak = new WeakSet();
8
+ #weakRefs = new Set();
9
+ #weakRefsMap = new WeakMap();
8
10
  #size = 0;
9
- #finalizationRegistry = new FinalizationRegistry(() => this.#size -= 1);
10
11
  #finalizationTokens = new WeakMap();
12
+ #finalizationRegistry = new FinalizationRegistry(weakRef => {
13
+ this.#size -= 1;
14
+ this.#weakRefs.delete(weakRef);
15
+ });
11
16
  /* CONSTRUCTOR */
12
17
  constructor(entries) {
13
18
  if (entries) {
@@ -29,8 +34,11 @@ class MildSet {
29
34
  if (isWeakReferable(value)) {
30
35
  this.#weak.add(value);
31
36
  if (!hasValue) {
37
+ const weakRef = new WeakRef(value);
32
38
  const token = {};
33
- this.#finalizationRegistry.register(value, token, token);
39
+ this.#weakRefs.add(weakRef);
40
+ this.#weakRefsMap.set(value, weakRef);
41
+ this.#finalizationRegistry.register(value, weakRef, token);
34
42
  this.#finalizationTokens.set(value, token);
35
43
  }
36
44
  }
@@ -49,6 +57,11 @@ class MildSet {
49
57
  if (token) {
50
58
  this.#finalizationRegistry.unregister(token);
51
59
  this.#finalizationTokens.delete(value);
60
+ const weakRef = this.#weakRefsMap.get(value);
61
+ if (weakRef) {
62
+ this.#weakRefs.delete(weakRef);
63
+ this.#weakRefsMap.delete(value);
64
+ }
52
65
  }
53
66
  return this.#weak.delete(value);
54
67
  }
@@ -64,6 +77,36 @@ class MildSet {
64
77
  return this.#strong.has(value);
65
78
  }
66
79
  }
80
+ /* ITERATION API */
81
+ [Symbol.iterator]() {
82
+ return this.values();
83
+ }
84
+ *keys() {
85
+ yield* this.values();
86
+ }
87
+ *values() {
88
+ yield* this.#strong.values();
89
+ for (const weakRef of this.#weakRefs) {
90
+ const value = weakRef.deref();
91
+ if (value === undefined)
92
+ continue;
93
+ yield value;
94
+ }
95
+ }
96
+ *entries() {
97
+ yield* this.#strong.entries();
98
+ for (const weakRef of this.#weakRefs) {
99
+ const value = weakRef.deref();
100
+ if (value === undefined)
101
+ continue;
102
+ yield [value, value];
103
+ }
104
+ }
105
+ forEach(callback, thisArg) {
106
+ for (const [key, value] of this.entries()) {
107
+ callback.call(thisArg, value, key, this);
108
+ }
109
+ }
67
110
  }
68
111
  /* EXPORT */
69
112
  export default MildSet;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "repository": "github:fabiospampinato/mild-set",
4
4
  "description": "A WeakSet that supports any value, it holds strong references to primitives, and weak references to objects.",
5
5
  "license": "MIT",
6
- "version": "1.1.2",
6
+ "version": "1.2.0",
7
7
  "type": "module",
8
8
  "main": "dist/index.js",
9
9
  "exports": "./dist/index.js",
package/readme.md CHANGED
@@ -14,6 +14,8 @@ It has the same API of a WeakSet, except that it just works with primitives too.
14
14
 
15
15
  It also implements the `size` property, like a Set.
16
16
 
17
+ It also implements all the iteration methods, like a Set.
18
+
17
19
  ## License
18
20
 
19
21
  MIT © Fabio Spampinato