From 511aebdeccf9b1cd924dbb306a011e3523037cfe Mon Sep 17 00:00:00 2001 From: yangchangtao Date: Mon, 23 Sep 2024 19:16:57 +0800 Subject: [PATCH 1/5] feat(scheduler): supports nextick execution on queuejob first. --- .../runtime-core/__tests__/scheduler.spec.ts | 23 +++++++++++++++++++ packages/runtime-core/src/scheduler.ts | 12 +++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/scheduler.spec.ts b/packages/runtime-core/__tests__/scheduler.spec.ts index 5c5b04673ab..e85e0942ae6 100644 --- a/packages/runtime-core/__tests__/scheduler.spec.ts +++ b/packages/runtime-core/__tests__/scheduler.spec.ts @@ -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(r => { + nextTick(() => { + val = 2 + r() + }) + queueJob(() => { + val = 3 + }) + }) + await p + expect(val).toBe(2) + }) }) diff --git a/packages/runtime-core/src/scheduler.ts b/packages/runtime-core/src/scheduler.ts index 1eac06e5bf0..5a57aa30e2b 100644 --- a/packages/runtime-core/src/scheduler.ts +++ b/packages/runtime-core/src/scheduler.ts @@ -61,7 +61,17 @@ export function nextTick( fn?: (this: T) => R, ): Promise> { const p = currentFlushPromise || resolvedPromise - return fn ? p.then(this ? fn.bind(this) : fn) : p + let wrapperFn: ((this: T) => R | Promise | 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 From 1e7c3ed0e8acf583c0d026d74feca743baba0ba7 Mon Sep 17 00:00:00 2001 From: yangchangtao Date: Mon, 23 Sep 2024 19:20:23 +0800 Subject: [PATCH 2/5] chore: update --- packages/runtime-core/src/scheduler.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/runtime-core/src/scheduler.ts b/packages/runtime-core/src/scheduler.ts index 5a57aa30e2b..931bb143cfb 100644 --- a/packages/runtime-core/src/scheduler.ts +++ b/packages/runtime-core/src/scheduler.ts @@ -63,13 +63,14 @@ export function nextTick( const p = currentFlushPromise || resolvedPromise let wrapperFn: ((this: T) => R | Promise | undefined) | undefined = fn if (!currentFlushPromise) { - wrapperFn = function () { - if (!fn) return - if (currentFlushPromise) { - return currentFlushPromise.then(fn.bind(this)) - } - return fn.call(this) - } + wrapperFn = fn + ? function () { + if (currentFlushPromise) { + return currentFlushPromise.then(fn.bind(this)) + } + return fn.call(this) + } + : void 0 } return wrapperFn ? p.then(this ? wrapperFn.bind(this) : wrapperFn) : p } From 620c8c61b3e02d7da3ed7ed41037b09293a512ea Mon Sep 17 00:00:00 2001 From: yangchangtao Date: Tue, 24 Sep 2024 11:47:01 +0800 Subject: [PATCH 3/5] chore: wip --- packages/runtime-core/src/scheduler.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/runtime-core/src/scheduler.ts b/packages/runtime-core/src/scheduler.ts index d9d3f22b781..344158aa92d 100644 --- a/packages/runtime-core/src/scheduler.ts +++ b/packages/runtime-core/src/scheduler.ts @@ -62,17 +62,15 @@ export function nextTick( ): Promise> { const p = currentFlushPromise || resolvedPromise let wrapperFn: ((this: T) => R | Promise | undefined) | undefined = fn - if (!currentFlushPromise) { - wrapperFn = fn - ? function () { - if (currentFlushPromise) { - return currentFlushPromise.then(fn.bind(this)) - } - return fn.call(this) - } - : void 0 + if (!currentFlushPromise && fn) { + wrapperFn = () => { + if (currentFlushPromise) { + return currentFlushPromise.then(this ? fn.bind(this) : fn) + } + return fn.call(this) + } } - return wrapperFn ? p.then(this ? wrapperFn.bind(this) : wrapperFn) : p + return wrapperFn ? p.then(wrapperFn) : p } // Use binary-search to find a suitable position in the queue. The queue needs From e3c479786ffc8873fac22add4f772be5292d6f4a Mon Sep 17 00:00:00 2001 From: yangchangtao Date: Tue, 24 Sep 2024 12:02:56 +0800 Subject: [PATCH 4/5] chore: update --- packages/runtime-core/src/scheduler.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/src/scheduler.ts b/packages/runtime-core/src/scheduler.ts index 344158aa92d..c6db1c50384 100644 --- a/packages/runtime-core/src/scheduler.ts +++ b/packages/runtime-core/src/scheduler.ts @@ -63,14 +63,14 @@ export function nextTick( const p = currentFlushPromise || resolvedPromise let wrapperFn: ((this: T) => R | Promise | undefined) | undefined = fn if (!currentFlushPromise && fn) { - wrapperFn = () => { + wrapperFn = function () { if (currentFlushPromise) { - return currentFlushPromise.then(this ? fn.bind(this) : fn) + return currentFlushPromise.then(fn.bind(this)) } return fn.call(this) } } - return wrapperFn ? p.then(wrapperFn) : p + return wrapperFn ? p.then(this ? wrapperFn.bind(this) : wrapperFn) : p } // Use binary-search to find a suitable position in the queue. The queue needs From a7928d0a6f0346e0a1511be409db7d45c0e6f4db Mon Sep 17 00:00:00 2001 From: yangchangtao Date: Tue, 24 Sep 2024 14:27:52 +0800 Subject: [PATCH 5/5] chore: wip --- packages/runtime-core/src/scheduler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime-core/src/scheduler.ts b/packages/runtime-core/src/scheduler.ts index c6db1c50384..2702886f26d 100644 --- a/packages/runtime-core/src/scheduler.ts +++ b/packages/runtime-core/src/scheduler.ts @@ -65,7 +65,7 @@ export function nextTick( if (!currentFlushPromise && fn) { wrapperFn = function () { if (currentFlushPromise) { - return currentFlushPromise.then(fn.bind(this)) + return resolvedPromise.then(fn.bind(this)) } return fn.call(this) }