iterable-weak-set.js
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"use strict";
// An iterable WeakSet implementation inspired by the iterable WeakMap example code in the WeakRefs specification:
// https://github.com/tc39/proposal-weakrefs#iterable-weakmaps
module.exports = class IterableWeakSet {
constructor() {
this._refSet = new Set();
this._refMap = new WeakMap();
this._finalizationRegistry = new FinalizationRegistry(({ ref, set }) => set.delete(ref));
}
add(value) {
if (!this._refMap.has(value)) {
const ref = new WeakRef(value);
this._refMap.set(value, ref);
this._refSet.add(ref);
this._finalizationRegistry.register(value, { ref, set: this._refSet }, ref);
}
return this;
}
delete(value) {
const ref = this._refMap.get(value);
if (!ref) {
return false;
}
this._refMap.delete(value);
this._refSet.delete(ref);
this._finalizationRegistry.unregister(ref);
return true;
}
has(value) {
return this._refMap.has(value);
}
* [Symbol.iterator]() {
for (const ref of this._refSet) {
const value = ref.deref();
if (value === undefined) {
continue;
}
yield value;
}
}
};