From f3eac11058f7f199c5d850730f29031fcf9e5b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=9C=97=E7=89=9B=E8=80=81=E6=B9=BF?= Date: Fri, 12 Jun 2020 05:32:44 +0800 Subject: [PATCH] chore(reactivity): use ReactiveFlags instead of __v_ properties (#1313) --- packages/reactivity/src/reactive.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/reactivity/src/reactive.ts b/packages/reactivity/src/reactive.ts index 67dadf09..287385ca 100644 --- a/packages/reactivity/src/reactive.ts +++ b/packages/reactivity/src/reactive.ts @@ -37,7 +37,7 @@ const isObservableType = /*#__PURE__*/ makeMap( const canObserve = (value: Target): boolean => { return ( - !value.__v_skip && + !value[ReactiveFlags.skip] && isObservableType(toRawType(value)) && !Object.isFrozen(value) ) @@ -49,7 +49,7 @@ type UnwrapNestedRefs = T extends Ref ? T : UnwrapRef export function reactive(target: T): UnwrapNestedRefs export function reactive(target: object) { // if trying to observe a readonly proxy, return the readonly version. - if (target && (target as Target).__v_isReadonly) { + if (target && (target as Target)[ReactiveFlags.isReadonly]) { return target } return createReactiveObject( @@ -112,14 +112,19 @@ function createReactiveObject( } // target is already a Proxy, return it. // exception: calling readonly() on a reactive object - if (target.__v_raw && !(isReadonly && target.__v_isReactive)) { + if ( + target[ReactiveFlags.raw] && + !(isReadonly && target[ReactiveFlags.isReactive]) + ) { return target } // target already has corresponding Proxy if ( hasOwn(target, isReadonly ? ReactiveFlags.readonly : ReactiveFlags.reactive) ) { - return isReadonly ? target.__v_readonly : target.__v_reactive + return isReadonly + ? target[ReactiveFlags.readonly] + : target[ReactiveFlags.reactive] } // only a whitelist of value types can be observed. if (!canObserve(target)) { @@ -139,13 +144,13 @@ function createReactiveObject( export function isReactive(value: unknown): boolean { if (isReadonly(value)) { - return isReactive((value as Target).__v_raw) + return isReactive((value as Target)[ReactiveFlags.raw]) } - return !!(value && (value as Target).__v_isReactive) + return !!(value && (value as Target)[ReactiveFlags.isReactive]) } export function isReadonly(value: unknown): boolean { - return !!(value && (value as Target).__v_isReadonly) + return !!(value && (value as Target)[ReactiveFlags.isReadonly]) } export function isProxy(value: unknown): boolean { @@ -153,7 +158,9 @@ export function isProxy(value: unknown): boolean { } export function toRaw(observed: T): T { - return (observed && toRaw((observed as Target).__v_raw)) || observed + return ( + (observed && toRaw((observed as Target)[ReactiveFlags.raw])) || observed + ) } export function markRaw(value: T): T {