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

feat(scheduler): supports nextTick execution on queuejob first. #12010

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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 @@ -790,4 +790,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)
})
})
11 changes: 10 additions & 1 deletion packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ 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 && fn) {
wrapperFn = function () {
if (currentFlushPromise) {
return resolvedPromise.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