Skip to content

Commit 71019ed

Browse files
author
raoht
committed
fix vuejs#12897:call newly pushed watcher orderly
1 parent 6d9aac8 commit 71019ed

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/core/observer/scheduler.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Component } from 'types/component'
88

99
export const MAX_UPDATE_COUNT = 100
1010

11-
const queue: Array<Watcher> = []
11+
let queue: Array<Watcher> = []
1212
const activatedChildren: Array<Component> = []
1313
let has: { [key: number]: true | undefined | null } = {}
1414
let circular: { [key: number]: number } = {}
@@ -96,6 +96,12 @@ function flushSchedulerQueue() {
9696
id = watcher.id
9797
has[id] = null
9898
watcher.run()
99+
//when flushing,new watchers will be added to the queue and they should be resort
100+
queue = [
101+
...queue.slice(0, index + 1),
102+
...queue.slice(index + 1).sort(sortCompareFn)
103+
]
104+
99105
// in dev build, check and stop circular updates.
100106
if (__DEV__ && has[id] != null) {
101107
circular[id] = (circular[id] || 0) + 1

test/unit/modules/observer/scheduler.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,36 @@ describe('Scheduler', () => {
151151
}).then(done)
152152
})
153153

154+
// Github issue #12897
155+
it('should call newly pushed watcher orderly via the flush attribute after current watcher is done', done => {
156+
const callOrder: any[] = []
157+
queueWatcher({
158+
id: 1,
159+
user: true,
160+
run() {
161+
callOrder.push(1)
162+
163+
queueWatcher({
164+
id: 3,
165+
run() {
166+
callOrder.push(4)
167+
},
168+
post: true
169+
})
170+
queueWatcher({
171+
id: 4,
172+
run() {
173+
callOrder.push(3)
174+
}
175+
}),
176+
callOrder.push(2)
177+
}
178+
})
179+
waitForUpdate(() => {
180+
expect(callOrder).toEqual([1, 2, 3, 4])
181+
}).then(done)
182+
})
183+
154184
// GitHub issue #5191
155185
it('emit should work when updated hook called', done => {
156186
const el = document.createElement('div')

0 commit comments

Comments
 (0)