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 #964

Merged
merged 21 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
5 changes: 5 additions & 0 deletions src/vanilla/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export { devtools } from './utils/devtools.ts'
export { deepClone } from './utils/deepClone.ts'
export { proxySet } from './utils/proxySet.ts'
export { proxyMap } from './utils/proxyMap-indexMap-keyvals.ts'

export { proxyMap as newProxyMap } from './utils/proxyMap-indexMap-filled.ts'
export { proxyMap as newProxyMapKeyVals } from './utils/proxyMap-indexMap-keyvals.ts'
export { proxyMap as newProxyMapRawMap1 } from './utils/proxyMap-rawMap1.ts'
export { proxyMap as newProxyMapTree1 } from './utils/proxyMap-tree1.ts'
146 changes: 146 additions & 0 deletions src/vanilla/utils/proxyMap-indexMap-key-val.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { proxy, unstable_getInternalStates } from '../../vanilla.ts'
const { proxyStateMap } = 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> & {
dataKeys: Array<K>
dataValues: Array<V>
index: number
toJSON: () => Map<K, V>
}

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

if (entries !== null && typeof entries !== 'undefined') {
if (typeof entries[Symbol.iterator] !== 'function') {
throw new TypeError(
'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)
indexMap.set(key, initialIndex)
initialDataKeys[initialIndex] = key
initialDataValues[initialIndex] = value
initialIndex++
}
}

const vObject: InternalProxyObject<K, V> = {
dataKeys: initialDataKeys,
dataValues: initialDataValues,
index: initialIndex,
get size() {
return indexMap.size
},
get(key: K) {
const k = maybeProxify(key)
const index = indexMap.get(k)
if (index === undefined && !isProxy(this)) {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.index
}
if (index !== undefined) {
return this.dataValues[index]
}
return undefined
},
has(key: K) {
const k = maybeProxify(key)
const exists = indexMap.has(k)
if (!exists && !isProxy(this)) {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.index
}
return exists
},
set(key: K, value: V) {
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)
if (index === undefined) {
indexMap.set(k, this.index)
this.dataKeys[this.index] = k
this.dataValues[this.index] = v
this.index++
} else {
this.dataValues[index] = v
}
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)
if (index !== undefined) {
delete this.dataKeys[index]
delete this.dataValues[index]
indexMap.delete(k)
return true
}
return false
},
clear() {
if (!isProxy(this)) {
throw new Error('Cannot perform mutations on a snapshot')
}
indexMap.clear()
this.dataKeys.splice(0)
this.dataValues.splice(0)
},
forEach(cb: (value: V, key: K, map: Map<K, V>) => void) {
indexMap.forEach((index) => {
cb(this.dataValues[index]!, this.dataKeys[index]!, this)
})
},
*entries(): MapIterator<[K, V]> {
for (const index of indexMap.values()) {
yield [this.dataKeys[index], this.dataValues[index]] as [K, V]
}
},
*keys(): IterableIterator<K> {
for (const key of indexMap.keys()) {
yield key
}
},
*values(): IterableIterator<V> {
for (const index of indexMap.values()) {
yield this.dataValues[index]!
}
},
[Symbol.iterator]() {
return this.entries()
},
get [Symbol.toStringTag]() {
return 'Map'
},
toJSON(): Map<K, V> {
return new Map([...indexMap].map(([k, v]) => [k, this.dataValues[v]!]))
},
}

const proxiedObject = proxy(vObject)

Object.defineProperties(proxiedObject, {
size: { enumerable: false },
data: { enumerable: false },
toJSON: { enumerable: false },
})

Object.seal(proxiedObject)

return proxiedObject as unknown as Map<K, V> & {
$$valtioSnapshot: Omit<Map<K, V>, 'set' | 'delete' | 'clear'>
}
}
143 changes: 143 additions & 0 deletions src/vanilla/utils/proxyMap-indexMap-keyval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { proxy, unstable_getInternalStates } from '../../vanilla.ts'
const { proxyStateMap } = 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>
index: number
toJSON: () => Map<K, V>
}

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

if (entries !== null && typeof entries !== 'undefined') {
if (typeof entries[Symbol.iterator] !== 'function') {
throw new TypeError(
'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)
indexMap.set(key, initialIndex)
initialData[initialIndex++] = key
initialData[initialIndex++] = value
}
}

const vObject: InternalProxyObject<K, V> = {
data: initialData,
index: initialIndex,
get size() {
return indexMap.size
},
get(key: K) {
const k = maybeProxify(key)
const index = indexMap.get(k)
if (index !== undefined) {
return this.data[index + 1] as V
}
if (!isProxy(this)) {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.index
}
return undefined
},
has(key: K) {
const k = maybeProxify(key)
const exists = indexMap.has(k)
if (!exists && !isProxy(this)) {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.index
}
return exists
},
set(key: K, value: V) {
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)
if (index === undefined) {
let nextIndex = this.index
indexMap.set(k, nextIndex)
this.data[nextIndex++] = k
this.data[nextIndex++] = v
this.index = nextIndex
} else {
this.data[index + 1] = v
}
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)
if (index !== undefined) {
delete this.data[index]
delete this.data[index + 1]
indexMap.delete(k)
return true
}
return false
},
clear() {
if (!isProxy(this)) {
throw new Error('Cannot perform mutations on a snapshot')
}
indexMap.clear()
this.index = 0
this.data.splice(0)
},
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)
})
},
*entries(): MapIterator<[K, V]> {
for (const index of indexMap.values()) {
yield [this.data[index], this.data[index + 1]] as [K, V]
}
},
*keys(): IterableIterator<K> {
for (const key of indexMap.keys()) {
yield key
}
},
*values(): IterableIterator<V> {
for (const index of indexMap.values()) {
yield this.data[index + 1] as V
}
},
[Symbol.iterator]() {
return this.entries()
},
get [Symbol.toStringTag]() {
return 'Map'
},
toJSON(): Map<K, V> {
return new Map([...indexMap].map(([k, i]) => [k, this.data[i + 1] as V]))
},
}

const proxiedObject = proxy(vObject)

Object.defineProperties(proxiedObject, {
size: { enumerable: false },
data: { enumerable: false },
toJSON: { enumerable: false },
})

Object.seal(proxiedObject)

return proxiedObject as unknown as Map<K, V> & {
$$valtioSnapshot: Omit<Map<K, V>, 'set' | 'delete' | 'clear'>
}
}
Loading