import { LISTENERS, REACTIVE, PROXY_COUNT, SNAPSHOT, canProxy, createObjectFromPrototype, hasOwn, isObject, } from '../utils/index.js' import { snapshot } from './snapshot.js' let globalVersion = 1 const snapshotCache = new WeakMap() export type StoreListener = (props: PropertyKey[], version?: number) => void /** * @deprecated use `StoreListener` instead */ export type Listener = StoreListener export function proxy( initState: State, parentProps: PropertyKey[] = [], options?: { addParentProxyCount: (value?: number) => void } ): State { const { addParentProxyCount = () => {} } = options || {} let version = globalVersion // for all changes including nested objects, stored in `proxyState[LISTENERS]` const listeners = new Set() // only for changes in the current object, stored in function scope const propListenerMap = new Map() const notifyUpdate = (props: PropertyKey[], nextVersion = ++globalVersion) => { if (version !== nextVersion) { version = nextVersion for (const callback of listeners) { callback(props, version) } } } const getPropListener = (prop: PropertyKey) => { let listener = propListenerMap.get(prop) if (!listener) { listener = (props: PropertyKey[]) => void notifyUpdate(props) propListenerMap.set(prop, listener) } return listener } const popPropListener = (prop: PropertyKey) => { const listener = propListenerMap.get(prop) propListenerMap.delete(prop) return listener } const createSnapshot = (target: State, receiver: any) => { const cache = snapshotCache.get(receiver) if (cache?.[0] === version) return cache[1] const nextSnapshot = createObjectFromPrototype(target) snapshotCache.set(receiver, [version, nextSnapshot]) for (const key of Reflect.ownKeys(target)) { if (key === REACTIVE || key === PROXY_COUNT) continue const value: any = Reflect.get(target, key, receiver) if (value?.[REACTIVE]) { nextSnapshot[key as keyof State] = snapshot(value) } else { nextSnapshot[key as keyof State] = value } } Object.preventExtensions(nextSnapshot) return nextSnapshot } const baseObject = createObjectFromPrototype(initState) const addCurrentProxyCount = (value: number = 1) => { addParentProxyCount(value) const count = (Reflect.get(proxyState, PROXY_COUNT) || 1) as number Reflect.defineProperty(proxyState, PROXY_COUNT, { value: count + value, writable: true }) } const proxyState = new Proxy(baseObject, { get(target, prop, receiver) { if (prop === LISTENERS) { return listeners } if (prop === SNAPSHOT) { return createSnapshot(target, receiver) } return Reflect.get(target, prop, receiver) }, set(target, prop, value, receiver) { const props = [...parentProps, prop] const preValue = Reflect.get(target, prop, receiver) as any // when set a new object to `prop`, we need to remove the old listeners // in outdated object in `prop`, which is not in proxy state anymore // meanwhile, we need to pop the old listener from `propListenerMap` const childListeners = preValue?.[LISTENERS] if (childListeners) { childListeners.delete(popPropListener(prop)) } if (!isObject(value) && Object.is(preValue, value) && hasOwn.call(target, prop)) { // `return true` means the operation is successful, // but we don't need to notify the update here, // because the value is basic value, and it's the same as before return true } let nextValue = value if (nextValue?.[LISTENERS]) { // if the value is a proxy object, we need to merge the listeners nextValue[LISTENERS].add(getPropListener(prop)) } else if (canProxy(value)) { if (preValue?.[REACTIVE]) { const childrenCount = preValue?.[PROXY_COUNT] || 1 addCurrentProxyCount(-childrenCount) } addCurrentProxyCount(1) nextValue = proxy(value, props, { addParentProxyCount: addCurrentProxyCount, }) nextValue[LISTENERS].add(getPropListener(prop)) } const success = Reflect.set(target, prop, nextValue, receiver) if (success) { notifyUpdate(props) } return success }, deleteProperty(target: State, prop: string | symbol) { const props = [...parentProps, prop] const preValue = Reflect.get(target, prop) as any const childListeners = preValue?.[LISTENERS] if (childListeners) { childListeners.delete(popPropListener(prop)) } if (preValue?.[REACTIVE]) { const childrenCount = preValue?.[PROXY_COUNT] || 1 addCurrentProxyCount(-childrenCount) } const success = Reflect.deleteProperty(target, prop) if (success) { notifyUpdate(props) } return success }, }) // mark the proxy state as reactive state Reflect.defineProperty(proxyState, REACTIVE, { value: true }) for (const key of Reflect.ownKeys(initState)) { proxyState[key as keyof State] = initState[key as keyof State] } // Initialize the reactive count property if it doesn't exist if (!Reflect.has(proxyState, PROXY_COUNT)) { Reflect.defineProperty(proxyState, PROXY_COUNT, { value: 1, writable: true }) } return proxyState }