Skip to content

Commit 84c06fe

Browse files
author
Brian Vaughn
authored
Add createBridge and createStore exports to react-devtools-inline (for Replay integration) (#21032)
1 parent 686b635 commit 84c06fe

File tree

9 files changed

+91
-25
lines changed

9 files changed

+91
-25
lines changed

packages/react-devtools-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-devtools-core",
3-
"version": "4.11.0",
3+
"version": "4.12.0",
44
"description": "Use react-devtools outside of the browser",
55
"license": "MIT",
66
"main": "./dist/backend.js",

packages/react-devtools-extensions/chrome/manifest.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"manifest_version": 2,
33
"name": "React Developer Tools",
44
"description": "Adds React debugging tools to the Chrome Developer Tools.",
5-
"version": "4.11.0",
6-
"version_name": "4.11.0",
5+
"version": "4.12.0",
6+
"version_name": "4.12.0",
77

88
"minimum_chrome_version": "60",
99

packages/react-devtools-extensions/edge/manifest.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"manifest_version": 2,
33
"name": "React Developer Tools",
44
"description": "Adds React debugging tools to the Microsoft Edge Developer Tools.",
5-
"version": "4.11.0",
6-
"version_name": "4.11.0",
5+
"version": "4.12.0",
6+
"version_name": "4.12.0",
77

88
"minimum_chrome_version": "60",
99

packages/react-devtools-extensions/firefox/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "React Developer Tools",
44
"description": "Adds React debugging tools to the Firefox Developer Tools.",
5-
"version": "4.11.0",
5+
"version": "4.12.0",
66

77
"applications": {
88
"gecko": {

packages/react-devtools-inline/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,40 @@ iframe.onload = () => {
153153
};
154154
```
155155

156+
### Advanced integration with custom "wall"
157+
158+
Below is an example of an advanced integration with a website like [Replay.io](https://replay.io/).
159+
160+
```js
161+
import {
162+
createBridge,
163+
createStore,
164+
initialize as createDevTools,
165+
} from "react-devtools-inline/frontend";
166+
167+
// Custom Wall implementation enables serializing data
168+
// using an API other than window.postMessage()
169+
// For example...
170+
const wall = {
171+
emit() {},
172+
listen(listener) {
173+
wall._listener = listener;
174+
},
175+
async send(event, payload) {
176+
const response = await fetch(...).json();
177+
wall._listener(response);
178+
},
179+
};
180+
181+
// Create a Bridge and Store that use the custom Wall.
182+
const bridge = createBridge(target, wall);
183+
const store = createStore(bridge);
184+
const DevTools = createDevTools(target, { bridge, store });
185+
186+
// Render DevTools with it.
187+
<DevTools {...otherProps} />;
188+
```
189+
156190
## Local development
157191
You can also build and test this package from source.
158192

packages/react-devtools-inline/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-devtools-inline",
3-
"version": "4.11.1",
3+
"version": "4.12.0",
44
"description": "Embed react-devtools within a website",
55
"license": "MIT",
66
"main": "./dist/backend.js",

packages/react-devtools-inline/src/frontend.js

+42-15
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,47 @@ import {
1616
MESSAGE_TYPE_SAVED_PREFERENCES,
1717
} from './constants';
1818

19+
import type {Wall} from 'react-devtools-shared/src/types';
1920
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
2021
import type {Props} from 'react-devtools-shared/src/devtools/views/DevTools';
2122

23+
export function createStore(bridge: FrontendBridge): Store {
24+
return new Store(bridge, {supportsTraceUpdates: true});
25+
}
26+
27+
export function createBridge(
28+
contentWindow: window,
29+
wall?: Wall,
30+
): FrontendBridge {
31+
if (wall == null) {
32+
wall = {
33+
listen(fn) {
34+
const onMessage = ({data}) => {
35+
fn(data);
36+
};
37+
window.addEventListener('message', onMessage);
38+
return () => {
39+
window.removeEventListener('message', onMessage);
40+
};
41+
},
42+
send(event: string, payload: any, transferable?: Array<any>) {
43+
contentWindow.postMessage({event, payload}, '*', transferable);
44+
},
45+
};
46+
}
47+
48+
return (new Bridge(wall): FrontendBridge);
49+
}
50+
2251
export function initialize(
2352
contentWindow: window,
53+
{
54+
bridge,
55+
store,
56+
}: {|
57+
bridge?: FrontendBridge,
58+
store?: Store,
59+
|} = {},
2460
): React.AbstractComponent<Props, mixed> {
2561
const onGetSavedPreferencesMessage = ({data, source}) => {
2662
if (source === 'react-devtools-content-script') {
@@ -54,22 +90,13 @@ export function initialize(
5490

5591
window.addEventListener('message', onGetSavedPreferencesMessage);
5692

57-
const bridge: FrontendBridge = new Bridge({
58-
listen(fn) {
59-
const onMessage = ({data}) => {
60-
fn(data);
61-
};
62-
window.addEventListener('message', onMessage);
63-
return () => {
64-
window.removeEventListener('message', onMessage);
65-
};
66-
},
67-
send(event: string, payload: any, transferable?: Array<any>) {
68-
contentWindow.postMessage({event, payload}, '*', transferable);
69-
},
70-
});
93+
if (bridge == null) {
94+
bridge = createBridge();
95+
}
7196

72-
const store: Store = new Store(bridge, {supportsTraceUpdates: true});
97+
if (store == null) {
98+
store = createStore(bridge);
99+
}
73100

74101
const ForwardRef = forwardRef<Props, mixed>((props, ref) => (
75102
<DevTools ref={ref} bridge={bridge} store={store} {...props} />

packages/react-devtools/CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
<!-- Upcoming changes go here -->
1010
</details>
1111

12+
## 4.12.0 (April 12, 2021)
13+
Although this release is being made for all NPM packages, only the `react-devtools-inline` package contains changes.
14+
#### Feature
15+
* Added `createBridge` and `createStore` exports to the `react-devtools-inline/frontend` entrypoint to support advanced use cases ([bvaughn](https://github.com/bvaughn) in [#21032](https://github.com/facebook/react/pull/21032))
16+
1217
## 4.11.1 (April 11, 2021)
1318
#### Bugfix
14-
* Fixed broken import in `react-devtools-inline` for feature flags file ([bvaughn](https://github.com/bvaughn) in [#21235](https://github.com/facebook/react/issues/21235))
19+
* Fixed broken import in `react-devtools-inline` for feature flags file ([bvaughn](https://github.com/bvaughn) in [#21237](https://github.com/facebook/react/pull/21237))
1520

1621
## 4.11.0 (April 9, 2021)
1722
#### Bugfix

packages/react-devtools/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-devtools",
3-
"version": "4.11.0",
3+
"version": "4.12.0",
44
"description": "Use react-devtools outside of the browser",
55
"license": "MIT",
66
"repository": {
@@ -27,7 +27,7 @@
2727
"electron": "^11.1.0",
2828
"ip": "^1.1.4",
2929
"minimist": "^1.2.3",
30-
"react-devtools-core": "4.11.0",
30+
"react-devtools-core": "4.12.0",
3131
"update-notifier": "^2.1.0"
3232
}
3333
}

0 commit comments

Comments
 (0)