Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit dce9b61

Browse files
committed
refactor(runtime-vapor): extract firstEffect
1 parent a49b6f9 commit dce9b61

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

packages/runtime-vapor/src/componentAttrs.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import type { RawProps } from './componentProps'
66
import { renderEffect } from './renderEffect'
77

88
export function patchAttrs(instance: ComponentInternalInstance) {
9-
const attrs = instance.attrs
10-
const options = instance.propsOptions[0]
9+
const {
10+
attrs,
11+
rawProps,
12+
propsOptions: [options],
13+
} = instance
1114

1215
const keys = new Set<string>()
13-
if (instance.rawProps.length)
14-
for (const props of Array.from(instance.rawProps).reverse()) {
16+
if (rawProps.length)
17+
for (const props of Array.from(rawProps).reverse()) {
1518
if (isFunction(props)) {
1619
const resolved = props()
1720
for (const rawKey in resolved) {

packages/runtime-vapor/src/componentProps.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import {
99
isArray,
1010
isFunction,
1111
} from '@vue/shared'
12-
import { baseWatch, shallowReactive } from '@vue/reactivity'
12+
import { shallowReactive } from '@vue/reactivity'
1313
import { warn } from './warning'
1414
import {
1515
type Component,
1616
type ComponentInternalInstance,
1717
setCurrentInstance,
1818
} from './component'
1919
import { patchAttrs } from './componentAttrs'
20-
import { createVaporPreScheduler } from './scheduler'
20+
import { firstEffect } from './renderEffect'
2121

2222
export type ComponentPropsOptions<P = Data> =
2323
| ComponentObjectPropsOptions<P>
@@ -82,16 +82,16 @@ export function initProps(
8282
rawProps: RawProps,
8383
isStateful: boolean,
8484
) {
85-
const props: Data = {}
86-
const attrs = (instance.attrs = shallowReactive<Data>({}))
87-
8885
if (!rawProps) rawProps = []
8986
else if (!isArray(rawProps)) rawProps = [rawProps]
9087
instance.rawProps = rawProps
9188

89+
const props: Data = {}
90+
const attrs = (instance.attrs = shallowReactive<Data>({}))
9291
const [options] = instance.propsOptions
93-
92+
// has v-bind or :[eventName]
9493
const hasDynamicProps = rawProps.some(isFunction)
94+
9595
if (options) {
9696
if (hasDynamicProps) {
9797
for (const key in options) {
@@ -100,15 +100,11 @@ export function initProps(
100100
registerProp(instance, props, key, getter, true)
101101
}
102102
} else {
103+
const staticProps = rawProps[0] as StaticProps
103104
for (const key in options) {
104-
const rawKey = rawProps[0] && getRawKey(rawProps[0] as StaticProps, key)
105+
const rawKey = staticProps && getRawKey(staticProps, key)
105106
if (rawKey) {
106-
registerProp(
107-
instance,
108-
props,
109-
key,
110-
(rawProps[0] as StaticProps)[rawKey],
111-
)
107+
registerProp(instance, props, key, staticProps[rawKey])
112108
} else {
113109
registerProp(instance, props, key, undefined, false, true)
114110
}
@@ -122,15 +118,13 @@ export function initProps(
122118
}
123119

124120
if (hasDynamicProps) {
125-
baseWatch(() => patchAttrs(instance), undefined, {
126-
scheduler: createVaporPreScheduler(instance),
127-
})
121+
firstEffect(instance, () => patchAttrs(instance))
128122
} else {
129123
patchAttrs(instance)
130124
}
131125

132126
if (isStateful) {
133-
instance.props = /* isSSR ? props : */ shallowReactive(props)
127+
instance.props = /* TODO isSSR ? props : */ shallowReactive(props)
134128
} else {
135129
// functional w/ optional props, props === attrs
136130
instance.props = instance.propsOptions === EMPTY_ARR ? attrs : props

packages/runtime-vapor/src/componentSlots.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { type IfAny, isArray, isFunction } from '@vue/shared'
22
import {
33
type EffectScope,
4-
ReactiveEffect,
5-
type SchedulerJob,
6-
SchedulerJobFlags,
74
effectScope,
85
isReactive,
96
shallowReactive,
@@ -14,9 +11,8 @@ import {
1411
setCurrentInstance,
1512
} from './component'
1613
import { type Block, type Fragment, fragmentKey } from './apiRender'
17-
import { renderEffect } from './renderEffect'
14+
import { firstEffect, renderEffect } from './renderEffect'
1815
import { createComment, createTextNode, insert, remove } from './dom/element'
19-
import { queueJob } from './scheduler'
2016
import { VaporErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
2117

2218
// TODO: SSR
@@ -56,8 +52,7 @@ export function initSlots(
5652
if (dynamicSlots) {
5753
slots = shallowReactive(slots)
5854
const dynamicSlotKeys: Record<string, true> = {}
59-
60-
const effect = new ReactiveEffect(() => {
55+
firstEffect(instance, () => {
6156
const _dynamicSlots = callWithAsyncErrorHandling(
6257
dynamicSlots,
6358
instance,
@@ -98,12 +93,6 @@ export function initSlots(
9893
}
9994
}
10095
})
101-
102-
const job: SchedulerJob = () => effect.run()
103-
job.flags! |= SchedulerJobFlags.PRE
104-
job.id = instance.uid
105-
effect.scheduler = () => queueJob(job)
106-
effect.run()
10796
}
10897

10998
instance.slots = slots

packages/runtime-vapor/src/renderEffect.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
import { EffectFlags, ReactiveEffect, type SchedulerJob } from '@vue/reactivity'
1+
import {
2+
EffectFlags,
3+
ReactiveEffect,
4+
type SchedulerJob,
5+
SchedulerJobFlags,
6+
} from '@vue/reactivity'
27
import { invokeArrayFns } from '@vue/shared'
3-
import { getCurrentInstance, setCurrentInstance } from './component'
8+
import {
9+
type ComponentInternalInstance,
10+
getCurrentInstance,
11+
setCurrentInstance,
12+
} from './component'
413
import { queueJob, queuePostRenderEffect } from './scheduler'
514
import { VaporErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
615
import { invokeDirectiveHook } from './directives'
@@ -57,3 +66,15 @@ export function renderEffect(cb: () => void) {
5766

5867
effect.run()
5968
}
69+
70+
export function firstEffect(
71+
instance: ComponentInternalInstance,
72+
fn: () => void,
73+
) {
74+
const effect = new ReactiveEffect(fn)
75+
const job: SchedulerJob = () => effect.run()
76+
job.flags! |= SchedulerJobFlags.PRE
77+
job.id = instance.uid
78+
effect.scheduler = () => queueJob(job)
79+
effect.run()
80+
}

0 commit comments

Comments
 (0)