chore(reactivity): use ReactiveFlags instead of __v_ properties (#1313)

This commit is contained in:
蜗牛老湿 2020-06-12 05:32:44 +08:00 committed by GitHub
parent d4e9b19932
commit f3eac11058
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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> = T extends Ref ? T : UnwrapRef<T>
export function reactive<T extends object>(target: T): UnwrapNestedRefs<T>
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<T>(observed: T): T {
return (observed && toRaw((observed as Target).__v_raw)) || observed
return (
(observed && toRaw((observed as Target)[ReactiveFlags.raw])) || observed
)
}
export function markRaw<T extends object>(value: T): T {