diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 243847ed2a7..8a0add0be95 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -126,18 +126,7 @@ class RefImpl implements Dependency { } get value() { - const activeTrackId = System.activeTrackId - if (activeTrackId !== 0 && this.linkedTrackId !== activeTrackId) { - this.linkedTrackId = activeTrackId - if (__DEV__) { - onTrack(System.activeSub!, { - target: this, - type: TrackOpTypes.GET, - key: 'value', - }) - } - Dependency.linkSubscriber(this, System.activeSub!) - } + track(this) return this._value } @@ -151,22 +140,18 @@ class RefImpl implements Dependency { if (hasChanged(newValue, oldValue)) { this._rawValue = newValue this._value = useDirectValue ? newValue : toReactive(newValue) - if (this.subs !== undefined) { - if (__DEV__) { - triggerEventInfos.push({ - target: this, - type: TriggerOpTypes.SET, - key: 'value', - newValue, - oldValue, - }) - } - startBatch() - Dependency.propagate(this.subs) - endBatch() - if (__DEV__) { - triggerEventInfos.pop() - } + if (__DEV__) { + triggerEventInfos.push({ + target: this, + type: TriggerOpTypes.SET, + key: 'value', + newValue, + oldValue, + }) + } + triggerRef(this as unknown as Ref) + if (__DEV__) { + triggerEventInfos.pop() } } } @@ -199,13 +184,28 @@ class RefImpl implements Dependency { */ export function triggerRef(ref: Ref): void { // ref may be an instance of ObjectRefImpl - if ((ref as unknown as RefImpl).subs !== undefined) { + if ((ref as unknown as Dependency).subs !== undefined) { startBatch() - Dependency.propagate((ref as unknown as RefImpl).subs!) + Dependency.propagate((ref as unknown as Dependency).subs!) endBatch() } } +function track(dep: Dependency) { + const activeTrackId = System.activeTrackId + if (activeTrackId !== 0 && dep.linkedTrackId !== activeTrackId) { + dep.linkedTrackId = activeTrackId + if (__DEV__) { + onTrack(System.activeSub!, { + target: dep, + type: TrackOpTypes.GET, + key: 'value', + }) + } + Dependency.linkSubscriber(dep, System.activeSub!) + } +} + export type MaybeRef = | T | Ref @@ -308,7 +308,10 @@ class CustomRefImpl implements Dependency { public _value: T = undefined! constructor(factory: CustomRefFactory) { - const { get, set } = factory(this.track.bind(this), this.trigger.bind(this)) + const { get, set } = factory( + () => track(this), + () => triggerRef(this as unknown as Ref), + ) this._get = get this._set = set } @@ -320,29 +323,6 @@ class CustomRefImpl implements Dependency { set value(newVal) { this._set(newVal) } - - track() { - const activeTrackId = System.activeTrackId - if (activeTrackId !== 0 && this.linkedTrackId !== activeTrackId) { - this.linkedTrackId = activeTrackId - if (__DEV__) { - onTrack(System.activeSub!, { - target: this, - type: TrackOpTypes.GET, - key: 'value', - }) - } - Dependency.linkSubscriber(this, System.activeSub!) - } - } - - trigger() { - if (this.subs !== undefined) { - startBatch() - Dependency.propagate(this.subs) - endBatch() - } - } } /**