feat(compiler-core): wrap slot functions with render context
This commit is contained in:
@@ -18,7 +18,6 @@ import { warn } from './warning'
|
||||
// resolveComponent, resolveDirective) during render
|
||||
export let currentRenderingInstance: ComponentInternalInstance | null = null
|
||||
|
||||
// exposed for server-renderer only
|
||||
export function setCurrentRenderingInstance(
|
||||
instance: ComponentInternalInstance | null
|
||||
) {
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
// These are only used in esm-bundler builds, but since exports cannot be
|
||||
// conditional, we can only drop inner implementations in non-bundler builds.
|
||||
|
||||
import { ComponentInternalInstance } from '../component'
|
||||
import { withCtx } from './withRenderContext'
|
||||
|
||||
export let currentScopeId: string | null = null
|
||||
const scopeIdStack: string[] = []
|
||||
|
||||
@@ -20,13 +23,14 @@ export function popScopeId() {
|
||||
|
||||
export function withScopeId(id: string): <T extends Function>(fn: T) => T {
|
||||
if (__BUNDLER__) {
|
||||
return ((fn: Function) => {
|
||||
return function(this: any) {
|
||||
return ((fn: Function, ctx?: ComponentInternalInstance) => {
|
||||
function renderWithId(this: any) {
|
||||
pushScopeId(id)
|
||||
const res = fn.apply(this, arguments)
|
||||
popScopeId()
|
||||
return res
|
||||
}
|
||||
return ctx ? withCtx(renderWithId, ctx) : renderWithId
|
||||
}) as any
|
||||
} else {
|
||||
return undefined as any
|
||||
|
||||
16
packages/runtime-core/src/helpers/withRenderContext.ts
Normal file
16
packages/runtime-core/src/helpers/withRenderContext.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Slot } from '../componentSlots'
|
||||
import { ComponentInternalInstance } from '../component'
|
||||
import {
|
||||
setCurrentRenderingInstance,
|
||||
currentRenderingInstance
|
||||
} from '../componentRenderUtils'
|
||||
|
||||
export function withCtx(fn: Slot, ctx: ComponentInternalInstance) {
|
||||
return function renderFnWithContext() {
|
||||
const owner = currentRenderingInstance
|
||||
setCurrentRenderingInstance(ctx)
|
||||
const res = fn.apply(null, arguments)
|
||||
setCurrentRenderingInstance(owner)
|
||||
return res
|
||||
}
|
||||
}
|
||||
@@ -85,6 +85,7 @@ export {
|
||||
|
||||
// For compiler generated code
|
||||
// should sync with '@vue/compiler-core/src/runtimeConstants.ts'
|
||||
export { withCtx } from './helpers/withRenderContext'
|
||||
export { withDirectives } from './directives'
|
||||
export {
|
||||
resolveComponent,
|
||||
|
||||
Reference in New Issue
Block a user