chore(reactivity): use consistent naming for ReactiveFlags enum (#1428)
This commit is contained in:
parent
e765d81404
commit
178e89305d
@ -42,16 +42,16 @@ const arrayInstrumentations: Record<string, Function> = {}
|
|||||||
|
|
||||||
function createGetter(isReadonly = false, shallow = false) {
|
function createGetter(isReadonly = false, shallow = false) {
|
||||||
return function get(target: object, key: string | symbol, receiver: object) {
|
return function get(target: object, key: string | symbol, receiver: object) {
|
||||||
if (key === ReactiveFlags.isReactive) {
|
if (key === ReactiveFlags.IS_REACTIVE) {
|
||||||
return !isReadonly
|
return !isReadonly
|
||||||
} else if (key === ReactiveFlags.isReadonly) {
|
} else if (key === ReactiveFlags.IS_READONLY) {
|
||||||
return isReadonly
|
return isReadonly
|
||||||
} else if (
|
} else if (
|
||||||
key === ReactiveFlags.raw &&
|
key === ReactiveFlags.RAW &&
|
||||||
receiver ===
|
receiver ===
|
||||||
(isReadonly
|
(isReadonly
|
||||||
? (target as any).__v_readonly
|
? (target as any)[ReactiveFlags.READONLY]
|
||||||
: (target as any).__v_reactive)
|
: (target as any)[ReactiveFlags.REACTIVE])
|
||||||
) {
|
) {
|
||||||
return target
|
return target
|
||||||
}
|
}
|
||||||
|
@ -298,11 +298,11 @@ function createInstrumentationGetter(isReadonly: boolean, shallow: boolean) {
|
|||||||
key: string | symbol,
|
key: string | symbol,
|
||||||
receiver: CollectionTypes
|
receiver: CollectionTypes
|
||||||
) => {
|
) => {
|
||||||
if (key === ReactiveFlags.isReactive) {
|
if (key === ReactiveFlags.IS_REACTIVE) {
|
||||||
return !isReadonly
|
return !isReadonly
|
||||||
} else if (key === ReactiveFlags.isReadonly) {
|
} else if (key === ReactiveFlags.IS_READONLY) {
|
||||||
return isReadonly
|
return isReadonly
|
||||||
} else if (key === ReactiveFlags.raw) {
|
} else if (key === ReactiveFlags.RAW) {
|
||||||
return target
|
return target
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,21 +13,21 @@ import {
|
|||||||
import { UnwrapRef, Ref } from './ref'
|
import { UnwrapRef, Ref } from './ref'
|
||||||
|
|
||||||
export const enum ReactiveFlags {
|
export const enum ReactiveFlags {
|
||||||
skip = '__v_skip',
|
SKIP = '__v_skip',
|
||||||
isReactive = '__v_isReactive',
|
IS_REACTIVE = '__v_isReactive',
|
||||||
isReadonly = '__v_isReadonly',
|
IS_READONLY = '__v_isReadonly',
|
||||||
raw = '__v_raw',
|
RAW = '__v_raw',
|
||||||
reactive = '__v_reactive',
|
REACTIVE = '__v_reactive',
|
||||||
readonly = '__v_readonly'
|
READONLY = '__v_readonly'
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Target {
|
interface Target {
|
||||||
__v_skip?: boolean
|
[ReactiveFlags.SKIP]?: boolean
|
||||||
__v_isReactive?: boolean
|
[ReactiveFlags.IS_REACTIVE]?: boolean
|
||||||
__v_isReadonly?: boolean
|
[ReactiveFlags.IS_READONLY]?: boolean
|
||||||
__v_raw?: any
|
[ReactiveFlags.RAW]?: any
|
||||||
__v_reactive?: any
|
[ReactiveFlags.REACTIVE]?: any
|
||||||
__v_readonly?: any
|
[ReactiveFlags.READONLY]?: any
|
||||||
}
|
}
|
||||||
|
|
||||||
const collectionTypes = new Set<Function>([Set, Map, WeakMap, WeakSet])
|
const collectionTypes = new Set<Function>([Set, Map, WeakMap, WeakSet])
|
||||||
@ -37,7 +37,7 @@ const isObservableType = /*#__PURE__*/ makeMap(
|
|||||||
|
|
||||||
const canObserve = (value: Target): boolean => {
|
const canObserve = (value: Target): boolean => {
|
||||||
return (
|
return (
|
||||||
!value[ReactiveFlags.skip] &&
|
!value[ReactiveFlags.SKIP] &&
|
||||||
isObservableType(toRawType(value)) &&
|
isObservableType(toRawType(value)) &&
|
||||||
!Object.isFrozen(value)
|
!Object.isFrozen(value)
|
||||||
)
|
)
|
||||||
@ -49,7 +49,7 @@ type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRef<T>
|
|||||||
export function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
|
export function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
|
||||||
export function reactive(target: object) {
|
export function reactive(target: object) {
|
||||||
// if trying to observe a readonly proxy, return the readonly version.
|
// 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 target
|
||||||
}
|
}
|
||||||
return createReactiveObject(
|
return createReactiveObject(
|
||||||
@ -113,18 +113,18 @@ function createReactiveObject(
|
|||||||
// target is already a Proxy, return it.
|
// target is already a Proxy, return it.
|
||||||
// exception: calling readonly() on a reactive object
|
// exception: calling readonly() on a reactive object
|
||||||
if (
|
if (
|
||||||
target[ReactiveFlags.raw] &&
|
target[ReactiveFlags.RAW] &&
|
||||||
!(isReadonly && target[ReactiveFlags.isReactive])
|
!(isReadonly && target[ReactiveFlags.IS_REACTIVE])
|
||||||
) {
|
) {
|
||||||
return target
|
return target
|
||||||
}
|
}
|
||||||
// target already has corresponding Proxy
|
// target already has corresponding Proxy
|
||||||
if (
|
if (
|
||||||
hasOwn(target, isReadonly ? ReactiveFlags.readonly : ReactiveFlags.reactive)
|
hasOwn(target, isReadonly ? ReactiveFlags.READONLY : ReactiveFlags.REACTIVE)
|
||||||
) {
|
) {
|
||||||
return isReadonly
|
return isReadonly
|
||||||
? target[ReactiveFlags.readonly]
|
? target[ReactiveFlags.READONLY]
|
||||||
: target[ReactiveFlags.reactive]
|
: target[ReactiveFlags.REACTIVE]
|
||||||
}
|
}
|
||||||
// only a whitelist of value types can be observed.
|
// only a whitelist of value types can be observed.
|
||||||
if (!canObserve(target)) {
|
if (!canObserve(target)) {
|
||||||
@ -136,7 +136,7 @@ function createReactiveObject(
|
|||||||
)
|
)
|
||||||
def(
|
def(
|
||||||
target,
|
target,
|
||||||
isReadonly ? ReactiveFlags.readonly : ReactiveFlags.reactive,
|
isReadonly ? ReactiveFlags.READONLY : ReactiveFlags.REACTIVE,
|
||||||
observed
|
observed
|
||||||
)
|
)
|
||||||
return observed
|
return observed
|
||||||
@ -144,13 +144,13 @@ function createReactiveObject(
|
|||||||
|
|
||||||
export function isReactive(value: unknown): boolean {
|
export function isReactive(value: unknown): boolean {
|
||||||
if (isReadonly(value)) {
|
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 {
|
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 {
|
export function isProxy(value: unknown): boolean {
|
||||||
@ -159,11 +159,11 @@ export function isProxy(value: unknown): boolean {
|
|||||||
|
|
||||||
export function toRaw<T>(observed: T): T {
|
export function toRaw<T>(observed: T): T {
|
||||||
return (
|
return (
|
||||||
(observed && toRaw((observed as Target)[ReactiveFlags.raw])) || observed
|
(observed && toRaw((observed as Target)[ReactiveFlags.RAW])) || observed
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function markRaw<T extends object>(value: T): T {
|
export function markRaw<T extends object>(value: T): T {
|
||||||
def(value, ReactiveFlags.skip, true)
|
def(value, ReactiveFlags.SKIP, true)
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
@ -212,7 +212,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
|
|||||||
} = instance
|
} = instance
|
||||||
|
|
||||||
// let @vue/reatvitiy know it should never observe Vue public instances.
|
// let @vue/reatvitiy know it should never observe Vue public instances.
|
||||||
if (key === ReactiveFlags.skip) {
|
if (key === ReactiveFlags.SKIP) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user