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

fix(runtime-core): vnode hooks should not be called on async wrapper #4349

Merged
merged 1 commit into from
Aug 16, 2021
Merged
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
55 changes: 55 additions & 0 deletions packages/runtime-core/__tests__/apiAsyncComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,4 +744,59 @@ describe('api: defineAsyncComponent', () => {
expect(serializeInner(root)).toBe('<!---->')
expect(fooRef.value).toBe(null)
})

test('vnode hooks on async wrapper', async () => {
let resolve: (comp: Component) => void
const Foo = defineAsyncComponent(
() =>
new Promise(r => {
resolve = r as any
})
)
const updater = ref(0)

const vnodeHooks = {
onVnodeBeforeMount: jest.fn(),
onVnodeMounted: jest.fn(),
onVnodeBeforeUpdate: jest.fn(),
onVnodeUpdated: jest.fn(),
onVnodeBeforeUnmount: jest.fn(),
onVnodeUnmounted: jest.fn()
}

const toggle = ref(true)

const root = nodeOps.createElement('div')
createApp({
render: () => (toggle.value ? [h(Foo, vnodeHooks), updater.value] : null)
}).mount(root)

expect(serializeInner(root)).toBe('<!---->0')

resolve!({
data() {
return {
id: 'foo'
}
},
render: () => 'resolved'
})

await timeout()
expect(serializeInner(root)).toBe('resolved0')
expect(vnodeHooks.onVnodeBeforeMount).toHaveBeenCalledTimes(1)
expect(vnodeHooks.onVnodeMounted).toHaveBeenCalledTimes(1)

updater.value++
await nextTick()
expect(serializeInner(root)).toBe('resolved1')
expect(vnodeHooks.onVnodeBeforeUpdate).toHaveBeenCalledTimes(1)
expect(vnodeHooks.onVnodeUpdated).toHaveBeenCalledTimes(1)

toggle.value = false
await nextTick()
expect(serializeInner(root)).toBe('<!---->')
expect(vnodeHooks.onVnodeBeforeUnmount).toHaveBeenCalledTimes(1)
expect(vnodeHooks.onVnodeUnmounted).toHaveBeenCalledTimes(1)
})
})
25 changes: 20 additions & 5 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1317,14 +1317,18 @@ function baseCreateRenderer(
let vnodeHook: VNodeHook | null | undefined
const { el, props } = initialVNode
const { bm, m, parent } = instance
const isAsyncWrapperVNode = isAsyncWrapper(initialVNode)

effect.allowRecurse = false
// beforeMount hook
if (bm) {
invokeArrayFns(bm)
}
// onVnodeBeforeMount
if ((vnodeHook = props && props.onVnodeBeforeMount)) {
if (
!isAsyncWrapperVNode &&
(vnodeHook = props && props.onVnodeBeforeMount)
) {
invokeVNodeHook(vnodeHook, parent, initialVNode)
}
if (
Expand Down Expand Up @@ -1360,7 +1364,7 @@ function baseCreateRenderer(
}
}

if (isAsyncWrapper(initialVNode)) {
if (isAsyncWrapperVNode) {
;(initialVNode.type as ComponentOptions).__asyncLoader!().then(
// note: we are moving the render call into an async callback,
// which means it won't track dependencies - but it's ok because
Expand Down Expand Up @@ -1401,7 +1405,10 @@ function baseCreateRenderer(
queuePostRenderEffect(m, parentSuspense)
}
// onVnodeMounted
if ((vnodeHook = props && props.onVnodeMounted)) {
if (
!isAsyncWrapperVNode &&
(vnodeHook = props && props.onVnodeMounted)
) {
const scopedInitialVNode = initialVNode
queuePostRenderEffect(
() => invokeVNodeHook(vnodeHook!, parent, scopedInitialVNode),
Expand Down Expand Up @@ -2086,9 +2093,13 @@ function baseCreateRenderer(
}

const shouldInvokeDirs = shapeFlag & ShapeFlags.ELEMENT && dirs
const shouldInvokeVnodeHook = !isAsyncWrapper(vnode)

let vnodeHook: VNodeHook | undefined | null
if ((vnodeHook = props && props.onVnodeBeforeUnmount)) {
if (
shouldInvokeVnodeHook &&
(vnodeHook = props && props.onVnodeBeforeUnmount)
) {
invokeVNodeHook(vnodeHook, parentComponent, vnode)
}

Expand Down Expand Up @@ -2141,7 +2152,11 @@ function baseCreateRenderer(
}
}

if ((vnodeHook = props && props.onVnodeUnmounted) || shouldInvokeDirs) {
if (
(shouldInvokeVnodeHook &&
(vnodeHook = props && props.onVnodeUnmounted)) ||
shouldInvokeDirs
) {
queuePostRenderEffect(() => {
vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode)
shouldInvokeDirs &&
Expand Down