@@ -10,7 +10,10 @@ import {
10
10
MESSAGE_TYPE_SAVED_PREFERENCES ,
11
11
} from './constants' ;
12
12
13
- function startActivation ( contentWindow : window ) {
13
+ import type { BackendBridge } from 'react-devtools-shared/src/bridge' ;
14
+ import type { Wall } from 'react-devtools-shared/src/types' ;
15
+
16
+ function startActivation ( contentWindow : window , bridge : BackendBridge ) {
14
17
const { parent} = contentWindow ;
15
18
16
19
const onMessage = ( { data} ) => {
@@ -48,7 +51,7 @@ function startActivation(contentWindow: window) {
48
51
window . __REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = hideConsoleLogsInStrictMode ;
49
52
}
50
53
51
- finishActivation ( contentWindow ) ;
54
+ finishActivation ( contentWindow , bridge ) ;
52
55
break ;
53
56
default :
54
57
break ;
@@ -61,27 +64,11 @@ function startActivation(contentWindow: window) {
61
64
// because they are stored in localStorage within the context of the extension (on the frontend).
62
65
// Instead it relies on the extension to pass preferences through.
63
66
// Because we might be in a sandboxed iframe, we have to ask for them by way of postMessage().
67
+ // TODO WHAT HUH
64
68
parent . postMessage ( { type : MESSAGE_TYPE_GET_SAVED_PREFERENCES } , '*' ) ;
65
69
}
66
70
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
-
71
+ function finishActivation ( contentWindow : window , bridge : BackendBridge ) {
85
72
const agent = new Agent ( bridge ) ;
86
73
87
74
const hook = contentWindow . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
@@ -100,8 +87,45 @@ function finishActivation(contentWindow: window) {
100
87
}
101
88
}
102
89
103
- export function activate ( contentWindow : window ) : void {
104
- startActivation ( contentWindow ) ;
90
+ export function activate (
91
+ contentWindow : window ,
92
+ {
93
+ bridge,
94
+ } : { |
95
+ bridge ?: BackendBridge ,
96
+ | } = { } ,
97
+ ) : void {
98
+ if ( bridge == null ) {
99
+ bridge = createBridge ( contentWindow ) ;
100
+ }
101
+
102
+ startActivation ( contentWindow , bridge ) ;
103
+ }
104
+
105
+ export function createBridge (
106
+ contentWindow : window ,
107
+ wall ?: Wall ,
108
+ ) : BackendBridge {
109
+ const { parent} = contentWindow ;
110
+
111
+ if ( wall == null ) {
112
+ wall = {
113
+ listen ( fn ) {
114
+ const onMessage = ( { data} ) => {
115
+ fn ( data ) ;
116
+ } ;
117
+ window . addEventListener ( 'message' , onMessage ) ;
118
+ return ( ) => {
119
+ window . removeEventListener ( 'message' , onMessage ) ;
120
+ } ;
121
+ } ,
122
+ send ( event : string , payload : any , transferable ?: Array < any > ) {
123
+ parent. postMessage ( { event, payload} , '*' , transferable ) ;
124
+ } ,
125
+ } ;
126
+ }
127
+
128
+ return ( new Bridge ( wall ) : BackendBridge ) ;
105
129
}
106
130
107
131
export function initialize ( contentWindow : window ) : void {
0 commit comments