Skip to content

Commit

Permalink
added back in maybeProxify to proxySet
Browse files Browse the repository at this point in the history
  • Loading branch information
overthemike committed Oct 15, 2024
1 parent b001c7e commit 7e0b6e8
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/vanilla/utils/proxySet.ts
Original file line number Diff line number Diff line change
@@ -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<T> = Set<T> & {
Expand Down Expand Up @@ -35,8 +36,9 @@ export function proxySet<T>(initialValues?: Iterable<T> | 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
}
Expand All @@ -52,8 +54,9 @@ export function proxySet<T>(initialValues?: Iterable<T> | 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
Expand All @@ -65,10 +68,11 @@ export function proxySet<T>(initialValues?: Iterable<T> | 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
Expand All @@ -77,12 +81,13 @@ export function proxySet<T>(initialValues?: Iterable<T> | 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() {
Expand Down

0 comments on commit 7e0b6e8

Please sign in to comment.