Skip to content

Commit 52e4ea7

Browse files
authored
refactor(reactivity): reuse toReactive helper (#4641)
1 parent 6d6cc90 commit 52e4ea7

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

packages/reactivity/src/collectionHandlers.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
import { toRaw, reactive, readonly, ReactiveFlags } from './reactive'
1+
import { toRaw, ReactiveFlags, toReactive, toReadonly } from './reactive'
22
import { track, trigger, ITERATE_KEY, MAP_KEY_ITERATE_KEY } from './effect'
33
import { TrackOpTypes, TriggerOpTypes } from './operations'
4-
import {
5-
isObject,
6-
capitalize,
7-
hasOwn,
8-
hasChanged,
9-
toRawType,
10-
isMap
11-
} from '@vue/shared'
4+
import { capitalize, hasOwn, hasChanged, toRawType, isMap } from '@vue/shared'
125

136
export type CollectionTypes = IterableCollections | WeakCollections
147

@@ -17,12 +10,6 @@ type WeakCollections = WeakMap<any, any> | WeakSet<any>
1710
type MapTypes = Map<any, any> | WeakMap<any, any>
1811
type SetTypes = Set<any> | WeakSet<any>
1912

20-
const toReactive = <T extends unknown>(value: T): T =>
21-
isObject(value) ? reactive(value) : value
22-
23-
const toReadonly = <T extends unknown>(value: T): T =>
24-
isObject(value) ? readonly(value as Record<any, any>) : value
25-
2613
const toShallow = <T extends unknown>(value: T): T => value
2714

2815
const getProto = <T extends CollectionTypes>(v: T): any =>

packages/reactivity/src/reactive.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,9 @@ export function markRaw<T extends object>(value: T): T {
233233
def(value, ReactiveFlags.SKIP, true)
234234
return value
235235
}
236+
237+
export const toReactive = <T extends unknown>(value: T): T =>
238+
isObject(value) ? reactive(value) : value
239+
240+
export const toReadonly = <T extends unknown>(value: T): T =>
241+
isObject(value) ? readonly(value as Record<any, any>) : value

packages/reactivity/src/ref.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isTracking, trackEffects, triggerEffects } from './effect'
22
import { TrackOpTypes, TriggerOpTypes } from './operations'
3-
import { isArray, isObject, hasChanged } from '@vue/shared'
4-
import { reactive, isProxy, toRaw, isReactive } from './reactive'
3+
import { isArray, hasChanged } from '@vue/shared'
4+
import { isProxy, toRaw, isReactive, toReactive } from './reactive'
55
import { CollectionTypes } from './collectionHandlers'
66
import { createDep, Dep } from './dep'
77

@@ -60,9 +60,6 @@ export function triggerRefValue(ref: RefBase<any>, newVal?: any) {
6060
}
6161
}
6262

63-
const convert = <T extends unknown>(val: T): T =>
64-
isObject(val) ? reactive(val) : val
65-
6663
export function isRef<T>(r: Ref<T> | unknown): r is Ref<T>
6764
export function isRef(r: any): r is Ref {
6865
return Boolean(r && r.__v_isRef === true)
@@ -84,6 +81,13 @@ export function shallowRef(value?: unknown) {
8481
return createRef(value, true)
8582
}
8683

84+
function createRef(rawValue: unknown, shallow: boolean) {
85+
if (isRef(rawValue)) {
86+
return rawValue
87+
}
88+
return new RefImpl(rawValue, shallow)
89+
}
90+
8791
class RefImpl<T> {
8892
private _value: T
8993
private _rawValue: T
@@ -93,7 +97,7 @@ class RefImpl<T> {
9397

9498
constructor(value: T, public readonly _shallow: boolean) {
9599
this._rawValue = _shallow ? value : toRaw(value)
96-
this._value = _shallow ? value : convert(value)
100+
this._value = _shallow ? value : toReactive(value)
97101
}
98102

99103
get value() {
@@ -105,19 +109,12 @@ class RefImpl<T> {
105109
newVal = this._shallow ? newVal : toRaw(newVal)
106110
if (hasChanged(newVal, this._rawValue)) {
107111
this._rawValue = newVal
108-
this._value = this._shallow ? newVal : convert(newVal)
112+
this._value = this._shallow ? newVal : toReactive(newVal)
109113
triggerRefValue(this, newVal)
110114
}
111115
}
112116
}
113117

114-
function createRef(rawValue: unknown, shallow: boolean) {
115-
if (isRef(rawValue)) {
116-
return rawValue
117-
}
118-
return new RefImpl(rawValue, shallow)
119-
}
120-
121118
export function triggerRef(ref: Ref) {
122119
triggerRefValue(ref, __DEV__ ? ref.value : void 0)
123120
}

0 commit comments

Comments
 (0)