44 ComponentPublicInstance ,
55 ComponentInternalInstance
66} from 'vue'
7+ import { getGlobalThis } from './utils'
78
89type Events < T = unknown > = Record < number , Record < string , T [ ] > >
910
@@ -28,18 +29,38 @@ export function emitted<T = unknown>(
2829}
2930
3031export const attachEmitListener = ( ) => {
31- // use devtools to capture this "emit"
32- setDevtoolsHook ( createDevTools ( ) , { } )
32+ const target = getGlobalThis ( )
33+ // override emit to capture events when devtools is defined
34+ if ( target . __VUE_DEVTOOLS_GLOBAL_HOOK__ ) {
35+ const _emit = target . __VUE_DEVTOOLS_GLOBAL_HOOK__ . emit
36+ target . __VUE_DEVTOOLS_GLOBAL_HOOK__ . emit = (
37+ eventType : any ,
38+ ...payload : any [ ]
39+ ) => {
40+ _emit . call ( target . __VUE_DEVTOOLS_GLOBAL_HOOK__ , eventType , ...payload )
41+ captureDevtoolsVueComponentEmitEvent ( eventType , payload )
42+ }
43+ } else {
44+ // use devtools to capture this "emit"
45+ setDevtoolsHook ( createDevTools ( ) , { } )
46+ }
47+ }
48+
49+ function captureDevtoolsVueComponentEmitEvent (
50+ eventType : string ,
51+ payload : any [ ]
52+ ) {
53+ if ( eventType === DevtoolsHooks . COMPONENT_EMIT ) {
54+ const [ _ , componentVM , event , eventArgs ] = payload
55+ recordEvent ( componentVM , event , eventArgs )
56+ }
3357}
3458
3559// devtools hook only catches Vue component custom events
3660function createDevTools ( ) : any {
3761 return {
3862 emit ( eventType , ...payload ) {
39- if ( eventType !== DevtoolsHooks . COMPONENT_EMIT ) return
40-
41- const [ _ , componentVM , event , eventArgs ] = payload
42- recordEvent ( componentVM , event , eventArgs )
63+ captureDevtoolsVueComponentEmitEvent ( eventType , payload )
4364 }
4465 } as Partial < typeof devtools >
4566}
0 commit comments