@@ -64,13 +64,17 @@ export function nextTick<T = void, R = void>(
64
64
return fn ? p . then ( this ? fn . bind ( this ) : fn ) : p
65
65
}
66
66
67
- // #2768
68
- // Use binary-search to find a suitable position in the queue,
69
- // so that the queue maintains the increasing order of job's id,
70
- // which can prevent the job from being skipped and also can avoid repeated patching.
67
+ // Use binary-search to find a suitable position in the queue. The queue needs
68
+ // to be sorted in increasing order of the job ids. This ensures that:
69
+ // 1. Components are updated from parent to child. As the parent is always
70
+ // created before the child it will always have a smaller id.
71
+ // 2. If a component is unmounted during a parent component's update, its update
72
+ // can be skipped.
73
+ // A pre watcher will have the same id as its component's update job. The
74
+ // watcher should be inserted immediately before the update job. This allows
75
+ // watchers to be skipped if the component is unmounted by the parent update.
71
76
function findInsertionIndex ( id : number ) {
72
- // the start index should be `flushIndex + 1`
73
- let start = flushIndex + 1
77
+ let start = isFlushing ? flushIndex + 1 : 0
74
78
let end = queue . length
75
79
76
80
while ( start < end ) {
@@ -208,33 +212,13 @@ export function flushPostFlushCbs(seen?: CountMap): void {
208
212
const getId = ( job : SchedulerJob ) : number =>
209
213
job . id == null ? ( job . flags ! & SchedulerJobFlags . PRE ? - 1 : Infinity ) : job . id
210
214
211
- const comparator = ( a : SchedulerJob , b : SchedulerJob ) : number => {
212
- const diff = getId ( a ) - getId ( b )
213
- if ( diff === 0 ) {
214
- const isAPre = a . flags ! & SchedulerJobFlags . PRE
215
- const isBPre = b . flags ! & SchedulerJobFlags . PRE
216
- if ( isAPre && ! isBPre ) return - 1
217
- if ( isBPre && ! isAPre ) return 1
218
- }
219
- return diff
220
- }
221
-
222
215
function flushJobs ( seen ?: CountMap ) {
223
216
isFlushPending = false
224
217
isFlushing = true
225
218
if ( __DEV__ ) {
226
219
seen = seen || new Map ( )
227
220
}
228
221
229
- // Sort queue before flush.
230
- // This ensures that:
231
- // 1. Components are updated from parent to child. (because parent is always
232
- // created before the child so its render effect will have smaller
233
- // priority number)
234
- // 2. If a component is unmounted during a parent component's update,
235
- // its update can be skipped.
236
- queue . sort ( comparator )
237
-
238
222
// conditional usage of checkRecursiveUpdate must be determined out of
239
223
// try ... catch block since Rollup by default de-optimizes treeshaking
240
224
// inside try-catch. This can leave all warning code unshaked. Although
0 commit comments