suspense.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { VNode, normalizeVNode, VNodeChild } from './vnode'
  2. import { ShapeFlags } from '.'
  3. import { isFunction } from '@vue/shared'
  4. import { ComponentInternalInstance } from './component'
  5. import { Slots } from './componentSlots'
  6. export const SuspenseSymbol = __DEV__ ? Symbol('Suspense key') : Symbol()
  7. export interface SuspenseBoundary<
  8. HostNode = any,
  9. HostElement = any,
  10. HostVNode = VNode<HostNode, HostElement>
  11. > {
  12. vnode: HostVNode
  13. parent: SuspenseBoundary<HostNode, HostElement> | null
  14. parentComponent: ComponentInternalInstance | null
  15. isSVG: boolean
  16. optimized: boolean
  17. container: HostElement
  18. hiddenContainer: HostElement
  19. anchor: HostNode | null
  20. subTree: HostVNode
  21. fallbackTree: HostVNode
  22. deps: number
  23. isResolved: boolean
  24. isUnmounted: boolean
  25. effects: Function[]
  26. }
  27. export function createSuspenseBoundary<HostNode, HostElement>(
  28. vnode: VNode<HostNode, HostElement>,
  29. parent: SuspenseBoundary<HostNode, HostElement> | null,
  30. parentComponent: ComponentInternalInstance | null,
  31. container: HostElement,
  32. hiddenContainer: HostElement,
  33. anchor: HostNode | null,
  34. isSVG: boolean,
  35. optimized: boolean
  36. ): SuspenseBoundary<HostNode, HostElement> {
  37. return {
  38. vnode,
  39. parent,
  40. parentComponent,
  41. isSVG,
  42. optimized,
  43. container,
  44. hiddenContainer,
  45. anchor,
  46. deps: 0,
  47. subTree: null as any, // will be set immediately after creation
  48. fallbackTree: null as any, // will be set immediately after creation
  49. isResolved: false,
  50. isUnmounted: false,
  51. effects: []
  52. }
  53. }
  54. export function normalizeSuspenseChildren(
  55. vnode: VNode
  56. ): {
  57. content: VNode
  58. fallback: VNode
  59. } {
  60. const { shapeFlag, children } = vnode
  61. if (shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
  62. const { default: d, fallback } = children as Slots
  63. return {
  64. content: normalizeVNode(isFunction(d) ? d() : d),
  65. fallback: normalizeVNode(isFunction(fallback) ? fallback() : fallback)
  66. }
  67. } else {
  68. return {
  69. content: normalizeVNode(children as VNodeChild),
  70. fallback: normalizeVNode(null)
  71. }
  72. }
  73. }