-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
apiRender.ts
175 lines (152 loc) · 4.42 KB
/
apiRender.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import {
type ComponentInternalInstance,
componentKey,
createSetupContext,
getAttrsProxy,
getSlotsProxy,
setCurrentInstance,
validateComponentName,
} from './component'
import { insert, querySelector } from './dom/element'
import { flushPostFlushCbs, queuePostFlushCb } from './scheduler'
import { invokeLifecycle } from './componentLifecycle'
import { VaporLifecycleHooks } from './enums'
import {
pauseTracking,
proxyRefs,
resetTracking,
shallowReadonly,
} from '@vue/reactivity'
import { isArray, isFunction, isObject } from '@vue/shared'
import { VaporErrorCodes, callWithErrorHandling } from './errorHandling'
import { endMeasure, startMeasure } from './profiling'
import { devtoolsComponentAdded } from './devtools'
import { fallThroughAttrs } from './componentAttrs'
import { type Block, findFirstRootElement, fragmentKey } from './block'
export function setupComponent(instance: ComponentInternalInstance): void {
if (__DEV__) {
startMeasure(instance, `init`)
}
const reset = setCurrentInstance(instance)
instance.scope.run(() => {
const { type: component, props } = instance
if (__DEV__) {
if (component.name) {
validateComponentName(component.name, instance.appContext.config)
}
}
const setupFn = isFunction(component) ? component : component.setup
let stateOrNode: Block | undefined
if (setupFn) {
const setupContext = (instance.setupContext =
setupFn && setupFn.length > 1 ? createSetupContext(instance) : null)
pauseTracking()
stateOrNode = callWithErrorHandling(
setupFn,
instance,
VaporErrorCodes.SETUP_FUNCTION,
[__DEV__ ? shallowReadonly(props) : props, setupContext],
)
resetTracking()
}
let block: Block | undefined
if (
stateOrNode &&
(stateOrNode instanceof Node ||
isArray(stateOrNode) ||
fragmentKey in stateOrNode ||
componentKey in stateOrNode)
) {
block = stateOrNode
} else if (isObject(stateOrNode)) {
instance.setupState = proxyRefs(stateOrNode)
}
if (!block && component.render) {
pauseTracking()
block = callWithErrorHandling(
component.render,
instance,
VaporErrorCodes.RENDER_FUNCTION,
[
instance.setupState, // _ctx
__DEV__ ? shallowReadonly(props) : props, // $props
instance.emit, // $emit
__DEV__ ? getAttrsProxy(instance) : instance.attrs, // $attrs
__DEV__ ? getSlotsProxy(instance) : instance.slots, // $slots
],
)
resetTracking()
}
if (!block) {
// TODO: warn no template
block = []
}
instance.block = block
const rootElement = findFirstRootElement(instance)
if (rootElement) {
fallThroughAttrs(instance, rootElement)
// attach scopeId
for (const id of instance.scopeIds) {
rootElement.setAttribute(id, '')
}
}
return block
})
reset()
if (__DEV__) {
endMeasure(instance, `init`)
}
}
export function render(
instance: ComponentInternalInstance,
container: string | ParentNode,
): void {
mountComponent(instance, (container = normalizeContainer(container)))
flushPostFlushCbs()
}
export function normalizeContainer(container: string | ParentNode): ParentNode {
return typeof container === 'string'
? (querySelector(container) as ParentNode)
: container
}
function mountComponent(
instance: ComponentInternalInstance,
container: ParentNode,
) {
instance.container = container
if (__DEV__) {
startMeasure(instance, 'mount')
}
// hook: beforeMount
invokeLifecycle(instance, VaporLifecycleHooks.BEFORE_MOUNT)
insert(instance.block!, instance.container)
// hook: mounted
invokeLifecycle(
instance,
VaporLifecycleHooks.MOUNTED,
instance => (instance.isMounted = true),
true,
)
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
devtoolsComponentAdded(instance)
}
if (__DEV__) {
endMeasure(instance, 'mount')
}
return instance
}
export function unmountComponent(instance: ComponentInternalInstance): void {
const { container, scope } = instance
// hook: beforeUnmount
invokeLifecycle(instance, VaporLifecycleHooks.BEFORE_UNMOUNT)
scope.stop()
container.textContent = ''
// hook: unmounted
invokeLifecycle(
instance,
VaporLifecycleHooks.UNMOUNTED,
instance => queuePostFlushCb(() => (instance.isUnmounted = true)),
true,
)
flushPostFlushCbs()
}