@@ -5,21 +5,20 @@ import {
5
5
type DebuggerEvent ,
6
6
type DebuggerOptions ,
7
7
activeSub ,
8
- activeTrackId ,
9
- nextTrackId ,
10
8
setActiveSub ,
11
9
} from './effect'
12
10
import { activeEffectScope } from './effectScope'
13
11
import type { Ref } from './ref'
14
12
import {
15
13
type Dependency ,
16
- type IComputed ,
17
14
type Link ,
15
+ type Subscriber ,
18
16
SubscriberFlags ,
19
- checkDirty ,
20
- endTrack ,
17
+ endTracking ,
21
18
link ,
22
- startTrack ,
19
+ processComputedUpdate ,
20
+ startTracking ,
21
+ updateDirtyFlag ,
23
22
} from './system'
24
23
import { warn } from './warning'
25
24
@@ -54,22 +53,20 @@ export interface WritableComputedOptions<T, S = T> {
54
53
* @private exported by @vue/reactivity for Vue core use, but not exported from
55
54
* the main vue package
56
55
*/
57
- export class ComputedRefImpl < T = any > implements IComputed {
56
+ export class ComputedRefImpl < T = any > implements Dependency , Subscriber {
58
57
/**
59
58
* @internal
60
59
*/
61
60
_value : T | undefined = undefined
62
- version = 0
63
61
64
62
// Dependency
65
63
subs : Link | undefined = undefined
66
64
subsTail : Link | undefined = undefined
67
- lastTrackedId = 0
68
65
69
66
// Subscriber
70
67
deps : Link | undefined = undefined
71
68
depsTail : Link | undefined = undefined
72
- flags : SubscriberFlags = SubscriberFlags . Dirty
69
+ flags : SubscriberFlags = SubscriberFlags . Computed | SubscriberFlags . Dirty
73
70
74
71
/**
75
72
* @internal
@@ -93,24 +90,20 @@ export class ComputedRefImpl<T = any> implements IComputed {
93
90
// for backwards compat
94
91
get _dirty ( ) : boolean {
95
92
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
+ ) {
97
98
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
- }
106
99
}
107
100
return false
108
101
}
109
102
set _dirty ( v : boolean ) {
110
103
if ( v ) {
111
104
this . flags |= SubscriberFlags . Dirty
112
105
} else {
113
- this . flags &= ~ SubscriberFlags . Dirtys
106
+ this . flags &= ~ ( SubscriberFlags . Dirty | SubscriberFlags . PendingComputed )
114
107
}
115
108
}
116
109
@@ -133,23 +126,20 @@ export class ComputedRefImpl<T = any> implements IComputed {
133
126
}
134
127
135
128
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 )
138
132
}
139
- if ( activeTrackId !== 0 && this . lastTrackedId !== activeTrackId ) {
133
+ if ( activeSub !== undefined ) {
140
134
if ( __DEV__ ) {
141
135
onTrack ( activeSub ! , {
142
136
target : this ,
143
137
type : TrackOpTypes . GET ,
144
138
key : 'value' ,
145
139
} )
146
140
}
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 ) {
153
143
link ( this , activeEffectScope )
154
144
}
155
145
return this . _value !
@@ -165,23 +155,20 @@ export class ComputedRefImpl<T = any> implements IComputed {
165
155
166
156
update ( ) : boolean {
167
157
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 )
173
160
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
175
168
} finally {
176
- setActiveSub ( prevSub , prevTrackId )
177
- endTrack ( this )
169
+ setActiveSub ( prevSub )
170
+ endTracking ( this )
178
171
}
179
- if ( hasChanged ( oldValue , newValue ) ) {
180
- this . _value = newValue
181
- this . version ++
182
- return true
183
- }
184
- return false
185
172
}
186
173
}
187
174
0 commit comments