2020-09-15 16:45:06 +00:00
|
|
|
import {
|
|
|
|
VNode,
|
|
|
|
normalizeVNode,
|
|
|
|
VNodeChild,
|
|
|
|
VNodeProps,
|
|
|
|
isSameVNodeType
|
|
|
|
} from '../vnode'
|
|
|
|
import { isFunction, isArray, ShapeFlags, toNumber } from '@vue/shared'
|
2019-11-04 23:38:55 +00:00
|
|
|
import { ComponentInternalInstance, handleSetupResult } from '../component'
|
|
|
|
import { Slots } from '../componentSlots'
|
2020-03-23 15:08:22 +00:00
|
|
|
import {
|
|
|
|
RendererInternals,
|
|
|
|
MoveType,
|
|
|
|
SetupRenderEffectFn,
|
|
|
|
RendererNode,
|
|
|
|
RendererElement
|
|
|
|
} from '../renderer'
|
2020-09-15 16:45:06 +00:00
|
|
|
import { queuePostFlushCb } from '../scheduler'
|
|
|
|
import { filterSingleRoot, updateHOCHostEl } from '../componentRenderUtils'
|
|
|
|
import { pushWarningContext, popWarningContext, warn } from '../warning'
|
2020-03-13 02:19:41 +00:00
|
|
|
import { handleError, ErrorCodes } from '../errorHandling'
|
2019-09-07 15:28:40 +00:00
|
|
|
|
2019-11-04 23:38:55 +00:00
|
|
|
export interface SuspenseProps {
|
|
|
|
onResolve?: () => void
|
2020-09-15 16:45:06 +00:00
|
|
|
onPending?: () => void
|
|
|
|
onFallback?: () => void
|
|
|
|
timeout?: string | number
|
2019-11-04 23:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 16:40:09 +00:00
|
|
|
export const isSuspense = (type: any): boolean => type.__isSuspense
|
|
|
|
|
2019-11-04 23:38:55 +00:00
|
|
|
// Suspense exposes a component-like API, and is treated like a component
|
|
|
|
// in the compiler, but internally it's a special built-in type that hooks
|
|
|
|
// directly into the renderer.
|
|
|
|
export const SuspenseImpl = {
|
|
|
|
// In order to make Suspense tree-shakable, we need to avoid importing it
|
|
|
|
// directly in the renderer. The renderer checks for the __isSuspense flag
|
|
|
|
// on a vnode's type and calls the `process` method, passing in renderer
|
|
|
|
// internals.
|
2019-11-01 16:43:27 +00:00
|
|
|
__isSuspense: true,
|
2019-10-29 16:30:09 +00:00
|
|
|
process(
|
|
|
|
n1: VNode | null,
|
|
|
|
n2: VNode,
|
2020-03-23 15:08:22 +00:00
|
|
|
container: RendererElement,
|
|
|
|
anchor: RendererNode | null,
|
2019-10-29 16:30:09 +00:00
|
|
|
parentComponent: ComponentInternalInstance | null,
|
|
|
|
parentSuspense: SuspenseBoundary | null,
|
|
|
|
isSVG: boolean,
|
|
|
|
optimized: boolean,
|
|
|
|
// platform-specific impl passed from renderer
|
|
|
|
rendererInternals: RendererInternals
|
|
|
|
) {
|
|
|
|
if (n1 == null) {
|
|
|
|
mountSuspense(
|
|
|
|
n2,
|
|
|
|
container,
|
|
|
|
anchor,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
isSVG,
|
|
|
|
optimized,
|
|
|
|
rendererInternals
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
patchSuspense(
|
|
|
|
n1,
|
|
|
|
n2,
|
|
|
|
container,
|
|
|
|
anchor,
|
|
|
|
parentComponent,
|
|
|
|
isSVG,
|
|
|
|
optimized,
|
|
|
|
rendererInternals
|
|
|
|
)
|
|
|
|
}
|
2020-03-13 02:19:41 +00:00
|
|
|
},
|
2020-09-15 16:45:06 +00:00
|
|
|
hydrate: hydrateSuspense,
|
|
|
|
create: createSuspenseBoundary
|
2019-10-29 16:30:09 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 23:38:55 +00:00
|
|
|
// Force-casted public typing for h and TSX props inference
|
|
|
|
export const Suspense = ((__FEATURE_SUSPENSE__
|
|
|
|
? SuspenseImpl
|
|
|
|
: null) as any) as {
|
|
|
|
__isSuspense: true
|
2020-02-16 02:48:45 +00:00
|
|
|
new (): { $props: VNodeProps & SuspenseProps }
|
2019-11-04 23:38:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 16:30:09 +00:00
|
|
|
function mountSuspense(
|
2020-09-15 16:45:06 +00:00
|
|
|
vnode: VNode,
|
2020-03-23 15:08:22 +00:00
|
|
|
container: RendererElement,
|
|
|
|
anchor: RendererNode | null,
|
2019-10-29 16:30:09 +00:00
|
|
|
parentComponent: ComponentInternalInstance | null,
|
|
|
|
parentSuspense: SuspenseBoundary | null,
|
|
|
|
isSVG: boolean,
|
|
|
|
optimized: boolean,
|
|
|
|
rendererInternals: RendererInternals
|
|
|
|
) {
|
|
|
|
const {
|
2020-02-15 16:40:09 +00:00
|
|
|
p: patch,
|
|
|
|
o: { createElement }
|
2019-10-29 16:30:09 +00:00
|
|
|
} = rendererInternals
|
|
|
|
const hiddenContainer = createElement('div')
|
2020-09-15 16:45:06 +00:00
|
|
|
const suspense = (vnode.suspense = createSuspenseBoundary(
|
|
|
|
vnode,
|
2019-10-29 16:30:09 +00:00
|
|
|
parentSuspense,
|
|
|
|
parentComponent,
|
|
|
|
container,
|
|
|
|
hiddenContainer,
|
|
|
|
anchor,
|
|
|
|
isSVG,
|
|
|
|
optimized,
|
|
|
|
rendererInternals
|
|
|
|
))
|
|
|
|
|
|
|
|
// start mounting the content subtree in an off-dom container
|
|
|
|
patch(
|
|
|
|
null,
|
2020-09-15 16:45:06 +00:00
|
|
|
(suspense.pendingBranch = vnode.ssContent!),
|
2019-10-29 16:30:09 +00:00
|
|
|
hiddenContainer,
|
|
|
|
null,
|
|
|
|
parentComponent,
|
|
|
|
suspense,
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
|
|
|
// now check if we have encountered any async deps
|
|
|
|
if (suspense.deps > 0) {
|
2020-09-15 16:45:06 +00:00
|
|
|
// has async
|
2019-10-29 16:30:09 +00:00
|
|
|
// mount the fallback tree
|
|
|
|
patch(
|
|
|
|
null,
|
2020-09-15 16:45:06 +00:00
|
|
|
vnode.ssFallback!,
|
2019-10-29 16:30:09 +00:00
|
|
|
container,
|
|
|
|
anchor,
|
|
|
|
parentComponent,
|
|
|
|
null, // fallback tree will not have suspense context
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
2020-09-15 16:45:06 +00:00
|
|
|
setActiveBranch(suspense, vnode.ssFallback!)
|
2019-10-29 16:30:09 +00:00
|
|
|
} else {
|
|
|
|
// Suspense has no async deps. Just resolve.
|
|
|
|
suspense.resolve()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function patchSuspense(
|
|
|
|
n1: VNode,
|
|
|
|
n2: VNode,
|
2020-03-23 15:08:22 +00:00
|
|
|
container: RendererElement,
|
|
|
|
anchor: RendererNode | null,
|
2019-10-29 16:30:09 +00:00
|
|
|
parentComponent: ComponentInternalInstance | null,
|
|
|
|
isSVG: boolean,
|
|
|
|
optimized: boolean,
|
2020-09-15 16:45:06 +00:00
|
|
|
{ p: patch, um: unmount, o: { createElement } }: RendererInternals
|
2019-10-29 16:30:09 +00:00
|
|
|
) {
|
|
|
|
const suspense = (n2.suspense = n1.suspense)!
|
|
|
|
suspense.vnode = n2
|
2020-09-15 16:45:06 +00:00
|
|
|
n2.el = n1.el
|
|
|
|
const newBranch = n2.ssContent!
|
|
|
|
const newFallback = n2.ssFallback!
|
|
|
|
|
|
|
|
const { activeBranch, pendingBranch, isInFallback, isHydrating } = suspense
|
|
|
|
if (pendingBranch) {
|
|
|
|
suspense.pendingBranch = newBranch
|
|
|
|
if (isSameVNodeType(newBranch, pendingBranch)) {
|
|
|
|
// same root type but content may have changed.
|
2019-10-29 16:30:09 +00:00
|
|
|
patch(
|
2020-09-15 16:45:06 +00:00
|
|
|
pendingBranch,
|
|
|
|
newBranch,
|
|
|
|
suspense.hiddenContainer,
|
|
|
|
null,
|
|
|
|
parentComponent,
|
|
|
|
suspense,
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
|
|
|
if (suspense.deps <= 0) {
|
|
|
|
suspense.resolve()
|
|
|
|
} else if (isInFallback) {
|
|
|
|
patch(
|
|
|
|
activeBranch,
|
|
|
|
newFallback,
|
|
|
|
container,
|
|
|
|
anchor,
|
|
|
|
parentComponent,
|
|
|
|
null, // fallback tree will not have suspense context
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
|
|
|
setActiveBranch(suspense, newFallback)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// toggled before pending tree is resolved
|
|
|
|
suspense.pendingId++
|
|
|
|
if (isHydrating) {
|
|
|
|
// if toggled before hydration is finished, the current DOM tree is
|
|
|
|
// no longer valid. set it as the active branch so it will be unmounted
|
|
|
|
// when resolved
|
|
|
|
suspense.isHydrating = false
|
|
|
|
suspense.activeBranch = pendingBranch
|
|
|
|
} else {
|
|
|
|
unmount(pendingBranch, parentComponent, null)
|
|
|
|
}
|
|
|
|
// increment pending ID. this is used to invalidate async callbacks
|
|
|
|
// reset suspense state
|
|
|
|
suspense.deps = 0
|
|
|
|
suspense.effects.length = 0
|
|
|
|
// discard previous container
|
|
|
|
suspense.hiddenContainer = createElement('div')
|
|
|
|
|
|
|
|
if (isInFallback) {
|
|
|
|
// already in fallback state
|
|
|
|
patch(
|
|
|
|
null,
|
|
|
|
newBranch,
|
|
|
|
suspense.hiddenContainer,
|
|
|
|
null,
|
|
|
|
parentComponent,
|
|
|
|
suspense,
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
|
|
|
if (suspense.deps <= 0) {
|
|
|
|
suspense.resolve()
|
|
|
|
} else {
|
|
|
|
patch(
|
|
|
|
activeBranch,
|
|
|
|
newFallback,
|
|
|
|
container,
|
|
|
|
anchor,
|
|
|
|
parentComponent,
|
|
|
|
null, // fallback tree will not have suspense context
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
|
|
|
setActiveBranch(suspense, newFallback)
|
|
|
|
}
|
|
|
|
} else if (activeBranch && isSameVNodeType(newBranch, activeBranch)) {
|
|
|
|
// toggled "back" to current active branch
|
|
|
|
patch(
|
|
|
|
activeBranch,
|
|
|
|
newBranch,
|
|
|
|
container,
|
|
|
|
anchor,
|
|
|
|
parentComponent,
|
|
|
|
suspense,
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
|
|
|
// force resolve
|
|
|
|
suspense.resolve(true)
|
|
|
|
} else {
|
|
|
|
// switched to a 3rd branch
|
|
|
|
patch(
|
|
|
|
null,
|
|
|
|
newBranch,
|
|
|
|
suspense.hiddenContainer,
|
|
|
|
null,
|
|
|
|
parentComponent,
|
|
|
|
suspense,
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
|
|
|
if (suspense.deps <= 0) {
|
|
|
|
suspense.resolve()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (activeBranch && isSameVNodeType(newBranch, activeBranch)) {
|
|
|
|
// root did not change, just normal patch
|
|
|
|
patch(
|
|
|
|
activeBranch,
|
|
|
|
newBranch,
|
2019-10-29 16:30:09 +00:00
|
|
|
container,
|
|
|
|
anchor,
|
|
|
|
parentComponent,
|
2020-09-15 16:45:06 +00:00
|
|
|
suspense,
|
2019-10-29 16:30:09 +00:00
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
2020-09-15 16:45:06 +00:00
|
|
|
setActiveBranch(suspense, newBranch)
|
|
|
|
} else {
|
|
|
|
// root node toggled
|
|
|
|
// invoke @pending event
|
|
|
|
const onPending = n2.props && n2.props.onPending
|
|
|
|
if (isFunction(onPending)) {
|
|
|
|
onPending()
|
|
|
|
}
|
|
|
|
// mount pending branch in off-dom container
|
|
|
|
suspense.pendingBranch = newBranch
|
|
|
|
suspense.pendingId++
|
|
|
|
patch(
|
|
|
|
null,
|
|
|
|
newBranch,
|
|
|
|
suspense.hiddenContainer,
|
|
|
|
null,
|
|
|
|
parentComponent,
|
|
|
|
suspense,
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
|
|
|
if (suspense.deps <= 0) {
|
|
|
|
// incoming branch has no async deps, resolve now.
|
|
|
|
suspense.resolve()
|
|
|
|
} else {
|
|
|
|
const { timeout, pendingId } = suspense
|
|
|
|
if (timeout > 0) {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (suspense.pendingId === pendingId) {
|
|
|
|
suspense.fallback(newFallback)
|
|
|
|
}
|
|
|
|
}, timeout)
|
|
|
|
} else if (timeout === 0) {
|
|
|
|
suspense.fallback(newFallback)
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 16:30:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 15:28:40 +00:00
|
|
|
|
2020-03-23 15:08:22 +00:00
|
|
|
export interface SuspenseBoundary {
|
2020-07-17 15:43:28 +00:00
|
|
|
vnode: VNode<RendererNode, RendererElement, SuspenseProps>
|
2020-03-23 15:08:22 +00:00
|
|
|
parent: SuspenseBoundary | null
|
2019-09-11 21:38:26 +00:00
|
|
|
parentComponent: ComponentInternalInstance | null
|
2019-09-12 16:16:01 +00:00
|
|
|
isSVG: boolean
|
|
|
|
optimized: boolean
|
2020-03-23 15:08:22 +00:00
|
|
|
container: RendererElement
|
|
|
|
hiddenContainer: RendererElement
|
|
|
|
anchor: RendererNode | null
|
2020-09-15 16:45:06 +00:00
|
|
|
activeBranch: VNode | null
|
|
|
|
pendingBranch: VNode | null
|
2019-09-07 15:28:40 +00:00
|
|
|
deps: number
|
2020-09-15 16:45:06 +00:00
|
|
|
pendingId: number
|
|
|
|
timeout: number
|
|
|
|
isInFallback: boolean
|
2020-03-13 02:19:41 +00:00
|
|
|
isHydrating: boolean
|
2019-09-11 17:22:18 +00:00
|
|
|
isUnmounted: boolean
|
2019-09-11 00:53:28 +00:00
|
|
|
effects: Function[]
|
2020-09-15 16:45:06 +00:00
|
|
|
resolve(force?: boolean): void
|
|
|
|
fallback(fallbackVNode: VNode): void
|
2020-03-23 15:08:22 +00:00
|
|
|
move(
|
|
|
|
container: RendererElement,
|
|
|
|
anchor: RendererNode | null,
|
|
|
|
type: MoveType
|
|
|
|
): void
|
|
|
|
next(): RendererNode | null
|
2019-10-29 16:30:09 +00:00
|
|
|
registerDep(
|
|
|
|
instance: ComponentInternalInstance,
|
2020-03-23 15:08:22 +00:00
|
|
|
setupRenderEffect: SetupRenderEffectFn
|
2019-10-29 16:30:09 +00:00
|
|
|
): void
|
2020-03-23 15:08:22 +00:00
|
|
|
unmount(parentSuspense: SuspenseBoundary | null, doRemove?: boolean): void
|
2019-09-07 15:28:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 20:13:44 +00:00
|
|
|
let hasWarned = false
|
|
|
|
|
2020-03-23 15:08:22 +00:00
|
|
|
function createSuspenseBoundary(
|
|
|
|
vnode: VNode,
|
|
|
|
parent: SuspenseBoundary | null,
|
2019-09-11 21:38:26 +00:00
|
|
|
parentComponent: ComponentInternalInstance | null,
|
2020-03-23 15:08:22 +00:00
|
|
|
container: RendererElement,
|
|
|
|
hiddenContainer: RendererElement,
|
|
|
|
anchor: RendererNode | null,
|
2019-09-12 16:16:01 +00:00
|
|
|
isSVG: boolean,
|
2019-10-29 16:30:09 +00:00
|
|
|
optimized: boolean,
|
2020-03-23 15:08:22 +00:00
|
|
|
rendererInternals: RendererInternals,
|
2020-03-13 02:19:41 +00:00
|
|
|
isHydrating = false
|
2020-03-23 15:08:22 +00:00
|
|
|
): SuspenseBoundary {
|
2020-04-24 20:13:44 +00:00
|
|
|
/* istanbul ignore if */
|
|
|
|
if (__DEV__ && !__TEST__ && !hasWarned) {
|
|
|
|
hasWarned = true
|
2020-06-09 14:17:42 +00:00
|
|
|
// @ts-ignore `console.info` cannot be null error
|
2020-04-24 20:13:44 +00:00
|
|
|
console[console.info ? 'info' : 'log'](
|
|
|
|
`<Suspense> is an experimental feature and its API will likely change.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:30:09 +00:00
|
|
|
const {
|
2020-02-15 16:40:09 +00:00
|
|
|
p: patch,
|
|
|
|
m: move,
|
|
|
|
um: unmount,
|
|
|
|
n: next,
|
2020-09-15 16:45:06 +00:00
|
|
|
o: { parentNode, remove }
|
2019-10-29 16:30:09 +00:00
|
|
|
} = rendererInternals
|
|
|
|
|
2020-09-15 16:45:06 +00:00
|
|
|
const timeout = toNumber(vnode.props && vnode.props.timeout)
|
2020-03-23 15:08:22 +00:00
|
|
|
const suspense: SuspenseBoundary = {
|
2019-09-09 20:00:50 +00:00
|
|
|
vnode,
|
2019-09-09 17:59:53 +00:00
|
|
|
parent,
|
2019-09-11 21:38:26 +00:00
|
|
|
parentComponent,
|
2019-09-12 16:16:01 +00:00
|
|
|
isSVG,
|
|
|
|
optimized,
|
2019-09-10 15:01:11 +00:00
|
|
|
container,
|
2019-09-11 21:38:26 +00:00
|
|
|
hiddenContainer,
|
|
|
|
anchor,
|
2019-09-07 15:28:40 +00:00
|
|
|
deps: 0,
|
2020-09-15 16:45:06 +00:00
|
|
|
pendingId: 0,
|
|
|
|
timeout: typeof timeout === 'number' ? timeout : -1,
|
|
|
|
activeBranch: null,
|
|
|
|
pendingBranch: null,
|
|
|
|
isInFallback: true,
|
2020-03-13 02:19:41 +00:00
|
|
|
isHydrating,
|
2019-09-11 17:22:18 +00:00
|
|
|
isUnmounted: false,
|
2019-10-29 16:30:09 +00:00
|
|
|
effects: [],
|
|
|
|
|
2020-09-15 16:45:06 +00:00
|
|
|
resolve(resume = false) {
|
2019-10-29 16:30:09 +00:00
|
|
|
if (__DEV__) {
|
2020-09-15 16:45:06 +00:00
|
|
|
if (!resume && !suspense.pendingBranch) {
|
2019-10-29 16:30:09 +00:00
|
|
|
throw new Error(
|
2020-09-15 16:45:06 +00:00
|
|
|
`suspense.resolve() is called without a pending branch.`
|
2019-10-29 16:30:09 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if (suspense.isUnmounted) {
|
|
|
|
throw new Error(
|
2020-09-15 16:45:06 +00:00
|
|
|
`suspense.resolve() is called on an already unmounted suspense boundary.`
|
2019-10-29 16:30:09 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const {
|
|
|
|
vnode,
|
2020-09-15 16:45:06 +00:00
|
|
|
activeBranch,
|
|
|
|
pendingBranch,
|
|
|
|
pendingId,
|
2019-10-29 16:30:09 +00:00
|
|
|
effects,
|
|
|
|
parentComponent,
|
|
|
|
container
|
|
|
|
} = suspense
|
|
|
|
|
2020-03-13 02:19:41 +00:00
|
|
|
if (suspense.isHydrating) {
|
|
|
|
suspense.isHydrating = false
|
2020-09-15 16:45:06 +00:00
|
|
|
} else if (!resume) {
|
|
|
|
const delayEnter =
|
|
|
|
activeBranch &&
|
|
|
|
pendingBranch!.transition &&
|
|
|
|
pendingBranch!.transition.mode === 'out-in'
|
|
|
|
if (delayEnter) {
|
|
|
|
activeBranch!.transition!.afterLeave = () => {
|
|
|
|
if (pendingId === suspense.pendingId) {
|
|
|
|
move(pendingBranch!, container, anchor, MoveType.ENTER)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-13 02:19:41 +00:00
|
|
|
// this is initial anchor on mount
|
|
|
|
let { anchor } = suspense
|
2020-09-15 16:45:06 +00:00
|
|
|
// unmount current active tree
|
|
|
|
if (activeBranch) {
|
2020-03-13 02:19:41 +00:00
|
|
|
// if the fallback tree was mounted, it may have been moved
|
|
|
|
// as part of a parent suspense. get the latest anchor for insertion
|
2020-09-15 16:45:06 +00:00
|
|
|
anchor = next(activeBranch)
|
|
|
|
unmount(activeBranch, parentComponent, suspense, true)
|
|
|
|
}
|
|
|
|
if (!delayEnter) {
|
|
|
|
// move content from off-dom container to actual container
|
|
|
|
move(pendingBranch!, container, anchor, MoveType.ENTER)
|
2020-03-13 02:19:41 +00:00
|
|
|
}
|
2019-10-29 16:30:09 +00:00
|
|
|
}
|
2020-03-13 02:19:41 +00:00
|
|
|
|
2020-09-15 16:45:06 +00:00
|
|
|
setActiveBranch(suspense, pendingBranch!)
|
|
|
|
suspense.pendingBranch = null
|
|
|
|
suspense.isInFallback = false
|
|
|
|
|
|
|
|
// flush buffered effects
|
2019-10-29 16:30:09 +00:00
|
|
|
// check if there is a pending parent suspense
|
|
|
|
let parent = suspense.parent
|
|
|
|
let hasUnresolvedAncestor = false
|
|
|
|
while (parent) {
|
2020-09-15 16:45:06 +00:00
|
|
|
if (parent.pendingBranch) {
|
2019-10-29 16:30:09 +00:00
|
|
|
// found a pending parent suspense, merge buffered post jobs
|
|
|
|
// into that parent
|
|
|
|
parent.effects.push(...effects)
|
|
|
|
hasUnresolvedAncestor = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
parent = parent.parent
|
|
|
|
}
|
|
|
|
// no pending parent suspense, flush all jobs
|
|
|
|
if (!hasUnresolvedAncestor) {
|
|
|
|
queuePostFlushCb(effects)
|
|
|
|
}
|
2020-03-18 19:40:20 +00:00
|
|
|
suspense.effects = []
|
2020-09-15 16:45:06 +00:00
|
|
|
|
2019-10-29 16:30:09 +00:00
|
|
|
// invoke @resolve event
|
|
|
|
const onResolve = vnode.props && vnode.props.onResolve
|
|
|
|
if (isFunction(onResolve)) {
|
|
|
|
onResolve()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-09-15 16:45:06 +00:00
|
|
|
fallback(fallbackVNode) {
|
|
|
|
if (!suspense.pendingBranch) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:30:09 +00:00
|
|
|
const {
|
|
|
|
vnode,
|
2020-09-15 16:45:06 +00:00
|
|
|
activeBranch,
|
2019-10-29 16:30:09 +00:00
|
|
|
parentComponent,
|
|
|
|
container,
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
} = suspense
|
|
|
|
|
2020-09-16 13:30:47 +00:00
|
|
|
// invoke @fallback event
|
2020-09-15 16:45:06 +00:00
|
|
|
const onFallback = vnode.props && vnode.props.onFallback
|
|
|
|
if (isFunction(onFallback)) {
|
|
|
|
onFallback()
|
|
|
|
}
|
|
|
|
|
|
|
|
const anchor = next(activeBranch!)
|
|
|
|
const mountFallback = () => {
|
|
|
|
if (!suspense.isInFallback) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// mount the fallback tree
|
|
|
|
patch(
|
|
|
|
null,
|
|
|
|
fallbackVNode,
|
|
|
|
container,
|
|
|
|
anchor,
|
|
|
|
parentComponent,
|
|
|
|
null, // fallback tree will not have suspense context
|
|
|
|
isSVG,
|
|
|
|
optimized
|
|
|
|
)
|
|
|
|
setActiveBranch(suspense, fallbackVNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
const delayEnter =
|
|
|
|
fallbackVNode.transition && fallbackVNode.transition.mode === 'out-in'
|
|
|
|
if (delayEnter) {
|
|
|
|
activeBranch!.transition!.afterLeave = mountFallback
|
|
|
|
}
|
|
|
|
// unmount current active branch
|
|
|
|
unmount(
|
|
|
|
activeBranch!,
|
2019-10-29 16:30:09 +00:00
|
|
|
parentComponent,
|
2020-09-15 16:45:06 +00:00
|
|
|
null, // no suspense so unmount hooks fire now
|
|
|
|
true // shouldRemove
|
2019-10-29 16:30:09 +00:00
|
|
|
)
|
|
|
|
|
2020-09-15 16:45:06 +00:00
|
|
|
suspense.isInFallback = true
|
|
|
|
if (!delayEnter) {
|
|
|
|
mountFallback()
|
2019-10-29 16:30:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-25 22:34:28 +00:00
|
|
|
move(container, anchor, type) {
|
2020-09-15 16:45:06 +00:00
|
|
|
suspense.activeBranch &&
|
|
|
|
move(suspense.activeBranch, container, anchor, type)
|
2019-10-29 16:40:54 +00:00
|
|
|
suspense.container = container
|
|
|
|
},
|
|
|
|
|
|
|
|
next() {
|
2020-09-15 16:45:06 +00:00
|
|
|
return suspense.activeBranch && next(suspense.activeBranch)
|
2019-10-29 16:40:54 +00:00
|
|
|
},
|
|
|
|
|
2019-10-29 16:30:09 +00:00
|
|
|
registerDep(instance, setupRenderEffect) {
|
2020-09-15 16:45:06 +00:00
|
|
|
if (!suspense.pendingBranch) {
|
|
|
|
return
|
2019-10-29 16:30:09 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 02:19:41 +00:00
|
|
|
const hydratedEl = instance.vnode.el
|
2019-10-29 16:30:09 +00:00
|
|
|
suspense.deps++
|
|
|
|
instance
|
|
|
|
.asyncDep!.catch(err => {
|
|
|
|
handleError(err, instance, ErrorCodes.SETUP_FUNCTION)
|
|
|
|
})
|
|
|
|
.then(asyncSetupResult => {
|
|
|
|
// retry when the setup() promise resolves.
|
|
|
|
// component may have been unmounted before resolve.
|
2020-09-15 16:45:06 +00:00
|
|
|
if (
|
|
|
|
instance.isUnmounted ||
|
|
|
|
suspense.isUnmounted ||
|
|
|
|
suspense.pendingId !== instance.suspenseId
|
|
|
|
) {
|
2019-10-29 16:30:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
suspense.deps--
|
|
|
|
// retry from this component
|
|
|
|
instance.asyncResolved = true
|
|
|
|
const { vnode } = instance
|
|
|
|
if (__DEV__) {
|
|
|
|
pushWarningContext(vnode)
|
|
|
|
}
|
2020-04-05 22:39:22 +00:00
|
|
|
handleSetupResult(instance, asyncSetupResult, false)
|
2020-03-13 02:19:41 +00:00
|
|
|
if (hydratedEl) {
|
|
|
|
// vnode may have been replaced if an update happened before the
|
2020-05-01 13:42:58 +00:00
|
|
|
// async dep is resolved.
|
2020-03-13 02:19:41 +00:00
|
|
|
vnode.el = hydratedEl
|
|
|
|
}
|
2020-09-15 16:45:06 +00:00
|
|
|
const placeholder = !hydratedEl && instance.subTree.el
|
2019-10-29 16:30:09 +00:00
|
|
|
setupRenderEffect(
|
|
|
|
instance,
|
|
|
|
vnode,
|
2020-03-13 02:19:41 +00:00
|
|
|
// component may have been moved before resolve.
|
|
|
|
// if this is not a hydration, instance.subTree will be the comment
|
|
|
|
// placeholder.
|
2020-09-15 16:45:06 +00:00
|
|
|
parentNode(hydratedEl || instance.subTree.el!)!,
|
2020-03-13 02:19:41 +00:00
|
|
|
// anchor will not be used if this is hydration, so only need to
|
|
|
|
// consider the comment placeholder case.
|
|
|
|
hydratedEl ? null : next(instance.subTree),
|
2020-02-14 04:31:03 +00:00
|
|
|
suspense,
|
2020-04-06 21:37:47 +00:00
|
|
|
isSVG,
|
|
|
|
optimized
|
2019-10-29 16:30:09 +00:00
|
|
|
)
|
2020-09-15 16:45:06 +00:00
|
|
|
if (placeholder) {
|
|
|
|
remove(placeholder)
|
|
|
|
}
|
2019-10-29 16:30:09 +00:00
|
|
|
updateHOCHostEl(instance, vnode.el)
|
|
|
|
if (__DEV__) {
|
|
|
|
popWarningContext()
|
|
|
|
}
|
|
|
|
if (suspense.deps === 0) {
|
|
|
|
suspense.resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
unmount(parentSuspense, doRemove) {
|
|
|
|
suspense.isUnmounted = true
|
2020-09-15 16:45:06 +00:00
|
|
|
if (suspense.activeBranch) {
|
|
|
|
unmount(
|
|
|
|
suspense.activeBranch,
|
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
doRemove
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (suspense.pendingBranch) {
|
2019-10-29 16:30:09 +00:00
|
|
|
unmount(
|
2020-09-15 16:45:06 +00:00
|
|
|
suspense.pendingBranch,
|
2019-10-29 16:30:09 +00:00
|
|
|
parentComponent,
|
|
|
|
parentSuspense,
|
|
|
|
doRemove
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 15:28:40 +00:00
|
|
|
}
|
2019-10-29 16:30:09 +00:00
|
|
|
|
|
|
|
return suspense
|
2019-09-07 15:28:40 +00:00
|
|
|
}
|
2019-09-10 15:01:11 +00:00
|
|
|
|
2020-03-13 02:19:41 +00:00
|
|
|
function hydrateSuspense(
|
|
|
|
node: Node,
|
|
|
|
vnode: VNode,
|
|
|
|
parentComponent: ComponentInternalInstance | null,
|
|
|
|
parentSuspense: SuspenseBoundary | null,
|
|
|
|
isSVG: boolean,
|
|
|
|
optimized: boolean,
|
|
|
|
rendererInternals: RendererInternals,
|
|
|
|
hydrateNode: (
|
|
|
|
node: Node,
|
|
|
|
vnode: VNode,
|
|
|
|
parentComponent: ComponentInternalInstance | null,
|
|
|
|
parentSuspense: SuspenseBoundary | null,
|
|
|
|
optimized: boolean
|
|
|
|
) => Node | null
|
|
|
|
): Node | null {
|
2020-06-10 20:54:23 +00:00
|
|
|
/* eslint-disable no-restricted-globals */
|
2020-03-13 02:19:41 +00:00
|
|
|
const suspense = (vnode.suspense = createSuspenseBoundary(
|
|
|
|
vnode,
|
|
|
|
parentSuspense,
|
|
|
|
parentComponent,
|
2020-03-23 15:08:22 +00:00
|
|
|
node.parentNode!,
|
2020-03-13 02:19:41 +00:00
|
|
|
document.createElement('div'),
|
|
|
|
null,
|
|
|
|
isSVG,
|
|
|
|
optimized,
|
|
|
|
rendererInternals,
|
|
|
|
true /* hydrating */
|
|
|
|
))
|
|
|
|
// there are two possible scenarios for server-rendered suspense:
|
|
|
|
// - success: ssr content should be fully resolved
|
|
|
|
// - failure: ssr content should be the fallback branch.
|
|
|
|
// however, on the client we don't really know if it has failed or not
|
|
|
|
// attempt to hydrate the DOM assuming it has succeeded, but we still
|
|
|
|
// need to construct a suspense boundary first
|
|
|
|
const result = hydrateNode(
|
|
|
|
node,
|
2020-09-15 16:45:06 +00:00
|
|
|
(suspense.pendingBranch = vnode.ssContent!),
|
2020-03-13 02:19:41 +00:00
|
|
|
parentComponent,
|
|
|
|
suspense,
|
|
|
|
optimized
|
|
|
|
)
|
|
|
|
if (suspense.deps === 0) {
|
|
|
|
suspense.resolve()
|
|
|
|
}
|
|
|
|
return result
|
2020-06-10 20:54:23 +00:00
|
|
|
/* eslint-enable no-restricted-globals */
|
2020-03-13 02:19:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 22:20:30 +00:00
|
|
|
export function normalizeSuspenseChildren(
|
2019-09-10 15:01:11 +00:00
|
|
|
vnode: VNode
|
|
|
|
): {
|
|
|
|
content: VNode
|
|
|
|
fallback: VNode
|
|
|
|
} {
|
2019-09-10 15:17:26 +00:00
|
|
|
const { shapeFlag, children } = vnode
|
2020-09-15 16:45:06 +00:00
|
|
|
let content: VNode
|
|
|
|
let fallback: VNode
|
2019-09-10 15:01:11 +00:00
|
|
|
if (shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
|
2020-09-15 16:45:06 +00:00
|
|
|
content = normalizeSuspenseSlot((children as Slots).default)
|
|
|
|
fallback = normalizeSuspenseSlot((children as Slots).fallback)
|
2019-10-13 02:52:11 +00:00
|
|
|
} else {
|
2020-09-15 16:45:06 +00:00
|
|
|
content = normalizeSuspenseSlot(children as VNodeChild)
|
|
|
|
fallback = normalizeVNode(null)
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
content,
|
|
|
|
fallback
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeSuspenseSlot(s: any) {
|
|
|
|
if (isFunction(s)) {
|
|
|
|
s = s()
|
|
|
|
}
|
|
|
|
if (isArray(s)) {
|
|
|
|
const singleChild = filterSingleRoot(s)
|
|
|
|
if (__DEV__ && !singleChild) {
|
|
|
|
warn(`<Suspense> slots expect a single root node.`)
|
2019-10-13 02:52:11 +00:00
|
|
|
}
|
2020-09-15 16:45:06 +00:00
|
|
|
s = singleChild
|
2019-09-10 15:01:11 +00:00
|
|
|
}
|
2020-09-15 16:45:06 +00:00
|
|
|
return normalizeVNode(s)
|
2019-09-10 15:01:11 +00:00
|
|
|
}
|
2019-10-29 16:40:54 +00:00
|
|
|
|
|
|
|
export function queueEffectWithSuspense(
|
|
|
|
fn: Function | Function[],
|
|
|
|
suspense: SuspenseBoundary | null
|
|
|
|
): void {
|
2020-09-15 16:45:06 +00:00
|
|
|
if (suspense && suspense.pendingBranch) {
|
2019-10-29 16:40:54 +00:00
|
|
|
if (isArray(fn)) {
|
|
|
|
suspense.effects.push(...fn)
|
|
|
|
} else {
|
|
|
|
suspense.effects.push(fn)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
queuePostFlushCb(fn)
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 16:45:06 +00:00
|
|
|
|
|
|
|
function setActiveBranch(suspense: SuspenseBoundary, branch: VNode) {
|
|
|
|
suspense.activeBranch = branch
|
|
|
|
const { vnode, parentComponent } = suspense
|
|
|
|
const el = (vnode.el = branch.el)
|
|
|
|
// in case suspense is the root node of a component,
|
|
|
|
// recursively update the HOC el
|
|
|
|
if (parentComponent && parentComponent.subTree === vnode) {
|
|
|
|
parentComponent.vnode.el = el
|
|
|
|
updateHOCHostEl(parentComponent, el)
|
|
|
|
}
|
|
|
|
}
|