Skip to content

Commit

Permalink
refactor: createComponentInstance to call getCurrentInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
ubugeeei authored and sxzz committed Mar 22, 2024
1 parent f40f5a6 commit 339e674
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 42 deletions.
40 changes: 20 additions & 20 deletions packages/runtime-vapor/__tests__/apiInject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ describe('api: provide/inject', () => {
const Provider = define({
setup() {
provide('foo', 1)
return createComponent(Middle, getCurrentInstance()!)
return createComponent(Middle)
},
})

const Middle = {
render() {
return createComponent(Consumer, getCurrentInstance()!)
return createComponent(Consumer)
},
}

Expand All @@ -59,12 +59,12 @@ describe('api: provide/inject', () => {
const Provider = define({
setup() {
provide(key, 1)
return createComponent(Middle, getCurrentInstance()!)
return createComponent(Middle)
},
})

const Middle = {
render: () => createComponent(Consumer, getCurrentInstance()!),
render: () => createComponent(Consumer),
}

const Consumer = {
Expand All @@ -86,12 +86,12 @@ describe('api: provide/inject', () => {
const Provider = define({
setup() {
provide('foo', 'foo')
return createComponent(Middle, getCurrentInstance()!)
return createComponent(Middle)
},
})

const Middle = {
render: () => createComponent(Consumer, getCurrentInstance()!),
render: () => createComponent(Consumer),
}

const Consumer = {
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('api: provide/inject', () => {
setup() {
provide('foo', 'foo')
provide('bar', 'bar')
return createComponent(ProviderTwo, getCurrentInstance()!)
return createComponent(ProviderTwo)
},
})

Expand All @@ -129,7 +129,7 @@ describe('api: provide/inject', () => {
// override parent value
provide('foo', 'fooOverride')
provide('baz', 'baz')
return createComponent(Consumer, getCurrentInstance()!)
return createComponent(Consumer)
},
}

Expand All @@ -156,12 +156,12 @@ describe('api: provide/inject', () => {
const Provider = define({
setup() {
provide('count', count)
return createComponent(Middle, getCurrentInstance()!)
return createComponent(Middle)
},
})

const Middle = {
render: () => createComponent(Consumer, getCurrentInstance()!),
render: () => createComponent(Consumer),
}

const Consumer = {
Expand Down Expand Up @@ -191,12 +191,12 @@ describe('api: provide/inject', () => {
const Provider = define({
setup() {
provide('count', readonly(count))
return createComponent(Middle, getCurrentInstance()!)
return createComponent(Middle)
},
})

const Middle = {
render: () => createComponent(Consumer, getCurrentInstance()!),
render: () => createComponent(Consumer),
}

const Consumer = {
Expand Down Expand Up @@ -232,12 +232,12 @@ describe('api: provide/inject', () => {
const Provider = define({
setup() {
provide('state', rootState)
return createComponent(Middle, getCurrentInstance()!)
return createComponent(Middle)
},
})

const Middle = {
render: () => createComponent(Consumer, getCurrentInstance()!),
render: () => createComponent(Consumer),
}

const Consumer = {
Expand Down Expand Up @@ -267,12 +267,12 @@ describe('api: provide/inject', () => {
const Provider = define({
setup() {
provide('state', readonly(rootState))
return createComponent(Middle, getCurrentInstance()!)
return createComponent(Middle)
},
})

const Middle = {
render: () => createComponent(Consumer, getCurrentInstance()!),
render: () => createComponent(Consumer),
}

const Consumer = {
Expand Down Expand Up @@ -305,12 +305,12 @@ describe('api: provide/inject', () => {
it('should warn unfound', () => {
const Provider = define({
setup() {
return createComponent(Middle, getCurrentInstance()!)
return createComponent(Middle)
},
})

const Middle = {
render: () => createComponent(Consumer, getCurrentInstance()!),
render: () => createComponent(Consumer),
}

const Consumer = {
Expand All @@ -333,12 +333,12 @@ describe('api: provide/inject', () => {
it('should not warn when default value is undefined', () => {
const Provider = define({
setup() {
return createComponent(Middle, getCurrentInstance()!)
return createComponent(Middle)
},
})

const Middle = {
render: () => createComponent(Consumer, getCurrentInstance()!),
render: () => createComponent(Consumer),
}

const Consumer = {
Expand Down
4 changes: 0 additions & 4 deletions packages/runtime-vapor/__tests__/componentAttrs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('attribute fallthrough', () => {
render(_ctx: Record<string, any>) {
return createComponent(
Child,
null,
[
{
foo: () => _ctx.foo,
Expand Down Expand Up @@ -80,7 +79,6 @@ describe('attribute fallthrough', () => {
render(_ctx: Record<string, any>) {
return createComponent(
Child,
null,
[
{
foo: () => _ctx.foo,
Expand Down Expand Up @@ -120,7 +118,6 @@ describe('attribute fallthrough', () => {
render() {
const n0 = createComponent(
Grandson,
null,
[
{
'custom-attr': () => 'custom-attr',
Expand All @@ -141,7 +138,6 @@ describe('attribute fallthrough', () => {
render(_ctx: Record<string, any>) {
return createComponent(
Child,
null,
[
{
foo: () => _ctx.foo,
Expand Down
5 changes: 1 addition & 4 deletions packages/runtime-vapor/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ describe('component: props', () => {
render(_ctx: Record<string, any>) {
return createComponent(
Child,
null,
{
foo: () => _ctx.foo,
id: () => _ctx.id,
Expand Down Expand Up @@ -409,9 +408,7 @@ describe('component: props', () => {
}

define(() =>
createComponent(Comp, null, [
() => (passFoo.value ? { foo: () => 'ok' } : {}),
]),
createComponent(Comp, [() => (passFoo.value ? { foo: () => 'ok' } : {})]),
).render()

expect(initialKeys).toMatchObject(['foo'])
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-vapor/__tests__/dom/prop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getMetadata, recordPropMetadata } from '../../src/componentMetadata'
let removeComponentInstance = NOOP
beforeEach(() => {
const reset = setCurrentInstance(
createComponentInstance((() => {}) as any, null, {}),
createComponentInstance((() => {}) as any, {}),
)
removeComponentInstance = () => {
reset()
Expand Down
3 changes: 0 additions & 3 deletions packages/runtime-vapor/src/apiCreateComponent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
type Component,
type ComponentInternalInstance,
createComponentInstance,
currentInstance,
} from './component'
Expand All @@ -10,14 +9,12 @@ import { withAttrs } from './componentAttrs'

export function createComponent(
comp: Component,
parent: ComponentInternalInstance | null,
rawProps: RawProps | null = null,
singleRoot: boolean = false,
) {
const current = currentInstance!
const instance = createComponentInstance(
comp,
parent,
singleRoot ? withAttrs(rawProps) : rawProps,
)
setupComponent(instance, singleRoot)
Expand Down
7 changes: 1 addition & 6 deletions packages/runtime-vapor/src/apiCreateVaporApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ export function createVaporApp(

mount(rootContainer): any {
if (!instance) {
instance = createComponentInstance(
rootComponent,
null,
rootProps,
context,
)
instance = createComponentInstance(rootComponent, rootProps, context)
setupComponent(instance)
render(instance, rootContainer)
return instance
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-vapor/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ const emptyAppContext = createAppContext()
let uid = 0
export function createComponentInstance(
component: ObjectComponent | FunctionalComponent,
parent: ComponentInternalInstance | null,
rawProps: RawProps | null,
// application root node only
appContext: AppContext | null = null,
): ComponentInternalInstance {
const parent = getCurrentInstance()
const _appContext =
(parent ? parent.appContext : appContext) || emptyAppContext

Expand Down
4 changes: 1 addition & 3 deletions playground/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import {
createComponent,
defineComponent,
getCurrentInstance,
on,
reactive,
ref,
Expand Down Expand Up @@ -34,7 +33,7 @@ export default defineComponent({
on(n0, 'click', () => handleClick)
renderEffect(() => setText(n0, count.value))
/** @type {any} */
const n1 = createComponent(child, getCurrentInstance(), [
const n1 = createComponent(child, [
{
/* <Comp :count="count" /> */
count: () => {
Expand All @@ -45,7 +44,6 @@ export default defineComponent({
inlineDouble: () => count.value * 2,
id: () => 'hello',
},

() => props,
])
return [n0, n1]
Expand Down

0 comments on commit 339e674

Please sign in to comment.