From: zhangenming <282126346@qq.com> Date: Wed, 22 Sep 2021 14:11:54 +0000 (+0800) Subject: refactor(reactivity): reuse toReactive helper (#4641) X-Git-Tag: v3.2.14~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52e4ea732d783dbec392f7df773030fc7dfdcc12;p=thirdparty%2Fvuejs%2Fcore.git refactor(reactivity): reuse toReactive helper (#4641) --- diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index fe79ad9e66..0e9e19e824 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -1,14 +1,7 @@ -import { toRaw, reactive, readonly, ReactiveFlags } from './reactive' +import { toRaw, ReactiveFlags, toReactive, toReadonly } from './reactive' import { track, trigger, ITERATE_KEY, MAP_KEY_ITERATE_KEY } from './effect' import { TrackOpTypes, TriggerOpTypes } from './operations' -import { - isObject, - capitalize, - hasOwn, - hasChanged, - toRawType, - isMap -} from '@vue/shared' +import { capitalize, hasOwn, hasChanged, toRawType, isMap } from '@vue/shared' export type CollectionTypes = IterableCollections | WeakCollections @@ -17,12 +10,6 @@ type WeakCollections = WeakMap | WeakSet type MapTypes = Map | WeakMap type SetTypes = Set | WeakSet -const toReactive = (value: T): T => - isObject(value) ? reactive(value) : value - -const toReadonly = (value: T): T => - isObject(value) ? readonly(value as Record) : value - const toShallow = (value: T): T => value const getProto = (v: T): any => diff --git a/packages/reactivity/src/reactive.ts b/packages/reactivity/src/reactive.ts index 2bcce11e71..92fd172707 100644 --- a/packages/reactivity/src/reactive.ts +++ b/packages/reactivity/src/reactive.ts @@ -233,3 +233,9 @@ export function markRaw(value: T): T { def(value, ReactiveFlags.SKIP, true) return value } + +export const toReactive = (value: T): T => + isObject(value) ? reactive(value) : value + +export const toReadonly = (value: T): T => + isObject(value) ? readonly(value as Record) : value diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 32426aaf3e..a4a69b1f18 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -1,7 +1,7 @@ import { isTracking, trackEffects, triggerEffects } from './effect' import { TrackOpTypes, TriggerOpTypes } from './operations' -import { isArray, isObject, hasChanged } from '@vue/shared' -import { reactive, isProxy, toRaw, isReactive } from './reactive' +import { isArray, hasChanged } from '@vue/shared' +import { isProxy, toRaw, isReactive, toReactive } from './reactive' import { CollectionTypes } from './collectionHandlers' import { createDep, Dep } from './dep' @@ -60,9 +60,6 @@ export function triggerRefValue(ref: RefBase, newVal?: any) { } } -const convert = (val: T): T => - isObject(val) ? reactive(val) : val - export function isRef(r: Ref | unknown): r is Ref export function isRef(r: any): r is Ref { return Boolean(r && r.__v_isRef === true) @@ -84,6 +81,13 @@ export function shallowRef(value?: unknown) { return createRef(value, true) } +function createRef(rawValue: unknown, shallow: boolean) { + if (isRef(rawValue)) { + return rawValue + } + return new RefImpl(rawValue, shallow) +} + class RefImpl { private _value: T private _rawValue: T @@ -93,7 +97,7 @@ class RefImpl { constructor(value: T, public readonly _shallow: boolean) { this._rawValue = _shallow ? value : toRaw(value) - this._value = _shallow ? value : convert(value) + this._value = _shallow ? value : toReactive(value) } get value() { @@ -105,19 +109,12 @@ class RefImpl { newVal = this._shallow ? newVal : toRaw(newVal) if (hasChanged(newVal, this._rawValue)) { this._rawValue = newVal - this._value = this._shallow ? newVal : convert(newVal) + this._value = this._shallow ? newVal : toReactive(newVal) triggerRefValue(this, newVal) } } } -function createRef(rawValue: unknown, shallow: boolean) { - if (isRef(rawValue)) { - return rawValue - } - return new RefImpl(rawValue, shallow) -} - export function triggerRef(ref: Ref) { triggerRefValue(ref, __DEV__ ? ref.value : void 0) }