Skip to content

Commit

Permalink
feat(scheduler): supports nextick execution on queuejob first.
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchangtao committed Sep 23, 2024
1 parent 9a36f2a commit 511aebd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/runtime-core/__tests__/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,27 @@ describe('scheduler', () => {
await nextTick()
expect(calls).toEqual(['cb2', 'cb1'])
})

test('nextick callback should be executed after the queuejob', async () => {
let val = 1
queueJob(() => {
val = 3
})
await nextTick(() => {
val = 2
})
expect(val).toBe(2)

const p = new Promise<void>(r => {
nextTick(() => {
val = 2
r()
})
queueJob(() => {
val = 3
})
})
await p
expect(val).toBe(2)
})
})
12 changes: 11 additions & 1 deletion packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ export function nextTick<T = void, R = void>(
fn?: (this: T) => R,
): Promise<Awaited<R>> {
const p = currentFlushPromise || resolvedPromise
return fn ? p.then(this ? fn.bind(this) : fn) : p
let wrapperFn: ((this: T) => R | Promise<R> | undefined) | undefined = fn
if (!currentFlushPromise) {
wrapperFn = function () {
if (!fn) return
if (currentFlushPromise) {
return currentFlushPromise.then(fn.bind(this))
}
return fn.call(this)
}
}
return wrapperFn ? p.then(this ? wrapperFn.bind(this) : wrapperFn) : p
}

// Use binary-search to find a suitable position in the queue. The queue needs
Expand Down

0 comments on commit 511aebd

Please sign in to comment.