diff --git a/libs/horizon/src/horizonx/Constants.ts b/libs/horizon/src/horizonx/Constants.ts index 205e008e..ded649bd 100644 --- a/libs/horizon/src/horizonx/Constants.ts +++ b/libs/horizon/src/horizonx/Constants.ts @@ -14,3 +14,5 @@ */ export const OBSERVER_KEY = typeof Symbol === 'function' ? Symbol('_horizonObserver') : '_horizonObserver'; + +export const RAW_VALUE = '_rawValue'; diff --git a/libs/horizon/src/horizonx/proxy/ProxyHandler.ts b/libs/horizon/src/horizonx/proxy/ProxyHandler.ts index d27c77eb..6d4027af 100644 --- a/libs/horizon/src/horizonx/proxy/ProxyHandler.ts +++ b/libs/horizon/src/horizonx/proxy/ProxyHandler.ts @@ -20,7 +20,7 @@ import { isArray, isCollection, isObject } from '../CommonUtils'; import { createArrayProxy } from './handlers/ArrayProxyHandler'; import { createCollectionProxy } from './handlers/CollectionProxyHandler'; import type { IObserver } from '../types'; -import { OBSERVER_KEY } from '../Constants'; +import { OBSERVER_KEY, RAW_VALUE } from '../Constants'; // 保存rawObj -> Proxy const proxyMap = new WeakMap(); @@ -31,6 +31,18 @@ export function getObserver(rawObj: any): Observer { return rawObj[OBSERVER_KEY]; } +const setObserverKey = typeof OBSERVER_KEY === 'string' + ? (rawObj, observer) => { + Object.defineProperty(rawObj, OBSERVER_KEY, { + configurable: false, + enumerable: false, + value: observer, + }); + } + : (rawObj, observer) => { + rawObj[OBSERVER_KEY] = observer; + }; + export function createProxy(rawObj: any, listener: { current: (...args) => any }, isHookObserver = true): any { // 不是对象(是原始数据类型)不用代理 if (!(rawObj && isObject(rawObj))) { @@ -52,15 +64,7 @@ export function createProxy(rawObj: any, listener: { current: (...args) => any } let observer: IObserver = getObserver(rawObj); if (!observer) { observer = isHookObserver ? new Observer() : new HooklessObserver(); - if (typeof OBSERVER_KEY === 'string') { - Object.defineProperty(rawObj, OBSERVER_KEY, { - configurable: false, - enumerable: false, - value: observer, - }); - } else { - rawObj[OBSERVER_KEY] = observer; - } + setObserverKey(rawObj, observer); } hookObserverMap.set(rawObj, isHookObserver); @@ -106,5 +110,5 @@ export function createProxy(rawObj: any, listener: { current: (...args) => any } } export function toRaw(observed: T): T { - return observed && (observed)['_rawValue']; + return observed && (observed)[RAW_VALUE]; } diff --git a/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts b/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts index 7e0c1810..c5aa46e3 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts @@ -17,7 +17,7 @@ import { createProxy, getObserver, hookObserverMap } from '../ProxyHandler'; import { isSame, isValidIntegerKey } from '../../CommonUtils'; import { resolveMutation } from '../../CommonUtils'; import { isPanelActive } from '../../devtools'; -import { OBSERVER_KEY } from '../../Constants'; +import { OBSERVER_KEY, RAW_VALUE } from '../../Constants'; function set(rawObj: any[], key: string, value: any, receiver: any) { const oldValue = rawObj[key]; @@ -137,7 +137,7 @@ export function createArrayProxy(rawObj: any[], listener: { current: (...args) = return objectGet(rawObj, key, receiver); } - if (key === '_rawValue') { + if (key === RAW_VALUE) { return rawObj; } diff --git a/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts index eee505b7..62ab7d74 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts @@ -17,6 +17,7 @@ import { createProxy, getObserver, hookObserverMap } from '../ProxyHandler'; import { isSame } from '../../CommonUtils'; import { resolveMutation } from '../../CommonUtils'; import { isPanelActive } from '../../devtools'; +import { RAW_VALUE } from "../../Constants"; const COLLECTION_CHANGE = '_collectionChange'; @@ -388,7 +389,7 @@ export function createMapProxy( }; } - if (key === '_rawValue') { + if (key === RAW_VALUE) { return rawObj; } diff --git a/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts b/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts index 13147980..0244bbdf 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts @@ -15,7 +15,7 @@ import { isSame, resolveMutation } from '../../CommonUtils'; import { createProxy, getObserver, hookObserverMap } from '../ProxyHandler'; -import { OBSERVER_KEY } from '../../Constants'; +import { OBSERVER_KEY, RAW_VALUE } from '../../Constants'; import { isPanelActive } from '../../devtools'; function set(rawObj: object, key: string, value: any, receiver: any): boolean { @@ -78,7 +78,7 @@ export function createObjectProxy( }; } - if (key === '_rawValue') { + if (key === RAW_VALUE) { return rawObj; } diff --git a/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts index 4295da46..1270b9c6 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts @@ -15,6 +15,7 @@ import { resolveMutation } from '../../CommonUtils'; import { createProxy, getObserver, hookObserverMap } from '../ProxyHandler'; +import { RAW_VALUE } from "../../Constants"; const COLLECTION_CHANGE = '_collectionChange'; @@ -180,7 +181,7 @@ export function createSetProxy( }; } - if (key === '_rawValue') { + if (key === RAW_VALUE) { return rawObj; } diff --git a/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts index 97b462b7..14d13178 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts @@ -17,6 +17,7 @@ import { createProxy, getObserver, hookObserverMap } from '../ProxyHandler'; import { isSame } from '../../CommonUtils'; import { resolveMutation } from '../../CommonUtils'; import { isPanelActive } from '../../devtools'; +import { RAW_VALUE } from "../../Constants"; const COLLECTION_CHANGE = '_collectionChange'; @@ -96,7 +97,7 @@ export function createWeakMapProxy( }; } - if (key === '_rawValue') { + if (key === RAW_VALUE) { return rawObj; } diff --git a/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts index 8d768a65..3131225e 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts @@ -15,6 +15,7 @@ import { resolveMutation } from '../../CommonUtils'; import { createProxy, getObserver, hookObserverMap } from '../ProxyHandler'; +import { RAW_VALUE } from "../../Constants"; export function createWeakSetProxy( rawObj: T, @@ -61,7 +62,7 @@ export function createWeakSetProxy( }; } - if (key === '_rawValue') { + if (key === RAW_VALUE) { return rawObj; }