Skip to content

Commit

Permalink
merged in changes
Browse files Browse the repository at this point in the history
  • Loading branch information
overthemike committed Oct 15, 2024
2 parents b0e4b3e + 016f590 commit dbcc0ac
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions src/vanilla/utils/proxyMap.ts
Original file line number Diff line number Diff line change
@@ -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<K, V> = Map<K, V> & {
data: Array<K | V>
data: Array<V>
index: number
toJSON: () => Map<K, V>
}

export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
const initialData: Array<K | V> = []
const initialData: Array<V> = []
let initialIndex = 0
const indexMap = new Map<K, number>()

Expand All @@ -32,11 +31,8 @@ export function proxyMap<K, V>(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
}
}
Expand All @@ -53,19 +49,17 @@ export function proxyMap<K, V>(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
Expand All @@ -76,32 +70,25 @@ export function proxyMap<K, V>(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
},
delete(key: K) {
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() {
Expand All @@ -114,14 +101,14 @@ export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
},
forEach(cb: (value: V, key: K, map: Map<K, V>) => 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<K> {
Expand All @@ -133,7 +120,7 @@ export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
*values(): IterableIterator<V> {
const map = getMapForThis(this)
for (const index of map.values()) {
yield this.data[index + 1] as V
yield this.data[index]!
}
},
[Symbol.iterator]() {
Expand Down

0 comments on commit dbcc0ac

Please sign in to comment.