Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Not For Merge] Keyed collections 2 #969

Merged
merged 6 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 48 additions & 20 deletions src/vanilla/utils/proxyMap-rawMap1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {
unstable_getInternalStates,
} from '../../vanilla.ts'

const { proxyStateMap } = unstable_getInternalStates()
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> & {
epoch: number
_registerSnap: boolean
toJSON: () => Map<K, V>
}

Expand All @@ -19,21 +20,45 @@ export function proxyMap<K, V>() {
const unsubKeyMap = new WeakMap<object, () => void>()
const unsubValMap = new WeakMap<object, () => void>()

const snapMapCache = new WeakMap<object, Map<K, V>>()
const registerSnapMap = () => {
const cache = snapCache.get(vObject)
const latestSnap = cache?.[1]
if (latestSnap && !snapMapCache.has(latestSnap)) {
const snapMap = new Map<K, V>()
for (const [k, v] of rawMap) {
snapMap.set(
isProxy(k) ? (snapshot(k as object) as K) : k,
isProxy(v) ? (snapshot(v as object) as V) : v,
)
}
snapMapCache.set(latestSnap, snapMap)
return true
}
return false
}
const getSnapMap = (x: any) => snapMapCache.get(x)

const vObject: InternalProxyObject<K, V> = {
epoch: 0,
get _registerSnap() {
return registerSnapMap()
},
get size() {
return rawMap.size
const map = getSnapMap(this) || rawMap
return map.size
},
get(key: K) {
const map = getSnapMap(this) || rawMap
const k = maybeProxify(key)
if (!rawMap.has(k)) {
if (!map.has(k)) {
if (!isProxy(this)) {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.epoch
}
return undefined
}
const val = rawMap.get(k) as V
const val = map.get(k) as V
if (isProxy(this)) {
return val
}
Expand All @@ -43,8 +68,9 @@ export function proxyMap<K, V>() {
return val
},
has(key: K) {
const map = getSnapMap(this) || rawMap
const k = maybeProxify(key)
const exists = rawMap.has(k)
const exists = map.has(k)
if (!exists && !isProxy(this)) {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.epoch
Expand Down Expand Up @@ -89,31 +115,33 @@ export function proxyMap<K, V>() {
rawMap.set(k, v)
return this
},
delete(key: K) {
delete(_key: K) {
throw new Error('Not implemented')
},
clear() {
throw new Error('Not implemented')
},
forEach(cb: (value: V, key: K, map: Map<K, V>) => void) {
//indexMap.forEach((index) => {
// cb(this.data[index + 1] as V, this.data[index] as K, this)
//})
const map = getSnapMap(this) || rawMap
map.forEach(cb)
},
*entries(): MapIterator<[K, V]> {
//for (const index of indexMap.values()) {
// yield [this.data[index], this.data[index + 1]] as [K, V]
//}
const map = getSnapMap(this) || rawMap
for (const [k, v] of map) {
yield [k, v]
}
},
*keys(): IterableIterator<K> {
//for (const key of indexMap.keys()) {
// yield key
//}
const map = getSnapMap(this) || rawMap
for (const k of map.keys()) {
yield k
}
},
*values(): IterableIterator<V> {
//for (const index of indexMap.values()) {
// yield this.data[index + 1] as V
//}
const map = getSnapMap(this) || rawMap
for (const v of map.values()) {
yield v
}
},
[Symbol.iterator]() {
return this.entries()
Expand All @@ -122,8 +150,8 @@ export function proxyMap<K, V>() {
return 'Map'
},
toJSON(): Map<K, V> {
throw new Error('Not implemented')
// return new Map([...indexMap].map(([k, i]) => [k, this.data[i + 1] as V]))
const map = getSnapMap(this) || rawMap
return map
},
}

Expand Down
6 changes: 3 additions & 3 deletions src/vanilla/utils/proxyMap-tree1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ const searchFromTreeNode = (node: TreeNode, key: number, key2 = key) => {
}

const deleteFromTreeNode = (
node: TreeNode,
_node: TreeNode,
key: number,
key2 = key,
_key2 = key,
): boolean => {
throw new Error('Not implemented')
}
Expand Down Expand Up @@ -163,7 +163,7 @@ export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
}
this.root = createNewTreeNode()
},
forEach(cb: (value: V, key: K, map: Map<K, V>) => void) {
forEach(_cb: (value: V, key: K, map: Map<K, V>) => void) {
// indexMap.forEach((index) => {
// cb(this.data[index + 1] as V, this.data[index] as K, this)
// })
Expand Down
Loading