@@ -5,83 +5,57 @@ import Bridge from 'react-devtools-shared/src/bridge';
5
5
import { initBackend } from 'react-devtools-shared/src/backend' ;
6
6
import { installHook } from 'react-devtools-shared/src/hook' ;
7
7
import setupNativeStyleEditor from 'react-devtools-shared/src/backend/NativeStyleEditor/setupNativeStyleEditor' ;
8
- import {
9
- MESSAGE_TYPE_GET_SAVED_PREFERENCES ,
10
- MESSAGE_TYPE_SAVED_PREFERENCES ,
11
- } from './constants' ;
12
8
13
- function startActivation ( contentWindow : window ) {
14
- const { parent} = contentWindow ;
15
-
16
- const onMessage = ( { data} ) => {
17
- switch ( data . type ) {
18
- case MESSAGE_TYPE_SAVED_PREFERENCES :
19
- // This is the only message we're listening for,
20
- // so it's safe to cleanup after we've received it.
21
- contentWindow . removeEventListener ( 'message' , onMessage ) ;
22
-
23
- const {
24
- appendComponentStack,
25
- breakOnConsoleErrors,
26
- componentFilters,
27
- showInlineWarningsAndErrors,
28
- hideConsoleLogsInStrictMode,
29
- } = data ;
30
-
31
- contentWindow . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = appendComponentStack ;
32
- contentWindow . __REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ = breakOnConsoleErrors ;
33
- contentWindow . __REACT_DEVTOOLS_COMPONENT_FILTERS__ = componentFilters ;
34
- contentWindow . __REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ = showInlineWarningsAndErrors ;
35
- contentWindow . __REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = hideConsoleLogsInStrictMode ;
36
-
37
- // TRICKY
38
- // The backend entry point may be required in the context of an iframe or the parent window.
39
- // If it's required within the parent window, store the saved values on it as well,
40
- // since the injected renderer interface will read from window.
41
- // Technically we don't need to store them on the contentWindow in this case,
42
- // but it doesn't really hurt anything to store them there too.
43
- if ( contentWindow !== window ) {
44
- window . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = appendComponentStack ;
45
- window . __REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ = breakOnConsoleErrors ;
46
- window . __REACT_DEVTOOLS_COMPONENT_FILTERS__ = componentFilters ;
47
- window . __REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ = showInlineWarningsAndErrors ;
48
- window . __REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = hideConsoleLogsInStrictMode ;
49
- }
50
-
51
- finishActivation ( contentWindow ) ;
52
- break ;
53
- default :
54
- break ;
9
+ import type { BackendBridge } from 'react-devtools-shared/src/bridge' ;
10
+ import type { Wall } from 'react-devtools-shared/src/types' ;
11
+
12
+ function startActivation ( contentWindow : window , bridge : BackendBridge ) {
13
+ const onSavedPreferences = data => {
14
+ // This is the only message we're listening for,
15
+ // so it's safe to cleanup after we've received it.
16
+ bridge . removeListener ( 'savedPreferences' , onSavedPreferences ) ;
17
+
18
+ const {
19
+ appendComponentStack,
20
+ breakOnConsoleErrors,
21
+ componentFilters,
22
+ showInlineWarningsAndErrors,
23
+ hideConsoleLogsInStrictMode,
24
+ } = data ;
25
+
26
+ contentWindow . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = appendComponentStack ;
27
+ contentWindow . __REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ = breakOnConsoleErrors ;
28
+ contentWindow . __REACT_DEVTOOLS_COMPONENT_FILTERS__ = componentFilters ;
29
+ contentWindow . __REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ = showInlineWarningsAndErrors ;
30
+ contentWindow . __REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = hideConsoleLogsInStrictMode ;
31
+
32
+ // TRICKY
33
+ // The backend entry point may be required in the context of an iframe or the parent window.
34
+ // If it's required within the parent window, store the saved values on it as well,
35
+ // since the injected renderer interface will read from window.
36
+ // Technically we don't need to store them on the contentWindow in this case,
37
+ // but it doesn't really hurt anything to store them there too.
38
+ if ( contentWindow !== window ) {
39
+ window . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = appendComponentStack ;
40
+ window . __REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ = breakOnConsoleErrors ;
41
+ window . __REACT_DEVTOOLS_COMPONENT_FILTERS__ = componentFilters ;
42
+ window . __REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ = showInlineWarningsAndErrors ;
43
+ window . __REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = hideConsoleLogsInStrictMode ;
55
44
}
45
+
46
+ finishActivation ( contentWindow , bridge ) ;
56
47
} ;
57
48
58
- contentWindow . addEventListener ( 'message ', onMessage ) ;
49
+ bridge . addListener ( 'savedPreferences ', onSavedPreferences ) ;
59
50
60
51
// The backend may be unable to read saved preferences directly,
61
52
// because they are stored in localStorage within the context of the extension (on the frontend).
62
53
// Instead it relies on the extension to pass preferences through.
63
54
// Because we might be in a sandboxed iframe, we have to ask for them by way of postMessage().
64
- parent . postMessage ( { type : MESSAGE_TYPE_GET_SAVED_PREFERENCES } , '* ') ;
55
+ bridge . send ( 'getSavedPreferences ') ;
65
56
}
66
57
67
- function finishActivation ( contentWindow : window ) {
68
- const { parent} = contentWindow ;
69
-
70
- const bridge = new Bridge ( {
71
- listen ( fn ) {
72
- const onMessage = event => {
73
- fn ( event . data ) ;
74
- } ;
75
- contentWindow . addEventListener ( 'message' , onMessage ) ;
76
- return ( ) => {
77
- contentWindow . removeEventListener ( 'message' , onMessage ) ;
78
- } ;
79
- } ,
80
- send ( event : string , payload : any , transferable ?: Array < any > ) {
81
- parent. postMessage ( { event, payload} , '*' , transferable ) ;
82
- } ,
83
- } ) ;
84
-
58
+ function finishActivation ( contentWindow : window , bridge : BackendBridge ) {
85
59
const agent = new Agent ( bridge ) ;
86
60
87
61
const hook = contentWindow . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
@@ -100,8 +74,45 @@ function finishActivation(contentWindow: window) {
100
74
}
101
75
}
102
76
103
- export function activate ( contentWindow : window ) : void {
104
- startActivation ( contentWindow ) ;
77
+ export function activate (
78
+ contentWindow : window ,
79
+ {
80
+ bridge,
81
+ } : { |
82
+ bridge ?: BackendBridge ,
83
+ | } = { } ,
84
+ ) : void {
85
+ if ( bridge == null ) {
86
+ bridge = createBridge ( contentWindow ) ;
87
+ }
88
+
89
+ startActivation ( contentWindow , bridge ) ;
90
+ }
91
+
92
+ export function createBridge (
93
+ contentWindow : window ,
94
+ wall ?: Wall ,
95
+ ) : BackendBridge {
96
+ const { parent} = contentWindow ;
97
+
98
+ if ( wall == null ) {
99
+ wall = {
100
+ listen ( fn ) {
101
+ const onMessage = ( { data} ) => {
102
+ fn ( data ) ;
103
+ } ;
104
+ contentWindow . addEventListener ( 'message' , onMessage ) ;
105
+ return ( ) => {
106
+ contentWindow . removeEventListener ( 'message' , onMessage ) ;
107
+ } ;
108
+ } ,
109
+ send ( event : string , payload : any , transferable ?: Array < any > ) {
110
+ parent. postMessage ( { event, payload} , '*' , transferable ) ;
111
+ } ,
112
+ } ;
113
+ }
114
+
115
+ return ( new Bridge ( wall ) : BackendBridge ) ;
105
116
}
106
117
107
118
export function initialize ( contentWindow : window ) : void {
0 commit comments