Match-id-6d5ca3fa4860f0cee0c43f85a5510f326302938b

This commit is contained in:
* 2023-06-07 17:51:35 +08:00
parent 75dabdb169
commit e867a248aa
8 changed files with 29 additions and 19 deletions

View File

@ -14,3 +14,5 @@
*/
export const OBSERVER_KEY = typeof Symbol === 'function' ? Symbol('_horizonObserver') : '_horizonObserver';
export const RAW_VALUE = '_rawValue';

View File

@ -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<T>(observed: T): T {
return observed && (observed)['_rawValue'];
return observed && (observed)[RAW_VALUE];
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<T extends object>(
};
}
if (key === '_rawValue') {
if (key === RAW_VALUE) {
return rawObj;
}

View File

@ -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<T extends object>(
};
}
if (key === '_rawValue') {
if (key === RAW_VALUE) {
return rawObj;
}

View File

@ -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;
}

View File

@ -15,6 +15,7 @@
import { resolveMutation } from '../../CommonUtils';
import { createProxy, getObserver, hookObserverMap } from '../ProxyHandler';
import { RAW_VALUE } from "../../Constants";
export function createWeakSetProxy<T extends object>(
rawObj: T,
@ -61,7 +62,7 @@ export function createWeakSetProxy<T extends object>(
};
}
if (key === '_rawValue') {
if (key === RAW_VALUE) {
return rawObj;
}