-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmap.ts
204 lines (151 loc) · 4.35 KB
/
map.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {
TrackedStorage,
createStorage,
getValue,
setValue,
} from 'ember-tracked-storage-polyfill';
export class TrackedMap<K = unknown, V = unknown> implements Map<K, V> {
private collection = createStorage(null, () => false);
private storages: Map<K, TrackedStorage<null>> = new Map();
private vals: Map<K, V>;
private readStorageFor(key: K): void {
const { storages } = this;
let storage = storages.get(key);
if (storage === undefined) {
storage = createStorage(null, () => false);
storages.set(key, storage);
}
getValue(storage);
}
private dirtyStorageFor(key: K): void {
const storage = this.storages.get(key);
if (storage) {
setValue(storage, null);
}
}
constructor();
constructor(entries: readonly (readonly [K, V])[] | null);
constructor(iterable: Iterable<readonly [K, V]>);
constructor(
existing?:
| readonly (readonly [K, V])[]
| Iterable<readonly [K, V]>
| null
| undefined
) {
// TypeScript doesn't correctly resolve the overloads for calling the `Map`
// constructor for the no-value constructor. This resolves that.
this.vals = existing ? new Map(existing) : new Map();
}
// **** KEY GETTERS ****
get(key: K): V | undefined {
// entangle the storage for the key
this.readStorageFor(key);
return this.vals.get(key);
}
has(key: K): boolean {
this.readStorageFor(key);
return this.vals.has(key);
}
// **** ALL GETTERS ****
entries(): IterableIterator<[K, V]> {
getValue(this.collection);
return this.vals.entries();
}
keys(): IterableIterator<K> {
getValue(this.collection);
return this.vals.keys();
}
values(): IterableIterator<V> {
getValue(this.collection);
return this.vals.values();
}
forEach(fn: (value: V, key: K, map: Map<K, V>) => void): void {
getValue(this.collection);
this.vals.forEach(fn);
}
get size(): number {
getValue(this.collection);
return this.vals.size;
}
[Symbol.iterator](): IterableIterator<[K, V]> {
getValue(this.collection);
return this.vals[Symbol.iterator]();
}
get [Symbol.toStringTag](): string {
return this.vals[Symbol.toStringTag];
}
// **** KEY SETTERS ****
set(key: K, value: V): this {
this.dirtyStorageFor(key);
setValue(this.collection, null);
this.vals.set(key, value);
return this;
}
delete(key: K): boolean {
this.dirtyStorageFor(key);
setValue(this.collection, null);
return this.vals.delete(key);
}
// **** ALL SETTERS ****
clear(): void {
this.storages.forEach((s) => setValue(s, null));
setValue(this.collection, null);
this.vals.clear();
}
}
// So instanceof works
Object.setPrototypeOf(TrackedMap.prototype, Map.prototype);
export class TrackedWeakMap<K extends object = object, V = unknown>
implements WeakMap<K, V>
{
private storages: WeakMap<K, TrackedStorage<null>> = new WeakMap();
private vals: WeakMap<K, V>;
private readStorageFor(key: K): void {
const { storages } = this;
let storage = storages.get(key);
if (storage === undefined) {
storage = createStorage(null, () => false);
storages.set(key, storage);
}
getValue(storage);
}
private dirtyStorageFor(key: K): void {
const storage = this.storages.get(key);
if (storage) {
setValue(storage, null);
}
}
constructor();
constructor(iterable: Iterable<readonly [K, V]>);
constructor(entries: readonly [K, V][] | null);
constructor(
existing?: readonly [K, V][] | Iterable<readonly [K, V]> | null | undefined
) {
// TypeScript doesn't correctly resolve the overloads for calling the `Map`
// constructor for the no-value constructor. This resolves that.
this.vals = existing ? new WeakMap(existing) : new WeakMap();
}
get(key: K): V | undefined {
this.readStorageFor(key);
return this.vals.get(key);
}
has(key: K): boolean {
this.readStorageFor(key);
return this.vals.has(key);
}
set(key: K, value: V): this {
this.dirtyStorageFor(key);
this.vals.set(key, value);
return this;
}
delete(key: K): boolean {
this.dirtyStorageFor(key);
return this.vals.delete(key);
}
get [Symbol.toStringTag](): string {
return this.vals[Symbol.toStringTag];
}
}
// So instanceof works
Object.setPrototypeOf(TrackedWeakMap.prototype, WeakMap.prototype);