refactor(reactivity): move array ref handling into getter
This commit is contained in:
parent
486dc188fe
commit
09b44e07cb
@ -36,7 +36,8 @@ 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 (isArray(target) && hasOwn(arrayInstrumentations, key)) {
|
const targetIsArray = isArray(target)
|
||||||
|
if (targetIsArray && hasOwn(arrayInstrumentations, key)) {
|
||||||
return Reflect.get(arrayInstrumentations, key, receiver)
|
return Reflect.get(arrayInstrumentations, key, receiver)
|
||||||
}
|
}
|
||||||
const res = Reflect.get(target, key, receiver)
|
const res = Reflect.get(target, key, receiver)
|
||||||
@ -48,18 +49,24 @@ function createGetter(isReadonly = false, shallow = false) {
|
|||||||
// TODO strict mode that returns a shallow-readonly version of the value
|
// TODO strict mode that returns a shallow-readonly version of the value
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
// ref unwrapping, only for Objects, not for Arrays.
|
if (isRef(res)) {
|
||||||
if (isRef(res) && !isArray(target)) {
|
if (targetIsArray) {
|
||||||
return res.value
|
track(target, TrackOpTypes.GET, key)
|
||||||
|
return res
|
||||||
|
} else {
|
||||||
|
// ref unwrapping, only for Objects, not for Arrays.
|
||||||
|
return res.value
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
track(target, TrackOpTypes.GET, key)
|
||||||
|
return isObject(res)
|
||||||
|
? isReadonly
|
||||||
|
? // need to lazy access readonly and reactive here to avoid
|
||||||
|
// circular dependency
|
||||||
|
readonly(res)
|
||||||
|
: reactive(res)
|
||||||
|
: res
|
||||||
}
|
}
|
||||||
track(target, TrackOpTypes.GET, key)
|
|
||||||
return isObject(res)
|
|
||||||
? isReadonly
|
|
||||||
? // need to lazy access readonly and reactive here to avoid
|
|
||||||
// circular dependency
|
|
||||||
readonly(res)
|
|
||||||
: reactive(res)
|
|
||||||
: res
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
mutableCollectionHandlers,
|
mutableCollectionHandlers,
|
||||||
readonlyCollectionHandlers
|
readonlyCollectionHandlers
|
||||||
} from './collectionHandlers'
|
} from './collectionHandlers'
|
||||||
import { UnwrapRef, Ref, isRef } from './ref'
|
import { UnwrapRef, Ref } from './ref'
|
||||||
import { makeMap } from '@vue/shared'
|
import { makeMap } from '@vue/shared'
|
||||||
|
|
||||||
// WeakMaps that store {raw <-> observed} pairs.
|
// WeakMaps that store {raw <-> observed} pairs.
|
||||||
@ -46,9 +46,6 @@ export function reactive(target: object) {
|
|||||||
if (readonlyToRaw.has(target)) {
|
if (readonlyToRaw.has(target)) {
|
||||||
return target
|
return target
|
||||||
}
|
}
|
||||||
if (isRef(target)) {
|
|
||||||
return target
|
|
||||||
}
|
|
||||||
return createReactiveObject(
|
return createReactiveObject(
|
||||||
target,
|
target,
|
||||||
rawToReactive,
|
rawToReactive,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user