Skip to content

Commit

Permalink
added isSnapshot function for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
overthemike committed Oct 8, 2024
1 parent 6fcb3cb commit 74f0ad8
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 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 isSnapshot = (context: any) => getVersion(context) === 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)) {
if (!indexMap.has(key) && isSnapshot(this)) {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.data.length
}
return indexMap.has(key)
},
set(key: K, value: V) {
if (getVersion(this) === undefined) {
if (isSnapshot(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 (getVersion(this) === undefined) {
if (isSnapshot(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 (getVersion(this) === undefined) {
if (isSnapshot(this)) {
if (import.meta.env?.MODE !== 'production') {
throw new Error('Cannot perform mutations on a snapshot')
} else {
Expand Down

0 comments on commit 74f0ad8

Please sign in to comment.