@@ -27,6 +27,7 @@ let flushIndex = 0
27
27
let pendingPostFlushCbs : Function [ ] | null = null
28
28
let pendingPostFlushIndex = 0
29
29
let hasPendingPreFlushJobs = false
30
+ let currentPreFlushParentJob : SchedulerJob | null = null
30
31
31
32
const RECURSION_LIMIT = 100
32
33
type CountMap = Map < SchedulerJob | Function , number >
@@ -44,9 +45,16 @@ export function queueJob(job: SchedulerJob) {
44
45
// allow it recursively trigger itself - it is the user's responsibility to
45
46
// ensure it doesn't end up in an infinite loop.
46
47
if (
47
- ! queue . length ||
48
- ! queue . includes ( job , isFlushing && job . cb ? flushIndex + 1 : flushIndex )
48
+ ( ! queue . length ||
49
+ ! queue . includes (
50
+ job ,
51
+ isFlushing && job . cb ? flushIndex + 1 : flushIndex
52
+ ) ) &&
53
+ job !== currentPreFlushParentJob
49
54
) {
55
+ if ( job . id && job . id > 0 ) {
56
+ debugger
57
+ }
50
58
queue . push ( job )
51
59
if ( ( job . id as number ) < 0 ) hasPendingPreFlushJobs = true
52
60
queueFlush ( )
@@ -60,16 +68,27 @@ export function invalidateJob(job: SchedulerJob) {
60
68
}
61
69
}
62
70
63
- export function runPreflushJobs ( ) {
71
+ /**
72
+ * Run flush: 'pre' watcher callbacks. This is only called in
73
+ * `updateComponentPreRender` to cover the case where pre-flush watchers are
74
+ * triggered by the change of a component's props. This means the scheduler is
75
+ * already flushing and we are already inside the component's update effect,
76
+ * right when the render function is about to be called. So if the watcher
77
+ * triggers the same component to update, we don't want it to be queued (this
78
+ * is checked via `currentPreFlushParentJob`).
79
+ */
80
+ export function runPreflushJobs ( parentJob : SchedulerJob ) {
64
81
if ( hasPendingPreFlushJobs ) {
82
+ currentPreFlushParentJob = parentJob
65
83
hasPendingPreFlushJobs = false
66
- for ( let job , i = queue . length - 1 ; i > flushIndex ; i -- ) {
84
+ for ( let job , i = flushIndex + 1 ; i < queue . length ; i ++ ) {
67
85
job = queue [ i ]
68
86
if ( job && ( job . id as number ) < 0 ) {
69
87
job ( )
70
88
queue [ i ] = null
71
89
}
72
90
}
91
+ currentPreFlushParentJob = null
73
92
}
74
93
}
75
94
0 commit comments