@@ -34,42 +34,25 @@ export type FunctionalComponent = SetupFn &
34
34
displayName ?: string
35
35
}
36
36
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
43
49
}
44
- : never
50
+ }
51
+ }
45
52
46
53
export function createSetupContext (
47
54
instance : ComponentInternalInstance ,
48
55
) : 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
-
73
56
if ( __DEV__ ) {
74
57
// We use getters in dev in case libs like test-utils overwrite instance
75
58
// properties (overwrites should not be done in prod)
@@ -83,15 +66,30 @@ export function createSetupContext(
83
66
get emit ( ) {
84
67
return ( event : string , ...args : any [ ] ) => instance . emit ( event , ...args )
85
68
} ,
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
88
91
} else {
89
- return {
90
- attrs : instance . attrs ,
91
- emit : instance . emit ,
92
- slots : instance . slots ,
93
- expose,
94
- }
92
+ return new SetupContext ( instance )
95
93
}
96
94
}
97
95
0 commit comments