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(suspense): emit initial fallback and pending #3965

Merged
merged 1 commit into from
Jun 21, 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
64 changes: 64 additions & 0 deletions packages/runtime-core/__tests__/components/Suspense.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,70 @@ describe('Suspense', () => {
expect(serializeInner(root)).toBe(`<div>async</div>`)
})

test('emits events', async () => {
const Async = defineAsyncComponent({
render() {
return h('div', 'async')
}
})

const onFallback = jest.fn()
const onResolve = jest.fn()
const onPending = jest.fn()

const show = ref(true)
const Comp = {
setup() {
return () =>
h(
Suspense,
{
onFallback,
onResolve,
onPending,
// force displaying the fallback right away
timeout: 0
},
{
default: () => (show.value ? h(Async) : null),
fallback: h('div', 'fallback')
}
)
}
}

const root = nodeOps.createElement('div')
render(h(Comp), root)
expect(onFallback).toHaveBeenCalledTimes(1)
expect(onPending).toHaveBeenCalledTimes(1)
expect(onResolve).toHaveBeenCalledTimes(0)

await Promise.all(deps)
await nextTick()
expect(onFallback).toHaveBeenCalledTimes(1)
expect(onPending).toHaveBeenCalledTimes(1)
expect(onResolve).toHaveBeenCalledTimes(1)

show.value = false
await nextTick()
expect(onFallback).toHaveBeenCalledTimes(1)
expect(onPending).toHaveBeenCalledTimes(2)
expect(onResolve).toHaveBeenCalledTimes(2)

deps.length = 0
show.value = true
await nextTick()
expect(onFallback).toHaveBeenCalledTimes(2)
expect(onPending).toHaveBeenCalledTimes(3)
expect(onResolve).toHaveBeenCalledTimes(2)

await Promise.all(deps)
await nextTick()
expect(onFallback).toHaveBeenCalledTimes(2)
expect(onPending).toHaveBeenCalledTimes(3)
expect(onResolve).toHaveBeenCalledTimes(3)
})

test('nested async deps', async () => {
const calls: string[] = []

Expand Down
29 changes: 17 additions & 12 deletions packages/runtime-core/src/components/Suspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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++
Expand Down Expand Up @@ -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) {
Expand All @@ -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 = () => {
Expand Down