|
1 | 1 | import {
|
2 |
| - consumeKey, |
3 |
| - consumeCollection, |
4 |
| - dirtyKey, |
5 |
| - dirtyCollection |
6 |
| -} from './util'; |
| 2 | + TrackedStorage, |
| 3 | + createStorage, |
| 4 | + getValue, |
| 5 | + setValue, |
| 6 | +} from 'ember-tracked-storage-polyfill'; |
| 7 | + |
| 8 | +export class TrackedMap<K = unknown, V = unknown> implements Map<K, V> { |
| 9 | + private collection = createStorage(null, () => false); |
| 10 | + |
| 11 | + private storages: Map<K, TrackedStorage<null>> = new Map(); |
| 12 | + |
| 13 | + private vals: Map<K, V>; |
| 14 | + |
| 15 | + private readStorageFor(key: K): void { |
| 16 | + const { storages } = this; |
| 17 | + let storage = storages.get(key); |
| 18 | + |
| 19 | + if (storage === undefined) { |
| 20 | + storage = createStorage(null, () => false); |
| 21 | + storages.set(key, storage); |
| 22 | + } |
| 23 | + |
| 24 | + getValue(storage); |
| 25 | + } |
| 26 | + |
| 27 | + private dirtyStorageFor(key: K): void { |
| 28 | + const storage = this.storages.get(key); |
| 29 | + |
| 30 | + if (storage) { |
| 31 | + setValue(storage, null); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + constructor(entries?: readonly (readonly [K, V])[] | null) { |
| 36 | + this.vals = new Map(entries); |
| 37 | + } |
7 | 38 |
|
8 |
| -export class TrackedMap<K = unknown, V = unknown> extends Map<K, V> { |
9 | 39 | // **** KEY GETTERS ****
|
10 |
| - get(key: K) { |
11 |
| - consumeKey(this, key); |
| 40 | + get(key: K): V | undefined { |
| 41 | + // entangle the storage for the key |
| 42 | + this.readStorageFor(key); |
12 | 43 |
|
13 |
| - return super.get(key); |
| 44 | + return this.vals.get(key); |
14 | 45 | }
|
15 | 46 |
|
16 |
| - has(key: K) { |
17 |
| - consumeKey(this, key); |
| 47 | + has(key: K): boolean { |
| 48 | + this.readStorageFor(key); |
18 | 49 |
|
19 |
| - return super.has(key); |
| 50 | + return this.vals.has(key); |
20 | 51 | }
|
21 | 52 |
|
22 | 53 | // **** ALL GETTERS ****
|
23 |
| - entries() { |
24 |
| - consumeCollection(this); |
| 54 | + entries(): IterableIterator<[K, V]> { |
| 55 | + getValue(this.collection); |
25 | 56 |
|
26 |
| - return super.entries(); |
| 57 | + return this.vals.entries(); |
27 | 58 | }
|
28 | 59 |
|
29 |
| - keys() { |
30 |
| - consumeCollection(this); |
| 60 | + keys(): IterableIterator<K> { |
| 61 | + getValue(this.collection); |
31 | 62 |
|
32 |
| - return super.keys(); |
| 63 | + return this.vals.keys(); |
33 | 64 | }
|
34 | 65 |
|
35 |
| - values() { |
36 |
| - consumeCollection(this); |
| 66 | + values(): IterableIterator<V> { |
| 67 | + getValue(this.collection); |
37 | 68 |
|
38 |
| - return super.values(); |
| 69 | + return this.vals.values(); |
39 | 70 | }
|
40 | 71 |
|
41 |
| - forEach(fn: (value: V, key: K, map: this) => void) { |
42 |
| - consumeCollection(this); |
| 72 | + forEach(fn: (value: V, key: K, map: Map<K, V>) => void): void { |
| 73 | + getValue(this.collection); |
43 | 74 |
|
44 |
| - super.forEach(fn); |
| 75 | + this.vals.forEach(fn); |
45 | 76 | }
|
46 | 77 |
|
47 |
| - get size() { |
48 |
| - consumeCollection(this); |
| 78 | + get size(): number { |
| 79 | + getValue(this.collection); |
| 80 | + |
| 81 | + return this.vals.size; |
| 82 | + } |
| 83 | + |
| 84 | + [Symbol.iterator](): IterableIterator<[K, V]> { |
| 85 | + getValue(this.collection); |
| 86 | + |
| 87 | + return this.vals[Symbol.iterator](); |
| 88 | + } |
49 | 89 |
|
50 |
| - return super.size; |
| 90 | + get [Symbol.toStringTag](): string { |
| 91 | + return this.vals[Symbol.toStringTag]; |
51 | 92 | }
|
52 | 93 |
|
53 | 94 | // **** KEY SETTERS ****
|
54 |
| - set(key: K, value: V) { |
55 |
| - dirtyKey(this, key); |
56 |
| - dirtyCollection(this); |
| 95 | + set(key: K, value: V): this { |
| 96 | + this.dirtyStorageFor(key); |
| 97 | + setValue(this.collection, null); |
| 98 | + |
| 99 | + this.vals.set(key, value); |
57 | 100 |
|
58 |
| - return super.set(key, value); |
| 101 | + return this; |
59 | 102 | }
|
60 | 103 |
|
61 |
| - delete(key: K) { |
62 |
| - dirtyKey(this, key); |
63 |
| - dirtyCollection(this); |
| 104 | + delete(key: K): boolean { |
| 105 | + this.dirtyStorageFor(key); |
| 106 | + setValue(this.collection, null); |
64 | 107 |
|
65 |
| - return super.delete(key); |
| 108 | + return this.vals.delete(key); |
66 | 109 | }
|
67 | 110 |
|
68 | 111 | // **** ALL SETTERS ****
|
69 |
| - clear() { |
70 |
| - super.forEach((_v, k) => dirtyKey(this, k)); |
71 |
| - dirtyCollection(this); |
| 112 | + clear(): void { |
| 113 | + this.storages.forEach((s) => setValue(s, null)); |
| 114 | + setValue(this.collection, null); |
72 | 115 |
|
73 |
| - return super.clear(); |
| 116 | + this.vals.clear(); |
74 | 117 | }
|
75 | 118 | }
|
76 | 119 |
|
77 |
| -if (typeof Symbol !== undefined) { |
78 |
| - let originalIterator = TrackedMap.prototype[Symbol.iterator]; |
| 120 | +// So instanceof works |
| 121 | +Object.setPrototypeOf(TrackedMap, Map.prototype); |
| 122 | + |
| 123 | +export class TrackedWeakMap< |
| 124 | + K extends object = object, |
| 125 | + V = unknown |
| 126 | +> implements WeakMap<K, V> { |
| 127 | + private storages: WeakMap<K, TrackedStorage<null>> = new WeakMap(); |
| 128 | + |
| 129 | + private vals: WeakMap<K, V>; |
| 130 | + |
| 131 | + private readStorageFor(key: K): void { |
| 132 | + const { storages } = this; |
| 133 | + let storage = storages.get(key); |
79 | 134 |
|
80 |
| - Object.defineProperty(TrackedMap.prototype, Symbol.iterator, { |
81 |
| - get() { |
82 |
| - consumeCollection(this); |
83 |
| - return originalIterator; |
| 135 | + if (storage === undefined) { |
| 136 | + storage = createStorage(null, () => false); |
| 137 | + storages.set(key, storage); |
84 | 138 | }
|
85 |
| - }); |
86 |
| -} |
87 | 139 |
|
88 |
| -export class TrackedWeakMap<K extends object = object, V = unknown> extends WeakMap< |
89 |
| - K, |
90 |
| - V |
91 |
| -> { |
92 |
| - get(key: K) { |
93 |
| - consumeKey(this, key); |
| 140 | + getValue(storage); |
| 141 | + } |
| 142 | + |
| 143 | + private dirtyStorageFor(key: K): void { |
| 144 | + const storage = this.storages.get(key); |
| 145 | + |
| 146 | + if (storage) { |
| 147 | + setValue(storage, null); |
| 148 | + } |
| 149 | + } |
94 | 150 |
|
95 |
| - return super.get(key); |
| 151 | + constructor(iterable?: (readonly [K, V][] | null)) { |
| 152 | + this.vals = new WeakMap(iterable); |
96 | 153 | }
|
97 | 154 |
|
98 |
| - has(key: K) { |
99 |
| - consumeKey(this, key); |
| 155 | + get(key: K): V | undefined { |
| 156 | + this.readStorageFor(key); |
100 | 157 |
|
101 |
| - return super.has(key); |
| 158 | + return this.vals.get(key); |
102 | 159 | }
|
103 | 160 |
|
104 |
| - set(key: K, value: V) { |
105 |
| - dirtyKey(this, key); |
| 161 | + has(key: K): boolean { |
| 162 | + this.readStorageFor(key); |
106 | 163 |
|
107 |
| - return super.set(key, value); |
| 164 | + return this.vals.has(key); |
108 | 165 | }
|
109 | 166 |
|
110 |
| - delete(key: K) { |
111 |
| - dirtyKey(this, key); |
| 167 | + set(key: K, value: V): this { |
| 168 | + this.dirtyStorageFor(key); |
| 169 | + |
| 170 | + this.vals.set(key, value); |
112 | 171 |
|
113 |
| - return super.delete(key); |
| 172 | + return this; |
| 173 | + } |
| 174 | + |
| 175 | + delete(key: K): boolean { |
| 176 | + this.dirtyStorageFor(key); |
| 177 | + |
| 178 | + return this.vals.delete(key); |
| 179 | + } |
| 180 | + |
| 181 | + get [Symbol.toStringTag](): string { |
| 182 | + return this.vals[Symbol.toStringTag]; |
114 | 183 | }
|
115 | 184 | }
|
| 185 | + |
| 186 | +// So instanceof works |
| 187 | +Object.setPrototypeOf(TrackedWeakMap, WeakMap.prototype); |
0 commit comments