diff --git a/libs/horizon/src/horizonx/proxy/ProxyHandler.ts b/libs/horizon/src/horizonx/proxy/ProxyHandler.ts index b2ff5cdd..d37cca1a 100644 --- a/libs/horizon/src/horizonx/proxy/ProxyHandler.ts +++ b/libs/horizon/src/horizonx/proxy/ProxyHandler.ts @@ -31,9 +31,7 @@ export function getObserver(rawObj: any): Observer { return rawObj[OBSERVER_KEY]; } -export function createProxy(rawObj: any, isHookObserver: boolean, listener: { current: (...args) => any }): any { - isHookObserver = isHookObserver || true; - +export function createProxy(rawObj: any, listener: { current: (...args) => any }, isHookObserver = true): any { // 不是对象(是原始数据类型)不用代理 if (!(rawObj && isObject(rawObj))) { return rawObj; diff --git a/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts b/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts index 630abd53..ea94e4a0 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts @@ -35,7 +35,7 @@ function set(rawObj: any[], key: string, value: any, receiver: any) { if (!isSame(newValue, oldValue)) { // 值不一样,触发监听器 - if (observer.watchers[key]) { + if (observer.watchers?.[key]) { observer.watchers[key].forEach(cb => { cb(key, oldValue, newValue, mutation); }); @@ -97,7 +97,7 @@ export function createArrayProxy(rawObj: any[], listener: { current: (...args) = // 对于value也需要进一步代理 const valProxy = singleLevel ? value - : createProxy(value, hookObserverMap.get(rawObj), { + : createProxy(value, { current: change => { if (!change.parents) change.parents = []; change.parents.push(rawObj); @@ -108,7 +108,9 @@ export function createArrayProxy(rawObj: any[], listener: { current: (...args) = listener.current(mutation); listeners.forEach(lst => lst(mutation)); }, - }); + }, + hookObserverMap.get(rawObj) + ); return valProxy; } diff --git a/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts index 6fd0b03d..03accb28 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts @@ -38,18 +38,20 @@ export function createMapProxy( const value = rawObj.get(keyProxy); // 对于value也需要进一步代理 - const valProxy = createProxy(value, hookObserverMap.get(rawObj), { - current: change => { - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, [key]: change.mutation.from }, - { ...rawObj, [key]: change.mutation.to } - ); - listener.current({ ...change, mutation }); - listeners.forEach(lst => lst({ ...change, mutation })); + const valProxy = createProxy(value, { + current: change => { + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, [key]: change.mutation.from }, + { ...rawObj, [key]: change.mutation.to } + ); + listener.current({ ...change, mutation }); + listeners.forEach(lst => lst({ ...change, mutation })); + }, }, - }); + hookObserverMap.get(rawObj) + ); return valProxy; } @@ -84,19 +86,21 @@ export function createMapProxy( oldData = [...Array.from(rawObj.entries())]; } else { // NEW VALUE - const keyProxy = createProxy(key, hookObserverMap.get(rawObj), { - current: change => { - // KEY CHANGE - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, ['_keyChange']: change.mutation.from }, - { ...rawObj, ['_keyChange']: change.mutation.to } - ); - listener.current({ ...change, mutation }); - listeners.forEach(lst => lst({ ...change, mutation })); + const keyProxy = createProxy(key, { + current: change => { + // KEY CHANGE + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, ['_keyChange']: change.mutation.from }, + { ...rawObj, ['_keyChange']: change.mutation.to } + ); + listener.current({ ...change, mutation }); + listeners.forEach(lst => lst({ ...change, mutation })); + }, }, - }); + hookObserverMap.get(rawObj) + ); proxies.set(key, keyProxy); rawObj.set(keyProxy, value); @@ -180,32 +184,36 @@ export function createMapProxy( const observer = getObserver(rawObj); observer.useProp(COLLECTION_CHANGE); rawObj.forEach((value, key) => { - const keyProxy = createProxy(value, hookObserverMap.get(rawObj), { - current: change => { - //KEY ATTRIBUTES CHANGED - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, ['_keyChange']: change.mutation.from }, - { ...rawObj, ['_keyChange']: change.mutation.to } - ); - listener.current({ ...change, mutation }); - listeners.forEach(lst => lst({ ...change, mutation })); + const keyProxy = createProxy(value, { + current: change => { + //KEY ATTRIBUTES CHANGED + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, ['_keyChange']: change.mutation.from }, + { ...rawObj, ['_keyChange']: change.mutation.to } + ); + listener.current({ ...change, mutation }); + listeners.forEach(lst => lst({ ...change, mutation })); + }, }, - }); - const valProxy = createProxy(key, hookObserverMap.get(rawObj), { - current: change => { - // VALUE ATTRIBUTE CHANGED - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, key: change.mutation.from }, - { ...rawObj, key: change.mutation.to } - ); - listener.current({ ...change, mutation }); - listeners.forEach(lst => lst({ ...change, mutation })); + hookObserverMap.get(rawObj) + ); + const valProxy = createProxy(key, { + current: change => { + // VALUE ATTRIBUTE CHANGED + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, key: change.mutation.from }, + { ...rawObj, key: change.mutation.to } + ); + listener.current({ ...change, mutation }); + listeners.forEach(lst => lst({ ...change, mutation })); + }, }, - }); + hookObserverMap.get(rawObj) + ); // 最后一个参数要返回代理对象 return callback(keyProxy, valProxy, rawObj); }); @@ -221,18 +229,20 @@ export function createMapProxy( const { value, done } = rawIt.next(); if (done) { return { - value: createProxy(value, hookObserver, { - current: change => { - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, [value]: change.mutation.from }, - { ...rawObj, [value]: change.mutation.to } - ); - listener.current({ ...change, mutation }); - listeners.forEach(lst => lst({ ...change, mutation })); + value: createProxy(value, { + current: change => { + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, [value]: change.mutation.from }, + { ...rawObj, [value]: change.mutation.to } + ); + listener.current({ ...change, mutation }); + listeners.forEach(lst => lst({ ...change, mutation })); + }, }, - }), + hookObserver + ), done, }; } @@ -242,45 +252,51 @@ export function createMapProxy( if (type === 'entries') { //ENTRY CHANGED newVal = [ - createProxy(value[0], hookObserver, { - current: change => { - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, ['itemChange']: { key: change.mutation.from, value: value[1] } }, - { ...rawObj, ['itemChange']: { key: change.mutation.to, value: value[1] } } - ); - listener.current({ ...change, mutation }); - listeners.forEach(lst => lst({ ...change, mutation })); + createProxy(value[0], { + current: change => { + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, ['itemChange']: { key: change.mutation.from, value: value[1] } }, + { ...rawObj, ['itemChange']: { key: change.mutation.to, value: value[1] } } + ); + listener.current({ ...change, mutation }); + listeners.forEach(lst => lst({ ...change, mutation })); + }, }, - }), - createProxy(value[1], hookObserver, { - current: change => { - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, item: { key: value[0], value: change.mutation.from } }, - { ...rawObj, item: { key: value[0], value: change.mutation.to } } - ); - listener.current({ ...change, mutation }); - listeners.forEach(lst => lst({ ...change, mutation })); + hookObserver + ), + createProxy(value[1], { + current: change => { + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, item: { key: value[0], value: change.mutation.from } }, + { ...rawObj, item: { key: value[0], value: change.mutation.to } } + ); + listener.current({ ...change, mutation }); + listeners.forEach(lst => lst({ ...change, mutation })); + }, }, - }), + hookObserver + ), ]; } else { // SINGLE VALUE CHANGED - newVal = createProxy(value, hookObserver, { - current: change => { - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, [type === 'keys' ? 'key' : 'value']: change.mutation.from }, - { ...rawObj, [type === 'keys' ? 'key' : 'value']: change.mutation.to } - ); - listener.current({ ...change, mutation }); - listeners.forEach(lst => lst({ ...change, mutation })); + newVal = createProxy(value, { + current: change => { + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, [type === 'keys' ? 'key' : 'value']: change.mutation.from }, + { ...rawObj, [type === 'keys' ? 'key' : 'value']: change.mutation.to } + ); + listener.current({ ...change, mutation }); + listeners.forEach(lst => lst({ ...change, mutation })); + }, }, - }); + hookObserver + ); } return { value: newVal, done }; diff --git a/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts b/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts index 4f7448ab..1a216477 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts @@ -29,7 +29,7 @@ function set(rawObj: object, key: string, value: any, receiver: any): boolean { const mutation = isPanelActive() ? resolveMutation(oldObject, rawObj) : resolveMutation(null, rawObj); if (!isSame(newValue, oldValue)) { - if (observer.watchers[key]) { + if (observer.watchers?.[key]) { observer.watchers[key].forEach(cb => { cb(key, oldValue, newValue, mutation); }); @@ -87,7 +87,7 @@ export function createObjectProxy( // 对于value也需要进一步代理 const valProxy = singleLevel ? value - : createProxy(value, hookObserverMap.get(rawObj), { + : createProxy(value, { current: change => { if (!change.parents) change.parents = []; change.parents.push(rawObj); @@ -98,7 +98,9 @@ export function createObjectProxy( listener.current({ ...change, mutation }); listeners.forEach(lst => lst({ ...change, mutation })); }, - }); + }, + hookObserverMap.get(rawObj) + ); return valProxy; } diff --git a/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts index 3eeeebd0..e5b19e4f 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts @@ -29,26 +29,28 @@ export function createSetProxy( // Set的add方法 function add(rawObj: { add: (any) => void; has: (any) => boolean; values: () => any[] }, value: any): Object { if (!rawObj.has(proxies.get(value))) { - const proxy = createProxy(value, hookObserverMap.get(rawObj), { - current: change => { - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, valueChange: change.mutation.from }, - { ...rawObj, valueChange: change.mutation.to } - ); - listener.current({ - ...change, - mutation, - }); - listeners.forEach(lst => - lst({ + const proxy = createProxy(value, { + current: change => { + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, valueChange: change.mutation.from }, + { ...rawObj, valueChange: change.mutation.to } + ); + listener.current({ ...change, mutation, - }) - ); + }); + listeners.forEach(lst => + lst({ + ...change, + mutation, + }) + ); + }, }, - }); + hookObserverMap.get(rawObj) + ); const oldValues = Array.from(rawObj.values()); proxies.set(value, proxy); @@ -210,13 +212,13 @@ export function createSetProxy( }; const { value, done } = rawIt.next(); if (done) { - return { value: createProxy(value, hookObserver, currentListener), done }; + return { value: createProxy(value, currentListener, hookObserver), done }; } observer.useProp(COLLECTION_CHANGE); let newVal; - newVal = createProxy(value, hookObserver, currentListener); + newVal = createProxy(value, currentListener, hookObserver); return { value: newVal, done }; }, @@ -274,8 +276,8 @@ export function createSetProxy( ); }, }; - const valProxy = createProxy(value, hookObserverMap.get(rawObj), currentListener); - const keyProxy = createProxy(key, hookObserverMap.get(rawObj), currentListener); + const valProxy = createProxy(value, currentListener, hookObserverMap.get(rawObj)); + const keyProxy = createProxy(key, currentListener, hookObserverMap.get(rawObj)); // 最后一个参数要返回代理对象 return callback(valProxy, keyProxy, rawObj); }); diff --git a/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts index 30c32fd8..70124cb6 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts @@ -42,18 +42,20 @@ export function createWeakMapProxy( const value = rawObj.get(key); // 对于value也需要进一步代理 - const valProxy = createProxy(value, hookObserverMap.get(rawObj), { - current: change => { - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, [key]: change.mutation.from }, - { ...rawObj, [key]: change.mutation.to } - ); - listener.current({ ...change, mutation }); - listeners.forEach(lst => lst({ ...change, mutation })); + const valProxy = createProxy(value, { + current: change => { + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, [key]: change.mutation.from }, + { ...rawObj, [key]: change.mutation.to } + ); + listener.current({ ...change, mutation }); + listeners.forEach(lst => lst({ ...change, mutation })); + }, }, - }); + hookObserverMap.get(rawObj) + ); return valProxy; } @@ -116,7 +118,7 @@ export function createWeakMapProxy( } if (valChange) { - if (observer.watchers[key]) { + if (observer.watchers?.[key]) { observer.watchers[key].forEach(cb => { cb(key, oldValue, newValue, mutation); }); diff --git a/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts index a9914be9..baaa6a2b 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts @@ -66,18 +66,20 @@ export function createWeakSetProxy( // Set的add方法 function add(rawObj: { add: (any) => void; has: (any) => boolean }, value: any): Object { if (!rawObj.has(proxies.get(value))) { - const proxy = createProxy(value, hookObserverMap.get(rawObj), { - current: change => { - if (!change.parents) change.parents = []; - change.parents.push(rawObj); - let mutation = resolveMutation( - { ...rawObj, [value]: change.mutation.from }, - { ...rawObj, [value]: change.mutation.to } - ); - listener.current({ ...change, mutation }); - listeners.forEach(lst => lst({ ...change, mutation })); + const proxy = createProxy(value, { + current: change => { + if (!change.parents) change.parents = []; + change.parents.push(rawObj); + let mutation = resolveMutation( + { ...rawObj, [value]: change.mutation.from }, + { ...rawObj, [value]: change.mutation.to } + ); + listener.current({ ...change, mutation }); + listeners.forEach(lst => lst({ ...change, mutation })); + }, }, - }); + hookObserverMap.get(rawObj) + ); proxies.set(value, proxy); diff --git a/libs/horizon/src/horizonx/store/StoreHandler.ts b/libs/horizon/src/horizonx/store/StoreHandler.ts index 2afd33f2..469ff88b 100644 --- a/libs/horizon/src/horizonx/store/StoreHandler.ts +++ b/libs/horizon/src/horizonx/store/StoreHandler.ts @@ -161,7 +161,7 @@ export function createStore, C extend current: listener => {}, }; - const proxyObj = createProxy(config.state, !config.options?.isReduxAdapter, listener); + const proxyObj = createProxy(config.state, listener, !config.options?.isReduxAdapter); proxyObj.$pending = false;