feat(compiler-core): wrap slot functions with render context

This commit is contained in:
Evan You
2020-03-16 11:23:29 -04:00
parent bcb750bb3a
commit ecd7ce60d5
13 changed files with 114 additions and 88 deletions

View File

@@ -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
) {

View File

@@ -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

View 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
}
}

View File

@@ -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,