fix(Suspense): emit initial fallback and pending events (#3965)

Fix #3964
This commit is contained in:
Eduardo San Martin Morote
2021-06-21 23:03:07 +02:00
committed by GitHub
parent 43e2a72900
commit ab6e927041
2 changed files with 81 additions and 12 deletions

View File

@@ -94,6 +94,16 @@ export const Suspense = ((__FEATURE_SUSPENSE__
new (): { $props: VNodeProps & SuspenseProps }
}
function triggerEvent(
vnode: VNode,
name: 'onResolve' | 'onPending' | 'onFallback'
) {
const eventListener = vnode.props && vnode.props[name]
if (isFunction(eventListener)) {
eventListener()
}
}
function mountSuspense(
vnode: VNode,
container: RendererElement,
@@ -137,6 +147,10 @@ function mountSuspense(
// now check if we have encountered any async deps
if (suspense.deps > 0) {
// has async
// invoke @fallback event
triggerEvent(vnode, 'onPending')
triggerEvent(vnode, 'onFallback')
// mount the fallback tree
patch(
null,
@@ -304,10 +318,7 @@ function patchSuspense(
} else {
// root node toggled
// invoke @pending event
const onPending = n2.props && n2.props.onPending
if (isFunction(onPending)) {
onPending()
}
triggerEvent(n2, 'onPending')
// mount pending branch in off-dom container
suspense.pendingBranch = newBranch
suspense.pendingId++
@@ -501,10 +512,7 @@ function createSuspenseBoundary(
suspense.effects = []
// invoke @resolve event
const onResolve = vnode.props && vnode.props.onResolve
if (isFunction(onResolve)) {
onResolve()
}
triggerEvent(vnode, 'onResolve')
},
fallback(fallbackVNode) {
@@ -521,10 +529,7 @@ function createSuspenseBoundary(
} = suspense
// invoke @fallback event
const onFallback = vnode.props && vnode.props.onFallback
if (isFunction(onFallback)) {
onFallback()
}
triggerEvent(vnode, 'onFallback')
const anchor = next(activeBranch!)
const mountFallback = () => {