fix: fix scheduler dupe invokes
This commit is contained in:
@@ -22,8 +22,6 @@ describe('scheduler', () => {
|
||||
calls.push('job1')
|
||||
// job1 queues job2
|
||||
queueJob(job2)
|
||||
// should be called sync
|
||||
expect(calls).toEqual(['job1', 'job2'])
|
||||
}
|
||||
const job2 = () => {
|
||||
calls.push('job2')
|
||||
@@ -60,8 +58,6 @@ describe('scheduler', () => {
|
||||
calls.push('job1')
|
||||
// job1 queues job2
|
||||
queueJob(job2, cb2)
|
||||
// should be called sync
|
||||
expect(calls).toEqual(['job1', 'job2'])
|
||||
}
|
||||
const job2 = () => {
|
||||
calls.push('job2')
|
||||
|
||||
@@ -2,7 +2,7 @@ const queue: Array<() => void> = []
|
||||
const postFlushCbs: Array<() => void> = []
|
||||
const p = Promise.resolve()
|
||||
|
||||
let flushing = false
|
||||
let hasPendingFlush = false
|
||||
|
||||
export function nextTick(fn?: () => void): Promise<void> {
|
||||
return p.then(fn)
|
||||
@@ -10,22 +10,18 @@ export function nextTick(fn?: () => void): Promise<void> {
|
||||
|
||||
export function queueJob(job: () => void, postFlushCb?: () => void) {
|
||||
if (queue.indexOf(job) === -1) {
|
||||
if (flushing) {
|
||||
job()
|
||||
} else {
|
||||
queue.push(job)
|
||||
queue.push(job)
|
||||
if (!hasPendingFlush) {
|
||||
hasPendingFlush = true
|
||||
nextTick(flushJobs)
|
||||
}
|
||||
}
|
||||
if (postFlushCb) {
|
||||
if (postFlushCb && postFlushCbs.indexOf(postFlushCb) === -1) {
|
||||
postFlushCbs.push(postFlushCb)
|
||||
}
|
||||
if (!flushing) {
|
||||
nextTick(flushJobs)
|
||||
}
|
||||
}
|
||||
|
||||
function flushJobs() {
|
||||
flushing = true
|
||||
let job
|
||||
while ((job = queue.shift())) {
|
||||
job()
|
||||
@@ -33,5 +29,5 @@ function flushJobs() {
|
||||
while ((job = postFlushCbs.shift())) {
|
||||
job()
|
||||
}
|
||||
flushing = false
|
||||
hasPendingFlush = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user