@@ -21,6 +21,7 @@ const enum DevtoolsHooks {
21
21
}
22
22
23
23
interface DevtoolsHook {
24
+ enabled ?: boolean
24
25
emit : ( event : string , ...payload : any [ ] ) => void
25
26
on : ( event : string , handler : Function ) => void
26
27
once : ( event : string , handler : Function ) => void
@@ -30,14 +31,33 @@ interface DevtoolsHook {
30
31
31
32
export let devtools : DevtoolsHook
32
33
33
- export function setDevtoolsHook ( hook : DevtoolsHook ) {
34
+ let buffer : { event : string ; args : any [ ] } [ ] = [ ]
35
+
36
+ function emit ( event : string , ...args : any [ ] ) {
37
+ if ( devtools ) {
38
+ devtools . emit ( event , ...args )
39
+ } else {
40
+ buffer . push ( { event, args } )
41
+ }
42
+ }
43
+
44
+ export function setDevtoolsHook ( hook : DevtoolsHook , target : any ) {
34
45
devtools = hook
46
+ if ( devtools ) {
47
+ devtools . enabled = true
48
+ buffer . forEach ( ( { event, args } ) => devtools . emit ( event , ...args ) )
49
+ buffer = [ ]
50
+ } else {
51
+ const replay = ( target . __VUE_DEVTOOLS_HOOK_REPLAY__ =
52
+ target . __VUE_DEVTOOLS_HOOK_REPLAY__ || [ ] )
53
+ replay . push ( ( newHook : DevtoolsHook ) => {
54
+ setDevtoolsHook ( newHook , target )
55
+ } )
56
+ }
35
57
}
36
58
37
59
export function devtoolsInitApp ( app : App , version : string ) {
38
- // TODO queue if devtools is undefined
39
- if ( ! devtools ) return
40
- devtools . emit ( DevtoolsHooks . APP_INIT , app , version , {
60
+ emit ( DevtoolsHooks . APP_INIT , app , version , {
41
61
Fragment,
42
62
Text,
43
63
Comment,
@@ -46,8 +66,7 @@ export function devtoolsInitApp(app: App, version: string) {
46
66
}
47
67
48
68
export function devtoolsUnmountApp ( app : App ) {
49
- if ( ! devtools ) return
50
- devtools . emit ( DevtoolsHooks . APP_UNMOUNT , app )
69
+ emit ( DevtoolsHooks . APP_UNMOUNT , app )
51
70
}
52
71
53
72
export const devtoolsComponentAdded = /*#__PURE__*/ createDevtoolsComponentHook (
@@ -62,8 +81,7 @@ export const devtoolsComponentRemoved =
62
81
63
82
function createDevtoolsComponentHook ( hook : DevtoolsHooks ) {
64
83
return ( component : ComponentInternalInstance ) => {
65
- if ( ! devtools ) return
66
- devtools . emit (
84
+ emit (
67
85
hook ,
68
86
component . appContext . app ,
69
87
component . uid ,
@@ -83,15 +101,7 @@ export const devtoolsPerfEnd = /*#__PURE__*/ createDevtoolsPerformanceHook(
83
101
84
102
function createDevtoolsPerformanceHook ( hook : DevtoolsHooks ) {
85
103
return ( component : ComponentInternalInstance , type : string , time : number ) => {
86
- if ( ! devtools ) return
87
- devtools . emit (
88
- hook ,
89
- component . appContext . app ,
90
- component . uid ,
91
- component ,
92
- type ,
93
- time
94
- )
104
+ emit ( hook , component . appContext . app , component . uid , component , type , time )
95
105
}
96
106
}
97
107
@@ -100,8 +110,7 @@ export function devtoolsComponentEmit(
100
110
event : string ,
101
111
params : any [ ]
102
112
) {
103
- if ( ! devtools ) return
104
- devtools . emit (
113
+ emit (
105
114
DevtoolsHooks . COMPONENT_EMIT ,
106
115
component . appContext . app ,
107
116
component ,
0 commit comments