-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterableWeakMap.ts
145 lines (111 loc) · 3.06 KB
/
IterableWeakMap.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import indentString from "./indentString";
import type { inspect as Inspect, InspectOptions } from "node:util";
/**
* Creates an iterable weak map, values are only considered reachable if the keys are.
*/
export default class IterableWeakMap<K extends object, V>
extends WeakMap<K, { value: V; ref: WeakRef<K> }>
implements Iterable<[K, V]> {
#refSet = new Set<WeakRef<K>>();
#finalizationGroup = new FinalizationRegistry(IterableWeakMap.#cleanup);
#size = 0;
get size() {
return this.#size;
}
static #cleanup({ set, ref, map }: { set: Set<WeakRef<any>>; ref: WeakRef<any>; map: IterableWeakMap<any, any> }) {
set.delete(ref);
map.#size--;
}
constructor(iterable: Iterable<[K, V]> = []) {
super();
for (const [key, value] of iterable)
this.set(key, value);
}
// @ts-expect-error
set(key: K, value: V) {
const ref = new WeakRef(key);
super.set(key, { value, ref });
this.#refSet.add(ref);
this.#finalizationGroup.register(key, {
set: this.#refSet,
ref,
map: this,
}, ref);
this.#size++;
return this;
}
// @ts-expect-error
get(key: K) {
const entry = super.get(key);
return entry?.value;
}
delete(key: any): key is K {
const entry = super.get(key);
if (entry === undefined)
return this.#size--, false;
super.delete(key);
this.#refSet.delete(entry.ref);
this.#finalizationGroup.unregister(entry.ref);
return true;
}
has(key: any): key is K {
return super.has(key);
}
clear() {
for (const [key] of this)
this.delete(key);
}
forEach<This = undefined>(callbackfn: (this: This, key: K, value: V, map: this) => void, thisArg: This = undefined as This) {
for (const [key, value] of this)
callbackfn.call(thisArg, key, value, this);
return this;
}
toJSON() {
return [...this];
}
* [Symbol.iterator]() {
for (const ref of this.#refSet) {
const key = ref.deref();
if (key === undefined) {
this.#refSet.delete(ref);
this.#size--;
continue;
}
const { value } = super.get(key)!;
yield [key, value] as [K, V];
}
}
* entries() {
yield* this[Symbol.iterator]();
}
* keys() {
for (const [key] of this)
yield key;
}
* values() {
for (const [, value] of this)
yield value;
}
[Symbol.for("nodejs.util.inspect.custom")](depth: number, options: InspectOptions, inspect: typeof Inspect) {
const { size } = this,
{ maxArrayLength = 100 } = options,
result: [any, any][] = [],
newline = size > 4 ? "\n" : " ",
indent = size > 4 ? " " : "";
if (size === 0)
return `${ IterableWeakMap.name }(${ size }) {}`;
for (const [key, value] of this)
result.push([
inspect(key, { ...options, depth: depth + 1 }),
inspect(value, { ...options, depth: depth + 1 }),
]);
return `${ IterableWeakMap.name }(${ size }) {${ newline }${
result
.slice(0, maxArrayLength!)
.map(([k, v]) => indentString(`${ k } => ${ v }`, indent))
.join(`,${ newline }`)
}${ result.length > 1 ? "," : "" }${
result.length > maxArrayLength! ? `\n${ indentString("...", indent) } ${ result.length - maxArrayLength! } more items` : ""
}${ newline }}`;
}
}