diff --git a/src/vanilla/utils/proxySet.ts b/src/vanilla/utils/proxySet.ts index 0f4f41ba..ecdddcf7 100644 --- a/src/vanilla/utils/proxySet.ts +++ b/src/vanilla/utils/proxySet.ts @@ -1,6 +1,7 @@ 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 InternalProxySet = Set & { @@ -35,8 +36,9 @@ export function proxySet(initialValues?: Iterable | null) { if (typeof initialValues[Symbol.iterator] !== 'function') { throw new TypeError('not iterable') } - for (const value of initialValues) { - if (!indexMap.has(value)) { + for (const v of initialValues) { + if (!indexMap.has(v)) { + const value = maybeProxify(v) indexMap.set(value, initialIndex) initialData[initialIndex++] = value } @@ -52,8 +54,9 @@ export function proxySet(initialValues?: Iterable | null) { } return indexMap.size }, - has(value: T) { + has(v: T) { const map = getMapForThis(this) + const value = maybeProxify(v) const exists = map.has(value) if (!exists) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions @@ -65,10 +68,11 @@ export function proxySet(initialValues?: Iterable | null) { if (!isProxy(this)) { throw new Error('Cannot perform mutations on a snapshot') } - if (!indexMap.has(value)) { + const v = maybeProxify(value) + if (!indexMap.has(v)) { let nextIndex = this.index - indexMap.set(value, nextIndex) - this.data[nextIndex++] = value + indexMap.set(v, nextIndex) + this.data[nextIndex++] = v this.index = nextIndex } return this @@ -77,12 +81,13 @@ export function proxySet(initialValues?: Iterable | null) { if (!isProxy(this)) { throw new Error('Cannot perform mutations on a snapshot') } - const index = indexMap.get(value) + const v = maybeProxify(value) + const index = indexMap.get(v) if (index === undefined) { return false } delete this.data[index] - indexMap.delete(value) + indexMap.delete(v) return true }, clear() {