Skip to content

Commit

Permalink
changed to isProxy with this context
Browse files Browse the repository at this point in the history
  • Loading branch information
overthemike committed Oct 9, 2024
1 parent 285f1a9 commit 4dbd796
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/vanilla/utils/proxyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { getVersion, proxy } from '../../vanilla.ts'

const maybeProxify = (x: any) => proxy({ x }).x

const isProxy = (x: any) => getVersion(x) !== undefined

type InternalProxyObject<K, V> = Map<K, V> & {
data: Array<[K, V | undefined]>
size: number
Expand Down Expand Up @@ -44,14 +46,14 @@ export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
},
has(k: K) {
const key = maybeProxify(k)
if (!indexMap.has(key) && shouldAbortMutation()) {
if (!indexMap.has(key) && !isProxy(this)) {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.data.length
}
return indexMap.has(key)
},
set(key: K, value: V) {
if (shouldAbortMutation()) {
if (!isProxy(this)) {
if (import.meta.env?.MODE !== 'production') {
throw new Error('Cannot perform mutations on a snapshot')
} else {
Expand All @@ -76,7 +78,7 @@ export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
return this
},
delete(key: K) {
if (shouldAbortMutation()) {
if (!isProxy(this)) {
if (import.meta.env?.MODE !== 'production') {
throw new Error('Cannot perform mutations on a snapshot')
} else {
Expand All @@ -93,7 +95,7 @@ export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
return false
},
clear() {
if (shouldAbortMutation()) {
if (!isProxy(this)) {
if (import.meta.env?.MODE !== 'production') {
throw new Error('Cannot perform mutations on a snapshot')
} else {
Expand Down Expand Up @@ -137,10 +139,6 @@ export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {

const proxiedObject = proxy(vObject)

function shouldAbortMutation() {
return getVersion(proxiedObject) !== undefined
}

Object.defineProperties(proxiedObject, {
size: { enumerable: false },
data: { enumerable: false },
Expand Down

0 comments on commit 4dbd796

Please sign in to comment.