@@ -27,12 +27,23 @@ export class Dep {
2727 * Link between this dep and the current active effect
2828 */
2929 activeLink ?: Link = undefined
30+
3031 /**
3132 * Doubly linked list representing the subscribing effects (tail)
3233 */
3334 subs ?: Link = undefined
3435
35- constructor ( public computed ?: ComputedRefImpl ) { }
36+ /**
37+ * Doubly linked list representing the subscribing effects (head)
38+ * DEV only, for invoking onTrigger hooks in correct order
39+ */
40+ subsHead ?: Link
41+
42+ constructor ( public computed ?: ComputedRefImpl ) {
43+ if ( __DEV__ ) {
44+ this . subsHead = undefined
45+ }
46+ }
3647
3748 track ( debugInfo ?: DebuggerEventExtraInfo ) : Link | undefined {
3849 if ( ! activeSub || ! shouldTrack ) {
@@ -113,21 +124,28 @@ export class Dep {
113124 notify ( debugInfo ?: DebuggerEventExtraInfo ) {
114125 startBatch ( )
115126 try {
116- for ( let link = this . subs ; link ; link = link . prevSub ) {
117- if (
118- __DEV__ &&
119- link . sub . onTrigger &&
120- ! ( link . sub . flags & EffectFlags . NOTIFIED )
121- ) {
122- link . sub . onTrigger (
123- extend (
124- {
125- effect : link . sub ,
126- } ,
127- debugInfo ,
128- ) ,
129- )
127+ if ( __DEV__ ) {
128+ // subs are notified and batched in reverse-order and then invoked in
129+ // original order at the end of the batch, but onTrigger hooks should
130+ // be invoked in original order here.
131+ for ( let head = this . subsHead ; head ; head = head . nextSub ) {
132+ if (
133+ __DEV__ &&
134+ head . sub . onTrigger &&
135+ ! ( head . sub . flags & EffectFlags . NOTIFIED )
136+ ) {
137+ head . sub . onTrigger (
138+ extend (
139+ {
140+ effect : head . sub ,
141+ } ,
142+ debugInfo ,
143+ ) ,
144+ )
145+ }
130146 }
147+ }
148+ for ( let link = this . subs ; link ; link = link . prevSub ) {
131149 link . sub . notify ( )
132150 }
133151 } finally {
@@ -152,6 +170,11 @@ function addSub(link: Link) {
152170 link . prevSub = currentTail
153171 if ( currentTail ) currentTail . nextSub = link
154172 }
173+
174+ if ( __DEV__ && link . dep . subsHead === undefined ) {
175+ link . dep . subsHead = link
176+ }
177+
155178 link . dep . subs = link
156179}
157180
0 commit comments