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

[v2] breaking: do not copy initial objects #815

Merged
merged 6 commits into from
Nov 13, 2023
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
67 changes: 31 additions & 36 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ const buildProxyFunction = (

versionHolder = [1, 1] as [number, number],

proxyFunction = <T extends object>(initialObject: T): T => {
if (!isObject(initialObject)) {
proxyFunction = <T extends object>(baseObject: T): T => {
if (!isObject(baseObject)) {
throw new Error('object required')
}
const found = proxyCache.get(initialObject) as T | undefined
const found = proxyCache.get(baseObject) as T | undefined
if (found) {
return found
}
Expand Down Expand Up @@ -167,18 +167,23 @@ const buildProxyFunction = (
string | symbol,
readonly [ProxyState, RemoveListener?]
>()
const addPropListener = (
prop: string | symbol,
propProxyState: ProxyState
) => {
if (import.meta.env?.MODE !== 'production' && propProxyStates.has(prop)) {
throw new Error('prop listener already exists')
}
if (listeners.size) {
const remove = propProxyState[3](createPropListener(prop))
propProxyStates.set(prop, [propProxyState, remove])
} else {
propProxyStates.set(prop, [propProxyState])
const addPropListener = (prop: string | symbol, propValue: unknown) => {
const propProxyState =
!refSet.has(propValue as object) &&
proxyStateMap.get(propValue as object)
if (propProxyState) {
if (
import.meta.env?.MODE !== 'production' &&
propProxyStates.has(prop)
) {
throw new Error('prop listener already exists')
}
if (listeners.size) {
const remove = propProxyState[3](createPropListener(prop))
propProxyStates.set(prop, [propProxyState, remove])
} else {
propProxyStates.set(prop, [propProxyState])
}
}
}
const removePropListener = (prop: string | symbol) => {
Expand Down Expand Up @@ -212,9 +217,7 @@ const buildProxyFunction = (
}
return removeListener
}
const baseObject = Array.isArray(initialObject)
? []
: Object.create(Object.getPrototypeOf(initialObject))
let initializing = true
const handler: ProxyHandler<T> = {
deleteProperty(target: T, prop: string | symbol) {
const prevValue = Reflect.get(target, prop)
Expand All @@ -226,7 +229,7 @@ const buildProxyFunction = (
return deleted
},
set(target: T, prop: string | symbol, value: any, receiver: object) {
const hasPrevValue = Reflect.has(target, prop)
const hasPrevValue = !initializing && Reflect.has(target, prop)
const prevValue = Reflect.get(target, prop, receiver)
if (
hasPrevValue &&
Expand Down Expand Up @@ -257,40 +260,32 @@ const buildProxyFunction = (
if (!proxyStateMap.has(value) && canProxy(value)) {
nextValue = proxyFunction(value)
}
const childProxyState =
!refSet.has(nextValue) && proxyStateMap.get(nextValue)
if (childProxyState) {
addPropListener(prop, childProxyState)
}
addPropListener(prop, nextValue)
}
Reflect.set(target, prop, nextValue, receiver)
notifyUpdate(['set', [prop], value, prevValue])
return true
},
}
const proxyObject = newProxy(baseObject, handler)
proxyCache.set(initialObject, proxyObject)
proxyCache.set(baseObject, proxyObject)
const proxyState: ProxyState = [
baseObject,
ensureVersion,
createSnapshot,
addListener,
]
proxyStateMap.set(proxyObject, proxyState)
Reflect.ownKeys(initialObject).forEach((key) => {
Reflect.ownKeys(baseObject).forEach((key) => {
const desc = Object.getOwnPropertyDescriptor(
initialObject,
baseObject,
key
) as PropertyDescriptor
if ('value' in desc) {
proxyObject[key as keyof T] = initialObject[key as keyof T]
// We need to delete desc.value because we already set it,
// and delete desc.writable because we want to write it again.
delete desc.value
delete desc.writable
if ('value' in desc && desc.writable) {
proxyObject[key as keyof T] = baseObject[key as keyof T]
}
Object.defineProperty(baseObject, key, desc)
})
initializing = false
return proxyObject
}
) =>
Expand All @@ -312,8 +307,8 @@ const buildProxyFunction = (

const [defaultProxyFunction] = buildProxyFunction()

export function proxy<T extends object>(initialObject: T = {} as T): T {
return defaultProxyFunction(initialObject)
export function proxy<T extends object>(baseObject: T = {} as T): T {
return defaultProxyFunction(baseObject)
}

export function getVersion(proxyObject: unknown): number | undefined {
Expand Down
1 change: 1 addition & 0 deletions src/vanilla/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { devtools } from './utils/devtools.ts'
export { derive, underive, unstable_deriveSubscriptions } from 'derive-valtio'
export { addComputed_DEPRECATED as addComputed } from './utils/addComputed.ts'
export { proxyWithComputed_DEPRECATED as proxyWithComputed } from './utils/proxyWithComputed.ts'
export { deepClone } from './utils/deepClone.ts'
export { proxyWithHistory } from './utils/proxyWithHistory.ts'
export { proxySet } from './utils/proxySet.ts'
export { proxyMap } from './utils/proxyMap.ts'
25 changes: 25 additions & 0 deletions src/vanilla/utils/deepClone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { unstable_buildProxyFunction as buildProxyFunction } from '../../vanilla.ts'

const isObject = (x: unknown): x is object =>
typeof x === 'object' && x !== null

let defaultRefSet: WeakSet<object> | undefined
const getDefaultRefSet = (): WeakSet<object> => {
if (!defaultRefSet) {
defaultRefSet = buildProxyFunction()[2]
}
return defaultRefSet
}

export const deepClone = <T>(obj: T, getRefSet = getDefaultRefSet): T => {
if (!isObject(obj) || getRefSet().has(obj)) {
return obj
}
const baseObject: T = Array.isArray(obj)
? []
: Object.create(Object.getPrototypeOf(obj))
Reflect.ownKeys(obj).forEach((key) => {
baseObject[key as keyof T] = deepClone(obj[key as keyof T], getRefSet)
})
return baseObject
}
30 changes: 2 additions & 28 deletions src/vanilla/utils/proxyWithHistory.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
import {
unstable_buildProxyFunction as buildProxyFunction,
proxy,
ref,
snapshot,
subscribe,
} from '../../vanilla.ts'
import { proxy, ref, snapshot, subscribe } from '../../vanilla.ts'
import type { INTERNAL_Snapshot as Snapshot } from '../../vanilla.ts'
import { deepClone } from './deepClone.ts'

type SnapshotOrUndefined<T> = Snapshot<T> | undefined
type Snapshots<T> = Snapshot<T>[]

const isObject = (x: unknown): x is object =>
typeof x === 'object' && x !== null

let refSet: WeakSet<object> | undefined

const deepClone = <T>(obj: T): T => {
if (!refSet) {
refSet = buildProxyFunction()[2]
}
if (!isObject(obj) || refSet.has(obj)) {
return obj
}
const baseObject: T = Array.isArray(obj)
? []
: Object.create(Object.getPrototypeOf(obj))
Reflect.ownKeys(obj).forEach((key) => {
baseObject[key as keyof T] = deepClone(obj[key as keyof T])
})
return baseObject
}

/**
* proxyWithHistory
*
Expand Down