diff --git a/src/vanilla/utils/proxyMap.ts b/src/vanilla/utils/proxyMap.ts index 593112ef..fda42d2f 100644 --- a/src/vanilla/utils/proxyMap.ts +++ b/src/vanilla/utils/proxyMap.ts @@ -1,17 +1,16 @@ import { proxy, unstable_getInternalStates } from '../../vanilla.ts' const { proxyStateMap, snapCache } = unstable_getInternalStates() -const maybeProxify = (x: any) => (typeof x === 'object' ? proxy({ x }).x : x) const isProxy = (x: any) => proxyStateMap.has(x) type InternalProxyObject = Map & { - data: Array + data: Array index: number toJSON: () => Map } export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { - const initialData: Array = [] + const initialData: Array = [] let initialIndex = 0 const indexMap = new Map() @@ -32,11 +31,8 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { 'proxyMap:\n\tinitial state must be iterable\n\t\ttip: structure should be [[key, value]]', ) } - for (const [k, v] of entries) { - const key = maybeProxify(k) - const value = maybeProxify(v) + for (const [key, value] of entries) { indexMap.set(key, initialIndex) - initialData[initialIndex++] = key initialData[initialIndex++] = value } } @@ -53,19 +49,17 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { }, get(key: K) { const map = getMapForThis(this) - const k = maybeProxify(key) - const index = map.get(k) + const index = map.get(key) if (index === undefined) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions this.index return undefined } - return this.data[index + 1] as V + return this.data[index] }, has(key: K) { const map = getMapForThis(this) - const k = maybeProxify(key) - const exists = map.has(k) + const exists = map.has(key) if (!exists) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions this.index @@ -76,17 +70,12 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { if (!isProxy(this)) { throw new Error('Cannot perform mutations on a snapshot') } - const k = maybeProxify(key) - const v = maybeProxify(value) - const index = indexMap.get(k) + const index = indexMap.get(key) if (index !== undefined) { - this.data[index + 1] = v + this.data[index] = value } else { - let nextIndex = this.index - indexMap.set(k, nextIndex) - this.data[nextIndex++] = k - this.data[nextIndex++] = v - this.index = nextIndex + indexMap.set(key, this.index) + this.data[this.index++] = value } return this }, @@ -94,14 +83,12 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { if (!isProxy(this)) { throw new Error('Cannot perform mutations on a snapshot') } - const k = maybeProxify(key) - const index = indexMap.get(k) + const index = indexMap.get(key) if (index === undefined) { return false } delete this.data[index] - delete this.data[index + 1] - indexMap.delete(k) + indexMap.delete(key) return true }, clear() { @@ -114,14 +101,14 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { }, forEach(cb: (value: V, key: K, map: Map) => void) { const map = getMapForThis(this) - map.forEach((index) => { - cb(this.data[index + 1] as V, this.data[index] as K, this) + map.forEach((index, key) => { + cb(this.data[index]!, key, this) }) }, *entries(): MapIterator<[K, V]> { const map = getMapForThis(this) - for (const index of map.values()) { - yield [this.data[index], this.data[index + 1]] as [K, V] + for (const [key, index] of map) { + yield [key, this.data[index]!] } }, *keys(): IterableIterator { @@ -133,7 +120,7 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { *values(): IterableIterator { const map = getMapForThis(this) for (const index of map.values()) { - yield this.data[index + 1] as V + yield this.data[index]! } }, [Symbol.iterator]() {