From: Evan You Date: Fri, 12 Jun 2020 13:25:40 +0000 (-0400) Subject: refactor: improve base getter readability X-Git-Tag: v3.0.0-beta.15~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d863ce721f2224e0c04e6350dbc845dcc3aba8be;p=thirdparty%2Fvuejs%2Fcore.git refactor: improve base getter readability --- diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 65838c1305..3f875abaa5 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -60,34 +60,34 @@ function createGetter(isReadonly = false, shallow = false) { if (targetIsArray && hasOwn(arrayInstrumentations, key)) { return Reflect.get(arrayInstrumentations, key, receiver) } + const res = Reflect.get(target, key, receiver) if ((isSymbol(key) && builtInSymbols.has(key)) || key === '__proto__') { return res } - !isReadonly && track(target, TrackOpTypes.GET, key) + if (!isReadonly) { + track(target, TrackOpTypes.GET, key) + } if (shallow) { return res } if (isRef(res)) { - if (targetIsArray) { - return res - } else { - // ref unwrapping, only for Objects, not for Arrays. - return res.value - } + // ref unwrapping, only for Objects, not for Arrays. + return targetIsArray ? res : res.value + } + + if (isObject(res)) { + // Convert returned value into a proxy as well. we do the isObject check + // here to avoid invalid value warning. Also need to lazy access readonly + // and reactive here to avoid circular dependency. + return isReadonly ? readonly(res) : reactive(res) } - return isObject(res) - ? isReadonly - ? // need to lazy access readonly and reactive here to avoid - // circular dependency - readonly(res) - : reactive(res) - : res + return res } }