Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add createBridge and createStore exports to react-devtools-inline (for Replay integration) #21032

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-devtools-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-devtools-core",
"version": "4.11.0",
"version": "4.12.0",
"description": "Use react-devtools outside of the browser",
"license": "MIT",
"main": "./dist/backend.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/react-devtools-extensions/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"manifest_version": 2,
"name": "React Developer Tools",
"description": "Adds React debugging tools to the Chrome Developer Tools.",
"version": "4.11.0",
"version_name": "4.11.0",
"version": "4.12.0",
"version_name": "4.12.0",

"minimum_chrome_version": "60",

Expand Down
4 changes: 2 additions & 2 deletions packages/react-devtools-extensions/edge/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"manifest_version": 2,
"name": "React Developer Tools",
"description": "Adds React debugging tools to the Microsoft Edge Developer Tools.",
"version": "4.11.0",
"version_name": "4.11.0",
"version": "4.12.0",
"version_name": "4.12.0",

"minimum_chrome_version": "60",

Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-extensions/firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "React Developer Tools",
"description": "Adds React debugging tools to the Firefox Developer Tools.",
"version": "4.11.0",
"version": "4.12.0",

"applications": {
"gecko": {
Expand Down
34 changes: 34 additions & 0 deletions packages/react-devtools-inline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,40 @@ iframe.onload = () => {
};
```

### Advanced integration with custom "wall"

Below is an example of an advanced integration with a website like [Replay.io](https://replay.io/).

```js
import {
createBridge,
createStore,
initialize as createDevTools,
} from "react-devtools-inline/frontend";

// Custom Wall implementation enables serializing data
// using an API other than window.postMessage()
// For example...
const wall = {
emit() {},
listen(listener) {
wall._listener = listener;
},
async send(event, payload) {
const response = await fetch(...).json();
wall._listener(response);
},
};

// Create a Bridge and Store that use the custom Wall.
const bridge = createBridge(target, wall);
const store = createStore(bridge);
const DevTools = createDevTools(target, { bridge, store });

// Render DevTools with it.
<DevTools {...otherProps} />;
```

## Local development
You can also build and test this package from source.

Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-inline/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-devtools-inline",
"version": "4.11.1",
"version": "4.12.0",
"description": "Embed react-devtools within a website",
"license": "MIT",
"main": "./dist/backend.js",
Expand Down
57 changes: 42 additions & 15 deletions packages/react-devtools-inline/src/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,47 @@ import {
MESSAGE_TYPE_SAVED_PREFERENCES,
} from './constants';

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

export function createStore(bridge: FrontendBridge): Store {
return new Store(bridge, {supportsTraceUpdates: true});
}

export function createBridge(
contentWindow: window,
wall?: Wall,
): FrontendBridge {
if (wall == null) {
wall = {
listen(fn) {
const onMessage = ({data}) => {
fn(data);
};
window.addEventListener('message', onMessage);
return () => {
window.removeEventListener('message', onMessage);
};
},
send(event: string, payload: any, transferable?: Array<any>) {
contentWindow.postMessage({event, payload}, '*', transferable);
},
};
}

return (new Bridge(wall): FrontendBridge);
}

export function initialize(
contentWindow: window,
{
bridge,
store,
}: {|
bridge?: FrontendBridge,
store?: Store,
|} = {},
): React.AbstractComponent<Props, mixed> {
const onGetSavedPreferencesMessage = ({data, source}) => {
if (source === 'react-devtools-content-script') {
Expand Down Expand Up @@ -54,22 +90,13 @@ export function initialize(

window.addEventListener('message', onGetSavedPreferencesMessage);

const bridge: FrontendBridge = new Bridge({
listen(fn) {
const onMessage = ({data}) => {
fn(data);
};
window.addEventListener('message', onMessage);
return () => {
window.removeEventListener('message', onMessage);
};
},
send(event: string, payload: any, transferable?: Array<any>) {
contentWindow.postMessage({event, payload}, '*', transferable);
},
});
if (bridge == null) {
bridge = createBridge();
}

const store: Store = new Store(bridge, {supportsTraceUpdates: true});
if (store == null) {
store = createStore(bridge);
}

const ForwardRef = forwardRef<Props, mixed>((props, ref) => (
<DevTools ref={ref} bridge={bridge} store={store} {...props} />
Expand Down
7 changes: 6 additions & 1 deletion packages/react-devtools/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
<!-- Upcoming changes go here -->
</details>

## 4.12.0 (April 12, 2021)
Although this release is being made for all NPM packages, only the `react-devtools-inline` package contains changes.
#### Feature
* 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))

## 4.11.1 (April 11, 2021)
#### Bugfix
* 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))
* 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))

## 4.11.0 (April 9, 2021)
#### Bugfix
Expand Down
4 changes: 2 additions & 2 deletions packages/react-devtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-devtools",
"version": "4.11.0",
"version": "4.12.0",
"description": "Use react-devtools outside of the browser",
"license": "MIT",
"repository": {
Expand All @@ -27,7 +27,7 @@
"electron": "^11.1.0",
"ip": "^1.1.4",
"minimist": "^1.2.3",
"react-devtools-core": "4.11.0",
"react-devtools-core": "4.12.0",
"update-notifier": "^2.1.0"
}
}