diff --git a/libs/horizon/index.ts b/libs/horizon/index.ts index efe6df6b..90c536d5 100644 --- a/libs/horizon/index.ts +++ b/libs/horizon/index.ts @@ -69,6 +69,7 @@ import { } from './src/dom/DOMExternal'; import { syncUpdates as flushSync } from './src/renderer/TreeBuilder'; +import { toRaw } from './src/horizonx/proxy/ProxyHandler'; const Horizon = { Children, @@ -157,6 +158,7 @@ export { clearStore, reduxAdapter, watch, + toRaw, // 兼容ReactIs isFragment, isElement, diff --git a/libs/horizon/src/horizonx/Constants.ts b/libs/horizon/src/horizonx/Constants.ts index 6d65ab52..205e008e 100644 --- a/libs/horizon/src/horizonx/Constants.ts +++ b/libs/horizon/src/horizonx/Constants.ts @@ -13,4 +13,4 @@ * See the Mulan PSL v2 for more details. */ -export const OBSERVER_KEY = '_horizonObserver'; +export const OBSERVER_KEY = typeof Symbol === 'function' ? Symbol('_horizonObserver') : '_horizonObserver'; diff --git a/libs/horizon/src/horizonx/proxy/ProxyHandler.ts b/libs/horizon/src/horizonx/proxy/ProxyHandler.ts index d37cca1a..d27c77eb 100644 --- a/libs/horizon/src/horizonx/proxy/ProxyHandler.ts +++ b/libs/horizon/src/horizonx/proxy/ProxyHandler.ts @@ -52,7 +52,15 @@ export function createProxy(rawObj: any, listener: { current: (...args) => any } let observer: IObserver = getObserver(rawObj); if (!observer) { observer = isHookObserver ? new Observer() : new HooklessObserver(); - rawObj[OBSERVER_KEY] = observer; + if (typeof OBSERVER_KEY === 'string') { + Object.defineProperty(rawObj, OBSERVER_KEY, { + configurable: false, + enumerable: false, + value: observer, + }); + } else { + rawObj[OBSERVER_KEY] = observer; + } } hookObserverMap.set(rawObj, isHookObserver); @@ -96,3 +104,7 @@ export function createProxy(rawObj: any, listener: { current: (...args) => any } return proxyObj; } + +export function toRaw(observed: T): T { + return observed && (observed)['_rawValue']; +} diff --git a/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts b/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts index ea94e4a0..7e0c1810 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/ArrayProxyHandler.ts @@ -137,6 +137,10 @@ export function createArrayProxy(rawObj: any[], listener: { current: (...args) = return objectGet(rawObj, key, receiver); } + if (key === '_rawValue') { + return rawObj; + } + return Reflect.get(rawObj, key, receiver); } diff --git a/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts index 03accb28..eee505b7 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/MapProxy.ts @@ -388,6 +388,10 @@ export function createMapProxy( }; } + if (key === '_rawValue') { + return rawObj; + } + return Reflect.get(rawObj, key, receiver); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts b/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts index 1a216477..13147980 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/ObjectProxyHandler.ts @@ -78,6 +78,10 @@ export function createObjectProxy( }; } + if (key === '_rawValue') { + return rawObj; + } + observer.useProp(key); const value = Reflect.get(rawObj, key, receiver); diff --git a/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts index e5b19e4f..4295da46 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/SetProxy.ts @@ -179,6 +179,11 @@ export function createSetProxy( }; }; } + + if (key === '_rawValue') { + return rawObj; + } + return Reflect.get(rawObj, key, receiver); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts index 70124cb6..97b462b7 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/WeakMapProxy.ts @@ -96,6 +96,10 @@ export function createWeakMapProxy( }; } + if (key === '_rawValue') { + return rawObj; + } + return Reflect.get(rawObj, key, receiver); } diff --git a/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts b/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts index baaa6a2b..8d768a65 100644 --- a/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts +++ b/libs/horizon/src/horizonx/proxy/handlers/WeakSetProxy.ts @@ -60,6 +60,11 @@ export function createWeakSetProxy( }; }; } + + if (key === '_rawValue') { + return rawObj; + } + return Reflect.get(rawObj, key, receiver); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////