Skip to content

Commit 2f8b9af

Browse files
Doctor-wusxzz
authored andcommitted
Revert "feat(runtime-vapor): extract apiSetup & init setup ctx"
This reverts commit a0b5653.
1 parent e4bc5ab commit 2f8b9af

File tree

6 files changed

+46
-120
lines changed

6 files changed

+46
-120
lines changed

packages/runtime-vapor/src/apiCreateComponent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
createComponentInstance,
44
currentInstance,
55
} from './component'
6-
import { setupComponent } from './apiSetup'
6+
import { setupComponent } from './apiRender'
77
import type { RawProps } from './componentProps'
88

99
export function createComponent(comp: Component, rawProps: RawProps = null) {

packages/runtime-vapor/src/apiCreateVaporApp.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {
66
} from './component'
77
import { warn } from './warning'
88
import { version } from '.'
9-
import { render, unmountComponent } from './apiRender'
9+
import { render, setupComponent, unmountComponent } from './apiRender'
1010
import type { RawProps } from './componentProps'
11-
import { setupComponent } from './apiSetup'
1211

1312
export function createVaporApp(
1413
rootComponent: Component,

packages/runtime-vapor/src/apiRender.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import type { ComponentInternalInstance } from './component'
1+
import { isArray, isFunction, isObject } from '@vue/shared'
2+
import { type ComponentInternalInstance, setCurrentInstance } from './component'
23
import { insert, querySelector, remove } from './dom/element'
34
import { flushPostFlushCbs, queuePostRenderEffect } from './scheduler'
5+
import { proxyRefs } from '@vue/reactivity'
46
import { invokeLifecycle } from './componentLifecycle'
57
import { VaporLifecycleHooks } from './apiLifecycle'
68

@@ -13,6 +15,43 @@ export type Fragment = {
1315
[fragmentKey]: true
1416
}
1517

18+
export function setupComponent(instance: ComponentInternalInstance): void {
19+
const reset = setCurrentInstance(instance)
20+
instance.scope.run(() => {
21+
const { component, props, emit, attrs } = instance
22+
const ctx = { expose: () => {}, emit, attrs }
23+
24+
const setupFn = isFunction(component) ? component : component.setup
25+
const stateOrNode = setupFn && setupFn(props, ctx)
26+
27+
let block: Block | undefined
28+
29+
if (
30+
stateOrNode &&
31+
(stateOrNode instanceof Node ||
32+
isArray(stateOrNode) ||
33+
(stateOrNode as any)[fragmentKey])
34+
) {
35+
block = stateOrNode as Block
36+
} else if (isObject(stateOrNode)) {
37+
instance.setupState = proxyRefs(stateOrNode)
38+
}
39+
if (!block && component.render) {
40+
block = component.render(instance.setupState)
41+
}
42+
43+
if (block instanceof DocumentFragment) {
44+
block = Array.from(block.childNodes)
45+
}
46+
if (!block) {
47+
// TODO: warn no template
48+
block = []
49+
}
50+
return (instance.block = block)
51+
})
52+
reset()
53+
}
54+
1655
export function render(
1756
instance: ComponentInternalInstance,
1857
container: string | ParentNode,

packages/runtime-vapor/src/apiSetup.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

packages/runtime-vapor/src/component.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ export interface ComponentInternalInstance {
6767
*/
6868
inheritAttrs?: boolean
6969

70-
attrsProxy: Data | null
71-
7270
// lifecycle
7371
isMounted: boolean
7472
isUnmounted: boolean
@@ -178,9 +176,6 @@ export function createComponentInstance(
178176
attrs: EMPTY_OBJ,
179177
refs: EMPTY_OBJ,
180178

181-
inheritAttrs: component.inheritAttrs,
182-
attrsProxy: null,
183-
184179
// lifecycle
185180
isMounted: false,
186181
isUnmounted: false,
@@ -235,6 +230,9 @@ export function createComponentInstance(
235230
*/
236231
// [VaporLifecycleHooks.SERVER_PREFETCH]: null,
237232
}
233+
if (component.inheritAttrs != null) {
234+
instance.inheritAttrs = component.inheritAttrs
235+
}
238236
initProps(instance, rawProps, !isFunction(component))
239237
instance.emit = emit.bind(null, instance)
240238

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { type Data, camelize, isFunction } from '@vue/shared'
1+
import { camelize, isFunction } from '@vue/shared'
22
import type { ComponentInternalInstance } from './component'
33
import { isEmitListener } from './componentEmits'
4-
import { TrackOpTypes, track } from '@vue/reactivity'
5-
import { warn } from './warning'
64

75
export function patchAttrs(instance: ComponentInternalInstance) {
86
const attrs = instance.attrs
@@ -44,45 +42,3 @@ export function patchAttrs(instance: ComponentInternalInstance) {
4442
}
4543
}
4644
}
47-
48-
/**
49-
* dev only flag to track whether $attrs was used during render.
50-
* If $attrs was used during render then the warning for failed attrs
51-
* fallthrough can be suppressed.
52-
*/
53-
let accessedAttrs: boolean = false
54-
55-
function markAttrsAccessed() {
56-
accessedAttrs = true
57-
}
58-
59-
export function getAttrsProxy(instance: ComponentInternalInstance): Data {
60-
return (
61-
instance.attrsProxy ||
62-
(instance.attrsProxy = new Proxy(
63-
instance.attrs,
64-
__DEV__
65-
? {
66-
get(target, key: string) {
67-
markAttrsAccessed()
68-
track(instance, TrackOpTypes.GET, '$attrs')
69-
return target[key]
70-
},
71-
set() {
72-
warn(`setupContext.attrs is readonly.`)
73-
return false
74-
},
75-
deleteProperty() {
76-
warn(`setupContext.attrs is readonly.`)
77-
return false
78-
},
79-
}
80-
: {
81-
get(target, key: string) {
82-
track(instance, TrackOpTypes.GET, '$attrs')
83-
return target[key]
84-
},
85-
},
86-
))
87-
)
88-
}

0 commit comments

Comments
 (0)