Skip to content

Commit 09b44e0

Browse files
committed
refactor(reactivity): move array ref handling into getter
1 parent 486dc18 commit 09b44e0

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

packages/reactivity/src/baseHandlers.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const arrayInstrumentations: Record<string, Function> = {}
3636

3737
function createGetter(isReadonly = false, shallow = false) {
3838
return function get(target: object, key: string | symbol, receiver: object) {
39-
if (isArray(target) && hasOwn(arrayInstrumentations, key)) {
39+
const targetIsArray = isArray(target)
40+
if (targetIsArray && hasOwn(arrayInstrumentations, key)) {
4041
return Reflect.get(arrayInstrumentations, key, receiver)
4142
}
4243
const res = Reflect.get(target, key, receiver)
@@ -48,18 +49,24 @@ function createGetter(isReadonly = false, shallow = false) {
4849
// TODO strict mode that returns a shallow-readonly version of the value
4950
return res
5051
}
51-
// ref unwrapping, only for Objects, not for Arrays.
52-
if (isRef(res) && !isArray(target)) {
53-
return res.value
52+
if (isRef(res)) {
53+
if (targetIsArray) {
54+
track(target, TrackOpTypes.GET, key)
55+
return res
56+
} else {
57+
// ref unwrapping, only for Objects, not for Arrays.
58+
return res.value
59+
}
60+
} else {
61+
track(target, TrackOpTypes.GET, key)
62+
return isObject(res)
63+
? isReadonly
64+
? // need to lazy access readonly and reactive here to avoid
65+
// circular dependency
66+
readonly(res)
67+
: reactive(res)
68+
: res
5469
}
55-
track(target, TrackOpTypes.GET, key)
56-
return isObject(res)
57-
? isReadonly
58-
? // need to lazy access readonly and reactive here to avoid
59-
// circular dependency
60-
readonly(res)
61-
: reactive(res)
62-
: res
6370
}
6471
}
6572

packages/reactivity/src/reactive.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
mutableCollectionHandlers,
1010
readonlyCollectionHandlers
1111
} from './collectionHandlers'
12-
import { UnwrapRef, Ref, isRef } from './ref'
12+
import { UnwrapRef, Ref } from './ref'
1313
import { makeMap } from '@vue/shared'
1414

1515
// WeakMaps that store {raw <-> observed} pairs.
@@ -46,9 +46,6 @@ export function reactive(target: object) {
4646
if (readonlyToRaw.has(target)) {
4747
return target
4848
}
49-
if (isRef(target)) {
50-
return target
51-
}
5249
return createReactiveObject(
5350
target,
5451
rawToReactive,

0 commit comments

Comments
 (0)