File tree Expand file tree Collapse file tree 4 files changed +46
-2
lines changed Expand file tree Collapse file tree 4 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,15 @@ if (inBrowser && !isIE) {
5959 }
6060}
6161
62+ const sortCompareFn = ( a : Watcher , b : Watcher ) : number => {
63+ if ( a . post ) {
64+ if ( ! b . post ) return 1
65+ } else if ( b . post ) {
66+ return - 1
67+ }
68+ return a . id - b . id
69+ }
70+
6271/**
6372 * Flush both queues and run the watchers.
6473 */
@@ -75,7 +84,7 @@ function flushSchedulerQueue() {
7584 // user watchers are created before the render watcher)
7685 // 3. If a component is destroyed during a parent component's watcher run,
7786 // its watchers can be skipped.
78- queue . sort ( ( a , b ) => a . id - b . id )
87+ queue . sort ( sortCompareFn )
7988
8089 // do not cache length because more watchers might be pushed
8190 // as we run existing watchers
Original file line number Diff line number Diff line change @@ -58,6 +58,7 @@ export default class Watcher implements DepTarget {
5858 noRecurse ?: boolean
5959 getter : Function
6060 value : any
61+ post : boolean
6162
6263 // dev only
6364 onTrack ?: ( ( event : DebuggerEvent ) => void ) | undefined
@@ -93,6 +94,7 @@ export default class Watcher implements DepTarget {
9394 this . cb = cb
9495 this . id = ++ uid // uid for batching
9596 this . active = true
97+ this . post = false
9698 this . dirty = this . lazy // for lazy watchers
9799 this . deps = [ ]
98100 this . newDeps = [ ]
Original file line number Diff line number Diff line change @@ -313,7 +313,7 @@ function doWatch(
313313 if ( flush === 'sync' ) {
314314 watcher . update = watcher . run
315315 } else if ( flush === 'post' ) {
316- watcher . id = Infinity
316+ watcher . post = true
317317 watcher . update = ( ) => queueWatcher ( watcher )
318318 } else {
319319 // pre
Original file line number Diff line number Diff line change @@ -1167,4 +1167,37 @@ describe('api: watch', () => {
11671167 set ( r . value , 'foo' , 1 )
11681168 expect ( spy ) . not . toHaveBeenCalled ( )
11691169 } )
1170+
1171+ // #12664
1172+ it ( 'queueing multiple flush: post watchers' , async ( ) => {
1173+ const parentSpy = vi . fn ( )
1174+ const childSpy = vi . fn ( )
1175+
1176+ const Child = {
1177+ setup ( ) {
1178+ const el = ref ( )
1179+ watch ( el , childSpy , { flush : 'post' } )
1180+ return { el }
1181+ } ,
1182+ template : `<div><span ref="el">hello child</span></div>`
1183+ }
1184+ const App = {
1185+ components : { Child } ,
1186+ setup ( ) {
1187+ const el = ref ( )
1188+ watch ( el , parentSpy , { flush : 'post' } )
1189+ return { el }
1190+ } ,
1191+ template : `<div><Child /><span ref="el">hello app1</span></div>`
1192+ }
1193+
1194+ const container = document . createElement ( 'div' )
1195+ const root = document . createElement ( 'div' )
1196+ container . appendChild ( root )
1197+ new Vue ( App ) . $mount ( root )
1198+
1199+ await nextTick ( )
1200+ expect ( parentSpy ) . toHaveBeenCalledTimes ( 1 )
1201+ expect ( childSpy ) . toHaveBeenCalledTimes ( 1 )
1202+ } )
11701203} )
You can’t perform that action at this time.
0 commit comments