Skip to content

Commit

Permalink
fix: simplify and use correct ctx in withCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 16, 2020
1 parent fd3418d commit 4dc8ffc
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const render = _withId(function render(_ctx, _cache) {
return (_openBlock(), _createBlock(_component_Child, null, {
default: _withId(() => [
_createVNode(\\"div\\")
], _ctx),
]),
_: 1
}))
})"
Expand All @@ -47,15 +47,15 @@ export const render = _withId(function render(_ctx, _cache) {
name: \\"foo\\",
fn: _withId(() => [
_createVNode(\\"div\\")
], _ctx)
])
}
: undefined,
_renderList(_ctx.list, (i) => {
return {
name: i,
fn: _withId(() => [
_createVNode(\\"div\\")
], _ctx)
])
}
})
]), 1024 /* DYNAMIC_SLOTS */))
Expand All @@ -72,10 +72,10 @@ export const render = _withId(function render(_ctx, _cache) {
return (_openBlock(), _createBlock(_component_Child, null, {
foo: _withId(({ msg }) => [
_createTextVNode(_toDisplayString(msg), 1 /* TEXT */)
], _ctx),
]),
bar: _withId(() => [
_createVNode(\\"div\\")
], _ctx),
]),
_: 1
}))
})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ return function render(_ctx, _cache) {
return (_openBlock(), _createBlock(_component_Comp, null, {
default: _withCtx(({ foo }) => [
_createTextVNode(_toDisplayString(_ctx.foo), 1 /* TEXT */)
], _ctx),
]),
_: 1
}))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ return function render(_ctx, _cache) {
const _component_Comp = _resolveComponent(\\"Comp\\")
return (_openBlock(), _createBlock(_component_Comp, null, {
[_ctx.one]: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
[_ctx.two]: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)], _ctx),
[_ctx.one]: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
[_ctx.two]: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)]),
_: 1
}, 1024 /* DYNAMIC_SLOTS */))
}"
Expand All @@ -23,7 +23,7 @@ return function render(_ctx, _cache) {
return (_openBlock(), _createBlock(_component_Comp, null, {
default: _withCtx(() => [
_createVNode(\\"div\\")
], _ctx),
]),
_: 1
}))
}"
Expand All @@ -39,7 +39,7 @@ return function render(_ctx, _cache) {
_renderList(_ctx.list, (name) => {
return {
name: name,
fn: _withCtx(() => [_toDisplayString(name)], _ctx)
fn: _withCtx(() => [_toDisplayString(name)])
}
})
]), 1024 /* DYNAMIC_SLOTS */))
Expand All @@ -56,7 +56,7 @@ return function render(_ctx, _cache) {
(_ctx.ok)
? {
name: \\"one\\",
fn: _withCtx((props) => [_toDisplayString(props)], _ctx)
fn: _withCtx((props) => [_toDisplayString(props)])
}
: undefined
]), 1024 /* DYNAMIC_SLOTS */))
Expand All @@ -76,16 +76,16 @@ return function render(_ctx, _cache) {
ok
? {
name: \\"one\\",
fn: _withCtx(() => [\\"foo\\"], _ctx)
fn: _withCtx(() => [\\"foo\\"])
}
: orNot
? {
name: \\"two\\",
fn: _withCtx((props) => [\\"bar\\"], _ctx)
fn: _withCtx((props) => [\\"bar\\"])
}
: {
name: \\"one\\",
fn: _withCtx(() => [\\"baz\\"], _ctx)
fn: _withCtx(() => [\\"baz\\"])
}
]), 1024 /* DYNAMIC_SLOTS */))
}
Expand All @@ -105,7 +105,7 @@ return function render(_ctx, _cache) {
ok
? {
name: \\"one\\",
fn: _withCtx(() => [\\"hello\\"], _ctx)
fn: _withCtx(() => [\\"hello\\"])
}
: undefined
]), 1024 /* DYNAMIC_SLOTS */))
Expand All @@ -120,8 +120,8 @@ return function render(_ctx, _cache) {
const _component_Comp = _resolveComponent(\\"Comp\\")
return (_openBlock(), _createBlock(_component_Comp, null, {
one: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
two: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)], _ctx),
one: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
two: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)]),
_: 1
}))
}"
Expand All @@ -137,11 +137,11 @@ return function render(_ctx, _cache) {
const _component_Comp = _resolveComponent(\\"Comp\\")
return (_openBlock(), _createBlock(_component_Comp, null, {
one: _withCtx(() => [\\"foo\\"], _ctx),
one: _withCtx(() => [\\"foo\\"]),
default: _withCtx(() => [
\\"bar\\",
_createVNode(\\"span\\")
], _ctx),
]),
_: 1
}))
}
Expand All @@ -158,14 +158,14 @@ return function render(_ctx, _cache) {
return (_openBlock(), _createBlock(_component_Comp, null, {
default: _withCtx(({ foo }) => [
_createVNode(_component_Inner, null, {
default: _withCtx(({ bar }) => [_toDisplayString(foo), _toDisplayString(bar), _toDisplayString(_ctx.baz)], _ctx),
default: _withCtx(({ bar }) => [_toDisplayString(foo), _toDisplayString(bar), _toDisplayString(_ctx.baz)]),
_: 1
}, 1024 /* DYNAMIC_SLOTS */),
\\" \\",
_toDisplayString(foo),
_toDisplayString(_ctx.bar),
_toDisplayString(_ctx.baz)
], _ctx),
]),
_: 1
}))
}"
Expand All @@ -178,7 +178,7 @@ return function render(_ctx, _cache) {
const _component_Comp = _resolveComponent(\\"Comp\\")
return (_openBlock(), _createBlock(_component_Comp, null, {
default: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
default: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
_: 1
}))
}"
Expand Down
7 changes: 4 additions & 3 deletions packages/compiler-core/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ import {
WITH_DIRECTIVES,
CREATE_BLOCK,
OPEN_BLOCK,
CREATE_STATIC
CREATE_STATIC,
WITH_CTX
} from './runtimeHelpers'
import { ImportItem } from './transform'

