Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scheduler): prevent duplicate jobs being queued #11826

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ export function queueJob(job: SchedulerJob): void {
queue.splice(findInsertionIndex(jobId), 0, job)
}

if (!(job.flags! & SchedulerJobFlags.ALLOW_RECURSE)) {
job.flags! |= SchedulerJobFlags.QUEUED
}
job.flags! |= SchedulerJobFlags.QUEUED

queueFlush()
}
}
Expand All @@ -128,9 +127,7 @@ export function queuePostFlushCb(cb: SchedulerJobs): void {
activePostFlushCbs.splice(postFlushIndex + 1, 0, cb)
} else if (!(cb.flags! & SchedulerJobFlags.QUEUED)) {
pendingPostFlushCbs.push(cb)
if (!(cb.flags! & SchedulerJobFlags.ALLOW_RECURSE)) {
cb.flags! |= SchedulerJobFlags.QUEUED
}
cb.flags! |= SchedulerJobFlags.QUEUED
}
} else {
// if cb is an array, it is a component lifecycle hook which can only be
Expand Down Expand Up @@ -161,6 +158,9 @@ export function flushPreFlushCbs(
}
queue.splice(i, 1)
i--
if (cb.flags! & SchedulerJobFlags.ALLOW_RECURSE) {
cb.flags! &= ~SchedulerJobFlags.QUEUED
}
cb()
cb.flags! &= ~SchedulerJobFlags.QUEUED
}
Expand Down Expand Up @@ -194,6 +194,9 @@ export function flushPostFlushCbs(seen?: CountMap): void {
if (__DEV__ && checkRecursiveUpdates(seen!, cb)) {
continue
}
if (cb.flags! & SchedulerJobFlags.ALLOW_RECURSE) {
cb.flags! &= ~SchedulerJobFlags.QUEUED
}
if (!(cb.flags! & SchedulerJobFlags.DISPOSED)) cb()
cb.flags! &= ~SchedulerJobFlags.QUEUED
}
Expand Down Expand Up @@ -228,6 +231,9 @@ function flushJobs(seen?: CountMap) {
if (__DEV__ && check(job)) {
continue
}
if (job.flags! & SchedulerJobFlags.ALLOW_RECURSE) {
job.flags! &= ~SchedulerJobFlags.QUEUED
}
callWithErrorHandling(
job,
job.i,
Expand All @@ -237,6 +243,14 @@ function flushJobs(seen?: CountMap) {
}
}
} finally {
// If there was an error we still need to clear the QUEUED flags
for (; flushIndex < queue.length; flushIndex++) {
const job = queue[flushIndex]
if (job) {
job.flags! &= ~SchedulerJobFlags.QUEUED
}
}

flushIndex = 0
queue.length = 0

Expand Down