@@ -5,21 +5,20 @@ import {
55 type DebuggerEvent ,
66 type DebuggerOptions ,
77 activeSub ,
8- activeTrackId ,
9- nextTrackId ,
108 setActiveSub ,
119} from './effect'
1210import { activeEffectScope } from './effectScope'
1311import type { Ref } from './ref'
1412import {
1513 type Dependency ,
16- type IComputed ,
1714 type Link ,
15+ type Subscriber ,
1816 SubscriberFlags ,
19- checkDirty ,
20- endTrack ,
17+ endTracking ,
2118 link ,
22- startTrack ,
19+ processComputedUpdate ,
20+ startTracking ,
21+ updateDirtyFlag ,
2322} from './system'
2423import { warn } from './warning'
2524
@@ -54,22 +53,20 @@ export interface WritableComputedOptions<T, S = T> {
5453 * @private exported by @vue/reactivity for Vue core use, but not exported from
5554 * the main vue package
5655 */
57- export class ComputedRefImpl < T = any > implements IComputed {
56+ export class ComputedRefImpl < T = any > implements Dependency , Subscriber {
5857 /**
5958 * @internal
6059 */
6160 _value : T | undefined = undefined
62- version = 0
6361
6462 // Dependency
6563 subs : Link | undefined = undefined
6664 subsTail : Link | undefined = undefined
67- lastTrackedId = 0
6865
6966 // Subscriber
7067 deps : Link | undefined = undefined
7168 depsTail : Link | undefined = undefined
72- flags : SubscriberFlags = SubscriberFlags . Dirty
69+ flags : SubscriberFlags = SubscriberFlags . Computed | SubscriberFlags . Dirty
7370
7471 /**
7572 * @internal
@@ -93,24 +90,20 @@ export class ComputedRefImpl<T = any> implements IComputed {
9390 // for backwards compat
9491 get _dirty ( ) : boolean {
9592 const flags = this . flags
96- if ( flags & SubscriberFlags . Dirty ) {
93+ if (
94+ flags & SubscriberFlags . Dirty ||
95+ ( flags & SubscriberFlags . PendingComputed &&
96+ updateDirtyFlag ( this , this . flags ) )
97+ ) {
9798 return true
98- } else if ( flags & SubscriberFlags . ToCheckDirty ) {
99- if ( checkDirty ( this . deps ! ) ) {
100- this . flags |= SubscriberFlags . Dirty
101- return true
102- } else {
103- this . flags &= ~ SubscriberFlags . ToCheckDirty
104- return false
105- }
10699 }
107100 return false
108101 }
109102 set _dirty ( v : boolean ) {
110103 if ( v ) {
111104 this . flags |= SubscriberFlags . Dirty
112105 } else {
113- this . flags &= ~ SubscriberFlags . Dirtys
106+ this . flags &= ~ ( SubscriberFlags . Dirty | SubscriberFlags . PendingComputed )
114107 }
115108 }
116109
@@ -133,23 +126,20 @@ export class ComputedRefImpl<T = any> implements IComputed {
133126 }
134127
135128 get value ( ) : T {
136- if ( this . _dirty ) {
137- this . update ( )
129+ const flags = this . flags
130+ if ( flags & ( SubscriberFlags . Dirty | SubscriberFlags . PendingComputed ) ) {
131+ processComputedUpdate ( this , flags )
138132 }
139- if ( activeTrackId !== 0 && this . lastTrackedId !== activeTrackId ) {
133+ if ( activeSub !== undefined ) {
140134 if ( __DEV__ ) {
141135 onTrack ( activeSub ! , {
142136 target : this ,
143137 type : TrackOpTypes . GET ,
144138 key : 'value' ,
145139 } )
146140 }
147- this . lastTrackedId = activeTrackId
148- link ( this , activeSub ! ) . version = this . version
149- } else if (
150- activeEffectScope !== undefined &&
151- this . lastTrackedId !== activeEffectScope . trackId
152- ) {
141+ link ( this , activeSub )
142+ } else if ( activeEffectScope !== undefined ) {
153143 link ( this , activeEffectScope )
154144 }
155145 return this . _value !
@@ -165,23 +155,20 @@ export class ComputedRefImpl<T = any> implements IComputed {
165155
166156 update ( ) : boolean {
167157 const prevSub = activeSub
168- const prevTrackId = activeTrackId
169- setActiveSub ( this , nextTrackId ( ) )
170- startTrack ( this )
171- const oldValue = this . _value
172- let newValue : T
158+ setActiveSub ( this )
159+ startTracking ( this )
173160 try {
174- newValue = this . fn ( oldValue )
161+ const oldValue = this . _value
162+ const newValue = this . fn ( oldValue )
163+ if ( hasChanged ( oldValue , newValue ) ) {
164+ this . _value = newValue
165+ return true
166+ }
167+ return false
175168 } finally {
176- setActiveSub ( prevSub , prevTrackId )
177- endTrack ( this )
169+ setActiveSub ( prevSub )
170+ endTracking ( this )
178171 }
179- if ( hasChanged ( oldValue , newValue ) ) {
180- this . _value = newValue
181- this . version ++
182- return true
183- }
184- return false
185172 }
186173}
187174
0 commit comments