From 178e89305d2cdf40a82ebe927a3f43e32508993a Mon Sep 17 00:00:00 2001 From: edison Date: Fri, 26 Jun 2020 21:32:09 +0800 Subject: [PATCH] chore(reactivity): use consistent naming for ReactiveFlags enum (#1428) --- packages/reactivity/src/baseHandlers.ts | 10 ++-- packages/reactivity/src/collectionHandlers.ts | 6 +-- packages/reactivity/src/reactive.ts | 50 +++++++++---------- packages/runtime-core/src/componentProxy.ts | 2 +- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 82e6b98d..fbb18a22 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -42,16 +42,16 @@ const arrayInstrumentations: Record = {} function createGetter(isReadonly = false, shallow = false) { return function get(target: object, key: string | symbol, receiver: object) { - if (key === ReactiveFlags.isReactive) { + if (key === ReactiveFlags.IS_REACTIVE) { return !isReadonly - } else if (key === ReactiveFlags.isReadonly) { + } else if (key === ReactiveFlags.IS_READONLY) { return isReadonly } else if ( - key === ReactiveFlags.raw && + key === ReactiveFlags.RAW && receiver === (isReadonly - ? (target as any).__v_readonly - : (target as any).__v_reactive) + ? (target as any)[ReactiveFlags.READONLY] + : (target as any)[ReactiveFlags.REACTIVE]) ) { return target } diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index 1e908d96..4d6cd99c 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -298,11 +298,11 @@ function createInstrumentationGetter(isReadonly: boolean, shallow: boolean) { key: string | symbol, receiver: CollectionTypes ) => { - if (key === ReactiveFlags.isReactive) { + if (key === ReactiveFlags.IS_REACTIVE) { return !isReadonly - } else if (key === ReactiveFlags.isReadonly) { + } else if (key === ReactiveFlags.IS_READONLY) { return isReadonly - } else if (key === ReactiveFlags.raw) { + } else if (key === ReactiveFlags.RAW) { return target } diff --git a/packages/reactivity/src/reactive.ts b/packages/reactivity/src/reactive.ts index 287385ca..0dfec8a9 100644 --- a/packages/reactivity/src/reactive.ts +++ b/packages/reactivity/src/reactive.ts @@ -13,21 +13,21 @@ import { import { UnwrapRef, Ref } from './ref' export const enum ReactiveFlags { - skip = '__v_skip', - isReactive = '__v_isReactive', - isReadonly = '__v_isReadonly', - raw = '__v_raw', - reactive = '__v_reactive', - readonly = '__v_readonly' + SKIP = '__v_skip', + IS_REACTIVE = '__v_isReactive', + IS_READONLY = '__v_isReadonly', + RAW = '__v_raw', + REACTIVE = '__v_reactive', + READONLY = '__v_readonly' } interface Target { - __v_skip?: boolean - __v_isReactive?: boolean - __v_isReadonly?: boolean - __v_raw?: any - __v_reactive?: any - __v_readonly?: any + [ReactiveFlags.SKIP]?: boolean + [ReactiveFlags.IS_REACTIVE]?: boolean + [ReactiveFlags.IS_READONLY]?: boolean + [ReactiveFlags.RAW]?: any + [ReactiveFlags.REACTIVE]?: any + [ReactiveFlags.READONLY]?: any } const collectionTypes = new Set([Set, Map, WeakMap, WeakSet]) @@ -37,7 +37,7 @@ const isObservableType = /*#__PURE__*/ makeMap( const canObserve = (value: Target): boolean => { return ( - !value[ReactiveFlags.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)[ReactiveFlags.isReadonly]) { + if (target && (target as Target)[ReactiveFlags.IS_READONLY]) { return target } return createReactiveObject( @@ -113,18 +113,18 @@ function createReactiveObject( // target is already a Proxy, return it. // exception: calling readonly() on a reactive object if ( - target[ReactiveFlags.raw] && - !(isReadonly && target[ReactiveFlags.isReactive]) + target[ReactiveFlags.RAW] && + !(isReadonly && target[ReactiveFlags.IS_REACTIVE]) ) { return target } // target already has corresponding Proxy if ( - hasOwn(target, isReadonly ? ReactiveFlags.readonly : ReactiveFlags.reactive) + hasOwn(target, isReadonly ? ReactiveFlags.READONLY : ReactiveFlags.REACTIVE) ) { return isReadonly - ? target[ReactiveFlags.readonly] - : target[ReactiveFlags.reactive] + ? target[ReactiveFlags.READONLY] + : target[ReactiveFlags.REACTIVE] } // only a whitelist of value types can be observed. if (!canObserve(target)) { @@ -136,7 +136,7 @@ function createReactiveObject( ) def( target, - isReadonly ? ReactiveFlags.readonly : ReactiveFlags.reactive, + isReadonly ? ReactiveFlags.READONLY : ReactiveFlags.REACTIVE, observed ) return observed @@ -144,13 +144,13 @@ function createReactiveObject( export function isReactive(value: unknown): boolean { if (isReadonly(value)) { - return isReactive((value as Target)[ReactiveFlags.raw]) + return isReactive((value as Target)[ReactiveFlags.RAW]) } - return !!(value && (value as Target)[ReactiveFlags.isReactive]) + return !!(value && (value as Target)[ReactiveFlags.IS_REACTIVE]) } export function isReadonly(value: unknown): boolean { - return !!(value && (value as Target)[ReactiveFlags.isReadonly]) + return !!(value && (value as Target)[ReactiveFlags.IS_READONLY]) } export function isProxy(value: unknown): boolean { @@ -159,11 +159,11 @@ export function isProxy(value: unknown): boolean { export function toRaw(observed: T): T { return ( - (observed && toRaw((observed as Target)[ReactiveFlags.raw])) || observed + (observed && toRaw((observed as Target)[ReactiveFlags.RAW])) || observed ) } export function markRaw(value: T): T { - def(value, ReactiveFlags.skip, true) + def(value, ReactiveFlags.SKIP, true) return value } diff --git a/packages/runtime-core/src/componentProxy.ts b/packages/runtime-core/src/componentProxy.ts index de7765d3..f1aeece4 100644 --- a/packages/runtime-core/src/componentProxy.ts +++ b/packages/runtime-core/src/componentProxy.ts @@ -212,7 +212,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { } = instance // let @vue/reatvitiy know it should never observe Vue public instances. - if (key === ReactiveFlags.skip) { + if (key === ReactiveFlags.SKIP) { return true }