From: Evan You Date: Mon, 24 Aug 2020 22:46:53 +0000 (-0400) Subject: fix(runtime-core/scheduler): handle nested flushPostFlushCbs calls X-Git-Tag: v3.0.0-rc.8~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=36fa42a88cf3a72b58e507b82b35c56a42e43f09;p=thirdparty%2Fvuejs%2Fcore.git fix(runtime-core/scheduler): handle nested flushPostFlushCbs calls fix #1947 --- diff --git a/packages/runtime-core/__tests__/scheduler.spec.ts b/packages/runtime-core/__tests__/scheduler.spec.ts index 53b0890fd7..39f4de981a 100644 --- a/packages/runtime-core/__tests__/scheduler.spec.ts +++ b/packages/runtime-core/__tests__/scheduler.spec.ts @@ -4,7 +4,8 @@ import { queuePostFlushCb, invalidateJob, queuePreFlushCb, - flushPreFlushCbs + flushPreFlushCbs, + flushPostFlushCbs } from '../src/scheduler' describe('scheduler', () => { @@ -505,4 +506,24 @@ describe('scheduler', () => { await nextTick() expect(count).toBe(1) }) + + // #1947 flushPostFlushCbs should handle nested calls + // e.g. app.mount inside app.mount + test('flushPostFlushCbs', async () => { + let count = 0 + + const queueAndFlush = (hook: Function) => { + queuePostFlushCb(hook) + flushPostFlushCbs() + } + + queueAndFlush(() => { + queueAndFlush(() => { + count++ + }) + }) + + await nextTick() + expect(count).toBe(1) + }) }) diff --git a/packages/runtime-core/src/scheduler.ts b/packages/runtime-core/src/scheduler.ts index 9120c3bf17..43b88ae0e3 100644 --- a/packages/runtime-core/src/scheduler.ts +++ b/packages/runtime-core/src/scheduler.ts @@ -151,8 +151,16 @@ export function flushPreFlushCbs( export function flushPostFlushCbs(seen?: CountMap) { if (pendingPostFlushCbs.length) { - activePostFlushCbs = [...new Set(pendingPostFlushCbs)] + const deduped = [...new Set(pendingPostFlushCbs)] pendingPostFlushCbs.length = 0 + + // #1947 already has active queue, nested flushPostFlushCbs call + if (activePostFlushCbs) { + activePostFlushCbs.push(...deduped) + return + } + + activePostFlushCbs = deduped if (__DEV__) { seen = seen || new Map() } @@ -167,6 +175,7 @@ export function flushPostFlushCbs(seen?: CountMap) { if (__DEV__) { checkRecursiveUpdates(seen!, activePostFlushCbs[postFlushIndex]) } + console.log(postFlushIndex) activePostFlushCbs[postFlushIndex]() } activePostFlushCbs = null