Skip to content

Commit d863ce7

Browse files
committed
refactor: improve base getter readability
1 parent 80e1693 commit d863ce7

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

packages/reactivity/src/baseHandlers.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,34 @@ function createGetter(isReadonly = false, shallow = false) {
6060
if (targetIsArray && hasOwn(arrayInstrumentations, key)) {
6161
return Reflect.get(arrayInstrumentations, key, receiver)
6262
}
63+
6364
const res = Reflect.get(target, key, receiver)
6465

6566
if ((isSymbol(key) && builtInSymbols.has(key)) || key === '__proto__') {
6667
return res
6768
}
6869

69-
!isReadonly && track(target, TrackOpTypes.GET, key)
70+
if (!isReadonly) {
71+
track(target, TrackOpTypes.GET, key)
72+
}
7073

7174
if (shallow) {
7275
return res
7376
}
7477

7578
if (isRef(res)) {
76-
if (targetIsArray) {
77-
return res
78-
} else {
79-
// ref unwrapping, only for Objects, not for Arrays.
80-
return res.value
81-
}
79+
// ref unwrapping, only for Objects, not for Arrays.
80+
return targetIsArray ? res : res.value
81+
}
82+
83+
if (isObject(res)) {
84+
// Convert returned value into a proxy as well. we do the isObject check
85+
// here to avoid invalid value warning. Also need to lazy access readonly
86+
// and reactive here to avoid circular dependency.
87+
return isReadonly ? readonly(res) : reactive(res)
8288
}
8389

84-
return isObject(res)
85-
? isReadonly
86-
? // need to lazy access readonly and reactive here to avoid
87-
// circular dependency
88-
readonly(res)
89-
: reactive(res)
90-
: res
90+
return res
9191
}
9292
}
9393

0 commit comments

Comments
 (0)