Skip to content

Commit 4da3bf0

Browse files
committed
fix(ref): merge review comments
1 parent 58195f0 commit 4da3bf0

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

packages/reactivity/src/computed.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ export interface WritableComputedOptions<T> {
2020
set: ComputedSetter<T>
2121
}
2222

23-
class _ComputedRef<T> {
23+
class ComputedRefImpl<T> {
2424
private _value!: T
2525
private _dirty = true
2626

27-
public readonly effect: ReactiveEffect<T>;
27+
public readonly effect: ReactiveEffect<T>
2828

29-
public [ReactiveFlags.IS_READONLY]: boolean
29+
public readonly __v_isRef = true;
30+
public readonly [ReactiveFlags.IS_READONLY]: boolean
3031

3132
constructor(
3233
getter: ComputedGetter<T>,
@@ -58,10 +59,6 @@ class _ComputedRef<T> {
5859
set value(newValue: T) {
5960
this._setter(newValue)
6061
}
61-
62-
get __v_isRef() {
63-
return true
64-
}
6562
}
6663

6764
export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
@@ -86,7 +83,7 @@ export function computed<T>(
8683
setter = getterOrOptions.set
8784
}
8885

89-
return new _ComputedRef(
86+
return new ComputedRefImpl(
9087
getter,
9188
setter,
9289
isFunction(getterOrOptions) || !getterOrOptions.set

packages/reactivity/src/ref.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ export function shallowRef(value?: unknown) {
4444
return createRef(value, true)
4545
}
4646

47-
class _Ref<T> {
47+
class RefImpl<T> {
4848
private _value: T
4949

50+
public readonly __v_isRef = true
51+
5052
constructor(private _rawValue: T, private readonly _shallow = false) {
5153
this._value = _shallow ? _rawValue : convert(_rawValue)
5254
}
@@ -63,17 +65,13 @@ class _Ref<T> {
6365
trigger(toRaw(this), TriggerOpTypes.SET, 'value', newVal)
6466
}
6567
}
66-
67-
get __v_isRef() {
68-
return true
69-
}
7068
}
7169

7270
function createRef(rawValue: unknown, shallow = false) {
7371
if (isRef(rawValue)) {
7472
return rawValue
7573
}
76-
return new _Ref(rawValue, shallow)
74+
return new RefImpl(rawValue, shallow)
7775
}
7876

7977
export function triggerRef(ref: Ref) {
@@ -113,10 +111,12 @@ export type CustomRefFactory<T> = (
113111
set: (value: T) => void
114112
}
115113

116-
class _CustomRef<T> {
114+
class CustomRefImpl<T> {
117115
private readonly _get: ReturnType<CustomRefFactory<T>>['get']
118116
private readonly _set: ReturnType<CustomRefFactory<T>>['set']
119117

118+
public readonly __v_isRef = true
119+
120120
constructor(factory: CustomRefFactory<T>) {
121121
const { get, set } = factory(
122122
() => track(this, TrackOpTypes.GET, 'value'),
@@ -133,14 +133,10 @@ class _CustomRef<T> {
133133
set value(newVal) {
134134
this._set(newVal)
135135
}
136-
137-
get __v_isRef() {
138-
return true
139-
}
140136
}
141137

142138
export function customRef<T>(factory: CustomRefFactory<T>): Ref<T> {
143-
return new _CustomRef(factory) as any
139+
return new CustomRefImpl(factory) as any
144140
}
145141

146142
export function toRefs<T extends object>(object: T): ToRefs<T> {
@@ -154,7 +150,9 @@ export function toRefs<T extends object>(object: T): ToRefs<T> {
154150
return ret
155151
}
156152

157-
class _ObjectRef<T extends object, K extends keyof T> {
153+
class ObjectRefImpl<T extends object, K extends keyof T> {
154+
public readonly __v_isRef = true
155+
158156
constructor(private readonly _object: T, private readonly _key: K) {}
159157

160158
get value() {
@@ -164,17 +162,13 @@ class _ObjectRef<T extends object, K extends keyof T> {
164162
set value(newVal) {
165163
this._object[this._key] = newVal
166164
}
167-
168-
get __v_isRef() {
169-
return true
170-
}
171165
}
172166

173167
export function toRef<T extends object, K extends keyof T>(
174168
object: T,
175169
key: K
176170
): Ref<T[K]> {
177-
return new _ObjectRef(object, key) as any
171+
return new ObjectRefImpl(object, key) as any
178172
}
179173

180174
// corner case when use narrows type

0 commit comments

Comments
 (0)