2019-09-07 00:58:31 +08:00
|
|
|
import { ComponentInternalInstance, currentInstance } from './component'
|
2020-01-29 11:58:02 +08:00
|
|
|
import {
|
|
|
|
VNode,
|
|
|
|
VNodeNormalizedChildren,
|
|
|
|
normalizeVNode,
|
2020-04-07 09:06:48 +08:00
|
|
|
VNodeChild,
|
2020-04-16 04:45:20 +08:00
|
|
|
InternalObjectKey
|
2020-01-29 11:58:02 +08:00
|
|
|
} from './vnode'
|
2020-04-07 09:06:48 +08:00
|
|
|
import {
|
|
|
|
isArray,
|
|
|
|
isFunction,
|
|
|
|
EMPTY_OBJ,
|
|
|
|
ShapeFlags,
|
|
|
|
PatchFlags,
|
|
|
|
extend,
|
|
|
|
def
|
|
|
|
} from '@vue/shared'
|
2019-08-31 03:26:16 +08:00
|
|
|
import { warn } from './warning'
|
2019-11-26 06:34:28 +08:00
|
|
|
import { isKeepAlive } from './components/KeepAlive'
|
2020-03-17 00:20:52 +08:00
|
|
|
import { withCtx } from './helpers/withRenderContext'
|
2019-05-31 18:07:43 +08:00
|
|
|
|
2019-11-01 23:32:53 +08:00
|
|
|
export type Slot = (...args: any[]) => VNode[]
|
2019-10-09 00:43:13 +08:00
|
|
|
|
|
|
|
export type InternalSlots = {
|
2020-02-25 12:05:35 +08:00
|
|
|
[name: string]: Slot | undefined
|
2019-10-09 00:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export type Slots = Readonly<InternalSlots>
|
|
|
|
|
2019-05-31 18:07:43 +08:00
|
|
|
export type RawSlots = {
|
|
|
|
[name: string]: unknown
|
2019-11-26 23:03:36 +08:00
|
|
|
// manual render fn hint to skip forced children updates
|
|
|
|
$stable?: boolean
|
2020-03-17 00:20:52 +08:00
|
|
|
// internal, for tracking slot owner instance. This is attached during
|
|
|
|
// normalizeChildren when the component vnode is created.
|
|
|
|
_ctx?: ComponentInternalInstance | null
|
2020-04-07 09:06:48 +08:00
|
|
|
// internal, indicates compiler generated slots
|
2020-02-13 02:45:35 +08:00
|
|
|
_?: 1
|
2019-05-31 18:07:43 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:06:48 +08:00
|
|
|
const isInternalKey = (key: string) => key[0] === '_' || key === '$stable'
|
|
|
|
|
2019-05-31 18:07:43 +08:00
|
|
|
const normalizeSlotValue = (value: unknown): VNode[] =>
|
|
|
|
isArray(value)
|
|
|
|
? value.map(normalizeVNode)
|
|
|
|
: [normalizeVNode(value as VNodeChild)]
|
|
|
|
|
2020-03-17 00:20:52 +08:00
|
|
|
const normalizeSlot = (
|
|
|
|
key: string,
|
|
|
|
rawSlot: Function,
|
|
|
|
ctx: ComponentInternalInstance | null | undefined
|
|
|
|
): Slot =>
|
|
|
|
withCtx((props: any) => {
|
2020-03-19 06:14:51 +08:00
|
|
|
if (__DEV__ && currentInstance) {
|
2020-03-17 00:20:52 +08:00
|
|
|
warn(
|
|
|
|
`Slot "${key}" invoked outside of the render function: ` +
|
|
|
|
`this will not track dependencies used in the slot. ` +
|
|
|
|
`Invoke the slot function inside the render function instead.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return normalizeSlotValue(rawSlot(props))
|
|
|
|
}, ctx)
|
2019-05-31 18:07:43 +08:00
|
|
|
|
2020-04-07 09:06:48 +08:00
|
|
|
const normalizeObjectSlots = (rawSlots: RawSlots, slots: InternalSlots) => {
|
|
|
|
const ctx = rawSlots._ctx
|
|
|
|
for (const key in rawSlots) {
|
|
|
|
if (isInternalKey(key)) continue
|
|
|
|
const value = rawSlots[key]
|
|
|
|
if (isFunction(value)) {
|
|
|
|
slots[key] = normalizeSlot(key, value, ctx)
|
|
|
|
} else if (value != null) {
|
|
|
|
if (__DEV__) {
|
|
|
|
warn(
|
|
|
|
`Non-function value encountered for slot "${key}". ` +
|
|
|
|
`Prefer function slots for better performance.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const normalized = normalizeSlotValue(value)
|
|
|
|
slots[key] = () => normalized
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const normalizeVNodeSlots = (
|
2019-09-07 00:58:31 +08:00
|
|
|
instance: ComponentInternalInstance,
|
2020-01-29 11:58:02 +08:00
|
|
|
children: VNodeNormalizedChildren
|
2020-04-07 09:06:48 +08:00
|
|
|
) => {
|
|
|
|
if (__DEV__ && !isKeepAlive(instance.vnode)) {
|
|
|
|
warn(
|
|
|
|
`Non-function value encountered for default slot. ` +
|
|
|
|
`Prefer function slots for better performance.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const normalized = normalizeSlotValue(children)
|
|
|
|
instance.slots.default = () => normalized
|
|
|
|
}
|
|
|
|
|
|
|
|
export const initSlots = (
|
|
|
|
instance: ComponentInternalInstance,
|
|
|
|
children: VNodeNormalizedChildren
|
|
|
|
) => {
|
2019-08-22 23:12:37 +08:00
|
|
|
if (instance.vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
|
2020-04-07 09:06:48 +08:00
|
|
|
if ((children as RawSlots)._ === 1) {
|
|
|
|
instance.slots = children as InternalSlots
|
2019-05-31 18:07:43 +08:00
|
|
|
} else {
|
2020-04-07 09:06:48 +08:00
|
|
|
normalizeObjectSlots(children as RawSlots, (instance.slots = {}))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
instance.slots = {}
|
|
|
|
if (children) {
|
|
|
|
normalizeVNodeSlots(instance, children)
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 04:45:20 +08:00
|
|
|
def(instance.slots, InternalObjectKey, 1)
|
2020-04-07 09:06:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const updateSlots = (
|
|
|
|
instance: ComponentInternalInstance,
|
|
|
|
children: VNodeNormalizedChildren
|
|
|
|
) => {
|
|
|
|
const { vnode, slots } = instance
|
|
|
|
let needDeletionCheck = true
|
|
|
|
let deletionComparisonTarget = EMPTY_OBJ
|
|
|
|
if (vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
|
|
|
|
if ((children as RawSlots)._ === 1) {
|
2020-04-09 00:23:44 +08:00
|
|
|
// compiled slots.
|
|
|
|
if (
|
|
|
|
// bail on dynamic slots (v-if, v-for, reference of scope variables)
|
|
|
|
!(vnode.patchFlag & PatchFlags.DYNAMIC_SLOTS) &&
|
|
|
|
// bail on HRM updates
|
|
|
|
!(__DEV__ && instance.parent && instance.parent.renderUpdated)
|
|
|
|
) {
|
|
|
|
// compiled AND static.
|
|
|
|
// no need to update, and skip stale slots removal.
|
2020-04-07 09:06:48 +08:00
|
|
|
needDeletionCheck = false
|
2020-04-09 00:23:44 +08:00
|
|
|
} else {
|
|
|
|
// compiled but dynamic - update slots, but skip normalization.
|
2020-04-07 09:06:48 +08:00
|
|
|
extend(slots, children as Slots)
|
2019-05-31 18:07:43 +08:00
|
|
|
}
|
2020-04-07 09:06:48 +08:00
|
|
|
} else {
|
|
|
|
needDeletionCheck = !(children as RawSlots).$stable
|
|
|
|
normalizeObjectSlots(children as RawSlots, slots)
|
2019-05-31 18:07:43 +08:00
|
|
|
}
|
2020-04-07 09:06:48 +08:00
|
|
|
deletionComparisonTarget = children as RawSlots
|
2020-03-19 06:14:51 +08:00
|
|
|
} else if (children) {
|
2019-06-03 19:58:12 +08:00
|
|
|
// non slot object children (direct value) passed to a component
|
2020-04-07 09:06:48 +08:00
|
|
|
normalizeVNodeSlots(instance, children)
|
|
|
|
deletionComparisonTarget = { default: 1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete stale slots
|
|
|
|
if (needDeletionCheck) {
|
|
|
|
for (const key in slots) {
|
|
|
|
if (!isInternalKey(key) && !(key in deletionComparisonTarget)) {
|
|
|
|
delete slots[key]
|
|
|
|
}
|
2019-05-31 18:07:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|