Created
November 28, 2021 11:08
-
-
Save sunnyy02/0a1155146d4d7ceb2213fe09fe0b7859 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createReactiveObject( | |
target: Target, | |
isReadonly: boolean, | |
baseHandlers: ProxyHandler<any>, | |
collectionHandlers: ProxyHandler<any>, | |
proxyMap: WeakMap<Target, any> | |
) { | |
if (!isObject(target)) { | |
if (__DEV__) { | |
console.warn(`value cannot be made reactive: ${String(target)}`) | |
} | |
return target | |
} | |
// target is already a Proxy, return it. | |
// exception: calling readonly() on a reactive object | |
if ( | |
target[ReactiveFlags.RAW] && | |
!(isReadonly && target[ReactiveFlags.IS_REACTIVE]) | |
) { | |
return target | |
} | |
// target already has corresponding Proxy | |
const existingProxy = proxyMap.get(target) | |
if (existingProxy) { | |
return existingProxy | |
} | |
// only a whitelist of value types can be observed. | |
const targetType = getTargetType(target) | |
if (targetType === TargetType.INVALID) { | |
return target | |
} | |
const proxy = new Proxy( | |
target, | |
targetType === TargetType.COLLECTION ? collectionHandlers : baseHandlers | |
) | |
proxyMap.set(target, proxy) | |
return proxy | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment