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

Commit 5828f24

Browse files
committed
perf: use class for SetupContext
1 parent d6415d8 commit 5828f24

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

packages/runtime-vapor/src/component.ts

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,25 @@ export type FunctionalComponent = SetupFn &
3434
displayName?: string
3535
}
3636

37-
export type SetupContext<E = EmitsOptions> = E extends any
38-
? {
39-
attrs: Data
40-
emit: EmitFn<E>
41-
expose: (exposed?: Record<string, any>) => void
42-
slots: Readonly<StaticSlots>
37+
export class SetupContext<E = EmitsOptions> {
38+
attrs: Data
39+
emit: EmitFn<E>
40+
slots: Readonly<StaticSlots>
41+
expose: (exposed?: Record<string, any>) => void
42+
43+
constructor(instance: ComponentInternalInstance) {
44+
this.attrs = instance.attrs
45+
this.emit = instance.emit as EmitFn<E>
46+
this.slots = instance.slots
47+
this.expose = (exposed = {}) => {
48+
instance.exposed = exposed
4349
}
44-
: never
50+
}
51+
}
4552

4653
export function createSetupContext(
4754
instance: ComponentInternalInstance,
4855
): SetupContext {
49-
const expose: SetupContext['expose'] = exposed => {
50-
if (__DEV__) {
51-
if (instance.exposed) {
52-
warn(`expose() should be called only once per setup().`)
53-
}
54-
if (exposed != null) {
55-
let exposedType: string = typeof exposed
56-
if (exposedType === 'object') {
57-
if (isArray(exposed)) {
58-
exposedType = 'array'
59-
} else if (isRef(exposed)) {
60-
exposedType = 'ref'
61-
}
62-
}
63-
if (exposedType !== 'object') {
64-
warn(
65-
`expose() should be passed a plain object, received ${exposedType}.`,
66-
)
67-
}
68-
}
69-
}
70-
instance.exposed = exposed || {}
71-
}
72-
7356
if (__DEV__) {
7457
// We use getters in dev in case libs like test-utils overwrite instance
7558
// properties (overwrites should not be done in prod)
@@ -83,15 +66,30 @@ export function createSetupContext(
8366
get emit() {
8467
return (event: string, ...args: any[]) => instance.emit(event, ...args)
8568
},
86-
expose,
87-
})
69+
expose: (exposed?: Record<string, any>) => {
70+
if (instance.exposed) {
71+
warn(`expose() should be called only once per setup().`)
72+
}
73+
if (exposed != null) {
74+
let exposedType: string = typeof exposed
75+
if (exposedType === 'object') {
76+
if (isArray(exposed)) {
77+
exposedType = 'array'
78+
} else if (isRef(exposed)) {
79+
exposedType = 'ref'
80+
}
81+
}
82+
if (exposedType !== 'object') {
83+
warn(
84+
`expose() should be passed a plain object, received ${exposedType}.`,
85+
)
86+
}
87+
}
88+
instance.exposed = exposed || {}
89+
},
90+
}) as SetupContext
8891
} else {
89-
return {
90-
attrs: instance.attrs,
91-
emit: instance.emit,
92-
slots: instance.slots,
93-
expose,
94-
}
92+
return new SetupContext(instance)
9593
}
9694
}
9795

0 commit comments

Comments
 (0)