1
1
import { setDevtoolsHook , devtools , ComponentPublicInstance } from 'vue'
2
+ import { ComponentInternalInstance } from '@vue/runtime-core'
2
3
3
4
const enum DevtoolsHooks {
4
5
COMPONENT_EMIT = 'component:emit'
@@ -10,43 +11,44 @@ let events: Record<string, typeof componentEvents>
10
11
export function emitted < T = unknown > (
11
12
vm : ComponentPublicInstance ,
12
13
eventName ?: string
13
- ) : T [ ] | Record < string , T [ ] > {
14
+ ) : undefined | T [ ] | Record < string , T [ ] > {
14
15
const cid = vm . $ . uid
15
16
16
17
const vmEvents = ( events as Record < string , Record < string , T [ ] > > ) [ cid ] || { }
17
18
if ( eventName ) {
18
- const emitted = vmEvents
19
- ? ( vmEvents as Record < string , T [ ] > ) [ eventName ]
20
- : undefined
21
- return emitted
19
+ return vmEvents ? ( vmEvents as Record < string , T [ ] > ) [ eventName ] : undefined
22
20
}
23
21
24
22
return vmEvents as Record < string , T [ ] >
25
23
}
24
+ type Events = { [ id : number ] : Record < string , any > }
26
25
27
26
export const attachEmitListener = ( ) => {
28
27
events = { }
29
28
// use devtools to capture this "emit"
30
29
setDevtoolsHook ( createDevTools ( events ) )
31
30
}
32
31
33
- function createDevTools ( events ) : any {
34
- const devTools : Partial < typeof devtools > = {
32
+ function createDevTools ( events : Events ) : any {
33
+ return {
35
34
emit ( eventType , ...payload ) {
36
35
if ( eventType !== DevtoolsHooks . COMPONENT_EMIT ) return
37
36
38
37
const [ rootVM , componentVM , event , eventArgs ] = payload
39
38
recordEvent ( events , componentVM , event , eventArgs )
40
39
}
41
- }
42
-
43
- return devTools
40
+ } as Partial < typeof devtools >
44
41
}
45
42
46
- function recordEvent ( events , vm , event , args ) : void {
43
+ function recordEvent (
44
+ events : Events ,
45
+ vm : ComponentInternalInstance ,
46
+ event : string ,
47
+ args : Events [ number ]
48
+ ) : void {
47
49
// Functional component wrapper creates a parent component
48
50
let wrapperVm = vm
49
- while ( typeof wrapperVm . type === 'function' ) wrapperVm = wrapperVm . parent
51
+ while ( typeof wrapperVm ? .type === 'function' ) wrapperVm = wrapperVm . parent !
50
52
51
53
const cid = wrapperVm . uid
52
54
if ( ! ( cid in events ) ) {
0 commit comments