2021-03-05 16:10:06 +00:00
|
|
|
import { ComponentInternalInstance } from './component'
|
|
|
|
import { isRenderingCompiledSlot } from './helpers/renderSlot'
|
|
|
|
import { closeBlock, openBlock } from './vnode'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mark the current rendering instance for asset resolution (e.g.
|
|
|
|
* resolveComponent, resolveDirective) during render
|
|
|
|
*/
|
|
|
|
export let currentRenderingInstance: ComponentInternalInstance | null = null
|
|
|
|
export let currentScopeId: string | null = null
|
|
|
|
|
2021-03-26 18:00:03 +00:00
|
|
|
/**
|
|
|
|
* Note: rendering calls maybe nested. The function returns the parent rendering
|
|
|
|
* instance if present, which should be restored after the render is done:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* const prev = setCurrentRenderingInstance(i)
|
|
|
|
* // ...render
|
|
|
|
* setCurrentRenderingInstance(prev)
|
|
|
|
* ```
|
|
|
|
*/
|
2021-03-05 16:10:06 +00:00
|
|
|
export function setCurrentRenderingInstance(
|
|
|
|
instance: ComponentInternalInstance | null
|
2021-03-26 18:00:03 +00:00
|
|
|
): ComponentInternalInstance | null {
|
|
|
|
const prev = currentRenderingInstance
|
2021-03-05 16:10:06 +00:00
|
|
|
currentRenderingInstance = instance
|
|
|
|
currentScopeId = (instance && instance.type.__scopeId) || null
|
2021-03-26 18:00:03 +00:00
|
|
|
return prev
|
2021-03-05 16:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set scope id when creating hoisted vnodes.
|
|
|
|
* @private compiler helper
|
|
|
|
*/
|
|
|
|
export function setScopeId(id: string | null) {
|
|
|
|
currentScopeId = id
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap a slot function to memoize current rendering instance
|
|
|
|
* @private compiler helper
|
|
|
|
*/
|
|
|
|
export function withCtx(
|
|
|
|
fn: Function,
|
|
|
|
ctx: ComponentInternalInstance | null = currentRenderingInstance
|
|
|
|
) {
|
|
|
|
if (!ctx) return fn
|
|
|
|
const renderFnWithContext = (...args: any[]) => {
|
|
|
|
// If a user calls a compiled slot inside a template expression (#1745), it
|
|
|
|
// can mess up block tracking, so by default we need to push a null block to
|
|
|
|
// avoid that. This isn't necessary if rendering a compiled `<slot>`.
|
|
|
|
if (!isRenderingCompiledSlot) {
|
|
|
|
openBlock(true /* null block that disables tracking */)
|
|
|
|
}
|
2021-03-26 18:00:03 +00:00
|
|
|
const prevInstance = setCurrentRenderingInstance(ctx)
|
2021-03-05 16:10:06 +00:00
|
|
|
const res = fn(...args)
|
|
|
|
setCurrentRenderingInstance(prevInstance)
|
|
|
|
if (!isRenderingCompiledSlot) {
|
|
|
|
closeBlock()
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
// mark this as a compiled slot function.
|
|
|
|
// this is used in vnode.ts -> normalizeChildren() to set the slot
|
|
|
|
// rendering flag.
|
|
|
|
renderFnWithContext._c = true
|
|
|
|
return renderFnWithContext
|
|
|
|
}
|