Expand Down Expand Up @@ -769,7 +770,7 @@ function genFunctionExpression(
if (genScopeId) {
push(`_withId(`)
} else if (isSlot) {
push(`_withCtx(`)
push(`_${helperNameMap[WITH_CTX]}(`)
}
push(`(`, node)
if (isArray(params)) {
Expand Down Expand Up @@ -799,7 +800,7 @@ function genFunctionExpression(
push(`}`)
}
if (genScopeId || isSlot) {
push(`, _ctx)`)
push(`)`)
}
}

Expand Down
16 changes: 8 additions & 8 deletions packages/compiler-ssr/__tests__/ssrComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('ssr: components', () => {
_createVNode(\\"div\\")
]
}
}, _ctx),
}),
_: 1
}, _parent))
}"
Expand All @@ -85,7 +85,7 @@ describe('ssr: components', () => {
_createTextVNode(_toDisplayString(msg + _ctx.outer), 1 /* TEXT */)
]
}
}, _ctx),
}),
_: 1
}, _parent))
}"
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('ssr: components', () => {
_createTextVNode(\\"foo\\")
]
}
}, _ctx),
}),
named: _withCtx((_, _push, _parent, _scopeId) => {
if (_push) {
_push(\`bar\`)
Expand All @@ -123,7 +123,7 @@ describe('ssr: components', () => {
_createTextVNode(\\"bar\\")
]
}
}, _ctx),
}),
_: 1
}, _parent))
}"
Expand Down Expand Up @@ -154,7 +154,7 @@ describe('ssr: components', () => {
_createTextVNode(\\"foo\\")
]
}
}, _ctx)
})
}
: undefined
]), _parent))
Expand Down Expand Up @@ -186,7 +186,7 @@ describe('ssr: components', () => {
_createTextVNode(_toDisplayString(msg + _ctx.key + _ctx.bar), 1 /* TEXT */)
]
}
}, _ctx)
})
}
})
]), _parent))
Expand Down Expand Up @@ -238,7 +238,7 @@ describe('ssr: components', () => {
: _createCommentVNode(\\"v-if\\", true)
]
}
}, _ctx),
}),
bar: _withCtx(({ ok }, _push, _parent, _scopeId) => {
if (_push) {
if (ok) {
Expand All @@ -261,7 +261,7 @@ describe('ssr: components', () => {
: _createCommentVNode(\\"v-if\\", true)
]
}
}, _ctx),
}),
_: 1
}, _parent))
}"
Expand Down
10 changes: 5 additions & 5 deletions packages/compiler-ssr/__tests__/ssrScopeId.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('ssr: scopeId', () => {
_createTextVNode(\\"foo\\")
]
}
}, _ctx),
}),
_: 1
}, _parent))
}"
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('ssr: scopeId', () => {
_createVNode(\\"span\\", null, \\"hello\\")
]
}
}, _ctx),
}),
_: 1
}, _parent))
}"
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('ssr: scopeId', () => {
_createVNode(\\"span\\")
]
}
}, _ctx),
}),
_: 1
}, _parent))
} else {
Expand All @@ -108,12 +108,12 @@ describe('ssr: scopeId', () => {
_createVNode(_component_bar, null, {
default: _withCtx(() => [
_createVNode(\\"span\\")
], _ctx),
]),
_: 1
})
]
}
}, _ctx),
}),
_: 1
}, _parent))
}"
Expand Down
8 changes: 3 additions & 5 deletions packages/runtime-core/src/helpers/scopeId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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
Expand All @@ -23,14 +22,13 @@ export function popScopeId() {

export function withScopeId(id: string): <T extends Function>(fn: T) => T {
if (__BUNDLER__) {
return ((fn: Function, ctx?: ComponentInternalInstance) => {
return withCtx(function(this: any) {
return ((fn: Function) =>
withCtx(function(this: any) {
pushScopeId(id)
const res = fn.apply(this, arguments)

This comment has been minimized.

Copy link
@thecrypticace

thecrypticace Mar 16, 2020

Contributor

withCtx appears to call the passed function with a null this. Is that intended? I only ask as this function preserves this but wrapping it in withCtx destroys that.

cc @yyx990803

This comment has been minimized.

Copy link
@yyx990803

yyx990803 Mar 16, 2020

Author Member

This is intended, withCtx returns the raw function if no ctx is present.

popScopeId()
return res
}, ctx)
}) as any
})) as any
} else {
return undefined as any
}
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/helpers/withRenderContext.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Slot } from '../componentSlots'
import { ComponentInternalInstance } from '../component'
import {
setCurrentRenderingInstance,
currentRenderingInstance
} from '../componentRenderUtils'
import { ComponentInternalInstance } from '../component'

export function withCtx(
fn: Slot,
ctx: ComponentInternalInstance | null | undefined
ctx: ComponentInternalInstance | null = currentRenderingInstance
) {
if (!ctx) return fn
return function renderFnWithContext() {
Expand Down

0 comments on commit 4dc8ffc

Please sign in to comment.