fix(runtime-core/scheduler): sort postFlushCbs to ensure refs are set before lifecycle hooks (#1854)
fix #1852
This commit is contained in:
parent
3fa5c9fdab
commit
caccec3f78
@ -7,7 +7,7 @@ import {
|
|||||||
ref,
|
ref,
|
||||||
h
|
h
|
||||||
} from '../src/index'
|
} from '../src/index'
|
||||||
import { render, nodeOps, serializeInner } from '@vue/runtime-test'
|
import { render, nodeOps, serializeInner, TestElement } from '@vue/runtime-test'
|
||||||
import {
|
import {
|
||||||
ITERATE_KEY,
|
ITERATE_KEY,
|
||||||
DebuggerEvent,
|
DebuggerEvent,
|
||||||
@ -484,6 +484,37 @@ describe('api: watch', () => {
|
|||||||
expect(calls).toEqual(['render', 'watcher 1', 'watcher 2', 'render'])
|
expect(calls).toEqual(['render', 'watcher 1', 'watcher 2', 'render'])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// #1852
|
||||||
|
it('flush: post watcher should fire after template refs updated', async () => {
|
||||||
|
const toggle = ref(false)
|
||||||
|
let dom: TestElement | null = null
|
||||||
|
|
||||||
|
const App = {
|
||||||
|
setup() {
|
||||||
|
const domRef = ref<TestElement | null>(null)
|
||||||
|
|
||||||
|
watch(
|
||||||
|
toggle,
|
||||||
|
() => {
|
||||||
|
dom = domRef.value
|
||||||
|
},
|
||||||
|
{ flush: 'post' }
|
||||||
|
)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
return toggle.value ? h('p', { ref: domRef }) : null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
render(h(App), nodeOps.createElement('div'))
|
||||||
|
expect(dom).toBe(null)
|
||||||
|
|
||||||
|
toggle.value = true
|
||||||
|
await nextTick()
|
||||||
|
expect(dom!.tag).toBe('p')
|
||||||
|
})
|
||||||
|
|
||||||
it('deep', async () => {
|
it('deep', async () => {
|
||||||
const state = reactive({
|
const state = reactive({
|
||||||
nested: {
|
nested: {
|
||||||
|
@ -403,6 +403,22 @@ describe('scheduler', () => {
|
|||||||
expect(calls).toEqual(['job3', 'job2', 'job1'])
|
expect(calls).toEqual(['job3', 'job2', 'job1'])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('sort SchedulerCbs based on id', async () => {
|
||||||
|
const calls: string[] = []
|
||||||
|
const cb1 = () => calls.push('cb1')
|
||||||
|
// cb1 has no id
|
||||||
|
const cb2 = () => calls.push('cb2')
|
||||||
|
cb2.id = 2
|
||||||
|
const cb3 = () => calls.push('cb3')
|
||||||
|
cb3.id = 1
|
||||||
|
|
||||||
|
queuePostFlushCb(cb1)
|
||||||
|
queuePostFlushCb(cb2)
|
||||||
|
queuePostFlushCb(cb3)
|
||||||
|
await nextTick()
|
||||||
|
expect(calls).toEqual(['cb3', 'cb2', 'cb1'])
|
||||||
|
})
|
||||||
|
|
||||||
// #1595
|
// #1595
|
||||||
test('avoid duplicate postFlushCb invocation', async () => {
|
test('avoid duplicate postFlushCb invocation', async () => {
|
||||||
const calls: string[] = []
|
const calls: string[] = []
|
||||||
|
@ -42,7 +42,8 @@ import {
|
|||||||
flushPostFlushCbs,
|
flushPostFlushCbs,
|
||||||
invalidateJob,
|
invalidateJob,
|
||||||
flushPreFlushCbs,
|
flushPreFlushCbs,
|
||||||
SchedulerJob
|
SchedulerJob,
|
||||||
|
SchedulerCb
|
||||||
} from './scheduler'
|
} from './scheduler'
|
||||||
import { effect, stop, ReactiveEffectOptions, isRef } from '@vue/reactivity'
|
import { effect, stop, ReactiveEffectOptions, isRef } from '@vue/reactivity'
|
||||||
import { updateProps } from './componentProps'
|
import { updateProps } from './componentProps'
|
||||||
@ -330,17 +331,20 @@ export const setRef = (
|
|||||||
// null values means this is unmount and it should not overwrite another
|
// null values means this is unmount and it should not overwrite another
|
||||||
// ref with the same key
|
// ref with the same key
|
||||||
if (value) {
|
if (value) {
|
||||||
|
;(doSet as SchedulerCb).id = -1
|
||||||
queuePostRenderEffect(doSet, parentSuspense)
|
queuePostRenderEffect(doSet, parentSuspense)
|
||||||
} else {
|
} else {
|
||||||
doSet()
|
doSet()
|
||||||
}
|
}
|
||||||
} else if (isRef(ref)) {
|
} else if (isRef(ref)) {
|
||||||
|
const doSet = () => {
|
||||||
|
ref.value = value
|
||||||
|
}
|
||||||
if (value) {
|
if (value) {
|
||||||
queuePostRenderEffect(() => {
|
;(doSet as SchedulerCb).id = -1
|
||||||
ref.value = value
|
queuePostRenderEffect(doSet, parentSuspense)
|
||||||
}, parentSuspense)
|
|
||||||
} else {
|
} else {
|
||||||
ref.value = value
|
doSet()
|
||||||
}
|
}
|
||||||
} else if (isFunction(ref)) {
|
} else if (isFunction(ref)) {
|
||||||
callWithErrorHandling(ref, parentComponent, ErrorCodes.FUNCTION_REF, [
|
callWithErrorHandling(ref, parentComponent, ErrorCodes.FUNCTION_REF, [
|
||||||
|
@ -22,18 +22,21 @@ export interface SchedulerJob {
|
|||||||
allowRecurse?: boolean
|
allowRecurse?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SchedulerCb = Function & { id?: number }
|
||||||
|
export type SchedulerCbs = SchedulerCb | SchedulerCb[]
|
||||||
|
|
||||||
let isFlushing = false
|
let isFlushing = false
|
||||||
let isFlushPending = false
|
let isFlushPending = false
|
||||||
|
|
||||||
const queue: (SchedulerJob | null)[] = []
|
const queue: (SchedulerJob | null)[] = []
|
||||||
let flushIndex = 0
|
let flushIndex = 0
|
||||||
|
|
||||||
const pendingPreFlushCbs: Function[] = []
|
const pendingPreFlushCbs: SchedulerCb[] = []
|
||||||
let activePreFlushCbs: Function[] | null = null
|
let activePreFlushCbs: SchedulerCb[] | null = null
|
||||||
let preFlushIndex = 0
|
let preFlushIndex = 0
|
||||||
|
|
||||||
const pendingPostFlushCbs: Function[] = []
|
const pendingPostFlushCbs: SchedulerCb[] = []
|
||||||
let activePostFlushCbs: Function[] | null = null
|
let activePostFlushCbs: SchedulerCb[] | null = null
|
||||||
let postFlushIndex = 0
|
let postFlushIndex = 0
|
||||||
|
|
||||||
const resolvedPromise: Promise<any> = Promise.resolve()
|
const resolvedPromise: Promise<any> = Promise.resolve()
|
||||||
@ -42,7 +45,7 @@ let currentFlushPromise: Promise<void> | null = null
|
|||||||
let currentPreFlushParentJob: SchedulerJob | null = null
|
let currentPreFlushParentJob: SchedulerJob | null = null
|
||||||
|
|
||||||
const RECURSION_LIMIT = 100
|
const RECURSION_LIMIT = 100
|
||||||
type CountMap = Map<SchedulerJob | Function, number>
|
type CountMap = Map<SchedulerJob | SchedulerCb, number>
|
||||||
|
|
||||||
export function nextTick(fn?: () => void): Promise<void> {
|
export function nextTick(fn?: () => void): Promise<void> {
|
||||||
const p = currentFlushPromise || resolvedPromise
|
const p = currentFlushPromise || resolvedPromise
|
||||||
@ -84,9 +87,9 @@ export function invalidateJob(job: SchedulerJob) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function queueCb(
|
function queueCb(
|
||||||
cb: Function | Function[],
|
cb: SchedulerCbs,
|
||||||
activeQueue: Function[] | null,
|
activeQueue: SchedulerCb[] | null,
|
||||||
pendingQueue: Function[],
|
pendingQueue: SchedulerCb[],
|
||||||
index: number
|
index: number
|
||||||
) {
|
) {
|
||||||
if (!isArray(cb)) {
|
if (!isArray(cb)) {
|
||||||
@ -108,11 +111,11 @@ function queueCb(
|
|||||||
queueFlush()
|
queueFlush()
|
||||||
}
|
}
|
||||||
|
|
||||||
export function queuePreFlushCb(cb: Function) {
|
export function queuePreFlushCb(cb: SchedulerCb) {
|
||||||
queueCb(cb, activePreFlushCbs, pendingPreFlushCbs, preFlushIndex)
|
queueCb(cb, activePreFlushCbs, pendingPreFlushCbs, preFlushIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function queuePostFlushCb(cb: Function | Function[]) {
|
export function queuePostFlushCb(cb: SchedulerCbs) {
|
||||||
queueCb(cb, activePostFlushCbs, pendingPostFlushCbs, postFlushIndex)
|
queueCb(cb, activePostFlushCbs, pendingPostFlushCbs, postFlushIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,6 +155,9 @@ export function flushPostFlushCbs(seen?: CountMap) {
|
|||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
seen = seen || new Map()
|
seen = seen || new Map()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
activePostFlushCbs.sort((a, b) => getId(a) - getId(b))
|
||||||
|
|
||||||
for (
|
for (
|
||||||
postFlushIndex = 0;
|
postFlushIndex = 0;
|
||||||
postFlushIndex < activePostFlushCbs.length;
|
postFlushIndex < activePostFlushCbs.length;
|
||||||
@ -167,7 +173,8 @@ export function flushPostFlushCbs(seen?: CountMap) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getId = (job: SchedulerJob) => (job.id == null ? Infinity : job.id)
|
const getId = (job: SchedulerJob | SchedulerCb) =>
|
||||||
|
job.id == null ? Infinity : job.id
|
||||||
|
|
||||||
function flushJobs(seen?: CountMap) {
|
function flushJobs(seen?: CountMap) {
|
||||||
isFlushPending = false
|
isFlushPending = false
|
||||||
@ -215,7 +222,7 @@ function flushJobs(seen?: CountMap) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkRecursiveUpdates(seen: CountMap, fn: SchedulerJob | Function) {
|
function checkRecursiveUpdates(seen: CountMap, fn: SchedulerJob | SchedulerCb) {
|
||||||
if (!seen.has(fn)) {
|
if (!seen.has(fn)) {
|
||||||
seen.set(fn, 1)
|
seen.set(fn, 1)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user