Skip to content

Commit

Permalink
fix vuejs#12897:call newly pushed watcher orderly
Browse files Browse the repository at this point in the history
  • Loading branch information
raoht committed Dec 9, 2022
1 parent 6d9aac8 commit 71019ed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/core/observer/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Component } from 'types/component'

export const MAX_UPDATE_COUNT = 100

const queue: Array<Watcher> = []
let queue: Array<Watcher> = []
const activatedChildren: Array<Component> = []
let has: { [key: number]: true | undefined | null } = {}
let circular: { [key: number]: number } = {}
Expand Down Expand Up @@ -96,6 +96,12 @@ function flushSchedulerQueue() {
id = watcher.id
has[id] = null
watcher.run()
//when flushing,new watchers will be added to the queue and they should be resort
queue = [
...queue.slice(0, index + 1),
...queue.slice(index + 1).sort(sortCompareFn)
]

// in dev build, check and stop circular updates.
if (__DEV__ && has[id] != null) {
circular[id] = (circular[id] || 0) + 1
Expand Down
30 changes: 30 additions & 0 deletions test/unit/modules/observer/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,36 @@ describe('Scheduler', () => {
}).then(done)
})

// Github issue #12897
it('should call newly pushed watcher orderly via the flush attribute after current watcher is done', done => {
const callOrder: any[] = []
queueWatcher({
id: 1,
user: true,
run() {
callOrder.push(1)

queueWatcher({
id: 3,
run() {
callOrder.push(4)
},
post: true
})
queueWatcher({
id: 4,
run() {
callOrder.push(3)
}
}),
callOrder.push(2)
}
})
waitForUpdate(() => {
expect(callOrder).toEqual([1, 2, 3, 4])
}).then(done)
})

// GitHub issue #5191
it('emit should work when updated hook called', done => {
const el = document.createElement('div')
Expand Down

0 comments on commit 71019ed

Please sign in to comment.