-
Notifications
You must be signed in to change notification settings - Fork 50.2k
DevTools: Support mulitple DevTools instances per page #22949
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,10 @@ import { | |
| MESSAGE_TYPE_SAVED_PREFERENCES, | ||
| } from './constants'; | ||
|
|
||
| function startActivation(contentWindow: window) { | ||
| import type {BackendBridge} from 'react-devtools-shared/src/bridge'; | ||
| import type {Wall} from 'react-devtools-shared/src/types'; | ||
|
|
||
| function startActivation(contentWindow: window, bridge: BackendBridge) { | ||
| const {parent} = contentWindow; | ||
|
|
||
| const onMessage = ({data}) => { | ||
|
|
@@ -48,7 +51,7 @@ function startActivation(contentWindow: window) { | |
| window.__REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = hideConsoleLogsInStrictMode; | ||
| } | ||
|
|
||
| finishActivation(contentWindow); | ||
| finishActivation(contentWindow, bridge); | ||
| break; | ||
| default: | ||
| break; | ||
|
|
@@ -61,27 +64,11 @@ function startActivation(contentWindow: window) { | |
| // because they are stored in localStorage within the context of the extension (on the frontend). | ||
| // Instead it relies on the extension to pass preferences through. | ||
| // Because we might be in a sandboxed iframe, we have to ask for them by way of postMessage(). | ||
| // TODO WHAT HUH | ||
|
||
| parent.postMessage({type: MESSAGE_TYPE_GET_SAVED_PREFERENCES}, '*'); | ||
| } | ||
|
|
||
| function finishActivation(contentWindow: window) { | ||
| const {parent} = contentWindow; | ||
|
|
||
| const bridge = new Bridge({ | ||
| listen(fn) { | ||
| const onMessage = event => { | ||
| fn(event.data); | ||
| }; | ||
| contentWindow.addEventListener('message', onMessage); | ||
| return () => { | ||
| contentWindow.removeEventListener('message', onMessage); | ||
| }; | ||
| }, | ||
| send(event: string, payload: any, transferable?: Array<any>) { | ||
| parent.postMessage({event, payload}, '*', transferable); | ||
| }, | ||
| }); | ||
|
|
||
| function finishActivation(contentWindow: window, bridge: BackendBridge) { | ||
bvaughn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const agent = new Agent(bridge); | ||
|
|
||
| const hook = contentWindow.__REACT_DEVTOOLS_GLOBAL_HOOK__; | ||
|
|
@@ -100,8 +87,45 @@ function finishActivation(contentWindow: window) { | |
| } | ||
| } | ||
|
|
||
| export function activate(contentWindow: window): void { | ||
| startActivation(contentWindow); | ||
| export function activate( | ||
| contentWindow: window, | ||
| { | ||
| bridge, | ||
| }: {| | ||
| bridge?: BackendBridge, | ||
| |} = {}, | ||
| ): void { | ||
| if (bridge == null) { | ||
| bridge = createBridge(contentWindow); | ||
| } | ||
|
|
||
| startActivation(contentWindow, bridge); | ||
| } | ||
|
|
||
| export function createBridge( | ||
| contentWindow: window, | ||
| wall?: Wall, | ||
| ): BackendBridge { | ||
| const {parent} = contentWindow; | ||
|
|
||
| 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>) { | ||
| parent.postMessage({event, payload}, '*', transferable); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| return (new Bridge(wall): BackendBridge); | ||
| } | ||
|
|
||
| export function initialize(contentWindow: window): void { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf8"> | ||
| <title>React DevTools</title> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | ||
| <style> | ||
| * { | ||
| box-sizing: border-box; | ||
| } | ||
| body { | ||
| display: flex; | ||
| flex-direction: row; | ||
| position: absolute; | ||
| top: 0; | ||
| left: 0; | ||
| right: 0; | ||
| bottom: 0; | ||
| margin: 0; | ||
| padding: 0; | ||
| font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, | ||
| sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol; | ||
| font-size: 12px; | ||
| line-height: 1.5; | ||
| } | ||
| .column { | ||
| display: flex; | ||
| flex-direction: column; | ||
| flex: 1 1 50%; | ||
| } | ||
| .column:first-of-type { | ||
| border-right: 1px solid #3d424a; | ||
| } | ||
| .iframe { | ||
| height: 50%; | ||
| flex: 0 0 50%; | ||
| border: none; | ||
| } | ||
| .devtools { | ||
| height: 50%; | ||
| flex: 0 0 50%; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div class="column left-column"> | ||
| <iframe id="iframe-left" class="iframe"></iframe> | ||
| <div id="devtools-left" class="devtools"></div> | ||
| </div> | ||
| <div class="column"> | ||
| <iframe id="iframe-right" class="iframe"></iframe> | ||
| <div id="devtools-right" class="devtools"></div> | ||
| </div> | ||
|
|
||
| <script src="dist/multi-devtools.js"></script> | ||
| </body> | ||
| </html> |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import * as React from 'react'; | ||
| import {createRoot} from 'react-dom'; | ||
| import { | ||
| activate as activateBackend, | ||
| createBridge as createBackendBridge, | ||
| initialize as initializeBackend, | ||
| } from 'react-devtools-inline/backend'; | ||
| import { | ||
| createBridge as createFrontendBridge, | ||
| createStore, | ||
| initialize as createDevTools, | ||
| } from 'react-devtools-inline/frontend'; | ||
| import {__DEBUG__} from 'react-devtools-shared/src/constants'; | ||
|
|
||
| function inject(contentDocument, sourcePath, callback) { | ||
| const script = contentDocument.createElement('script'); | ||
| script.onload = callback; | ||
| script.src = sourcePath; | ||
|
|
||
| ((contentDocument.body: any): HTMLBodyElement).appendChild(script); | ||
| } | ||
|
|
||
| function init(appIframe, devtoolsContainer, appSource) { | ||
| const {contentDocument, contentWindow} = appIframe; | ||
|
|
||
| // Wire each DevTools instance directly to its app. | ||
| // By default, DevTools dispatches "message" events on the window, | ||
| // but this means that only one instance of DevTools can live on a page. | ||
| const wall = { | ||
| _listeners: [], | ||
| listen(listener) { | ||
| if (__DEBUG__) { | ||
| console.log('[Shell] Wall.listen()'); | ||
| } | ||
|
|
||
| wall._listeners.push(listener); | ||
| }, | ||
| send(event, payload) { | ||
| if (__DEBUG__) { | ||
| console.log('[Shell] Wall.send()', {event, payload}); | ||
| } | ||
|
|
||
| wall._listeners.forEach(listener => listener({event, payload})); | ||
| }, | ||
| }; | ||
|
|
||
| const backendBridge = createBackendBridge(contentWindow, wall); | ||
|
|
||
| initializeBackend(contentWindow); | ||
|
|
||
| const frontendBridge = createFrontendBridge(contentWindow, wall); | ||
| const store = createStore(frontendBridge); | ||
| const DevTools = createDevTools(contentWindow, { | ||
| bridge: frontendBridge, | ||
| store, | ||
| }); | ||
|
|
||
| inject(contentDocument, appSource, () => { | ||
| createRoot(devtoolsContainer).render(<DevTools />); | ||
| }); | ||
|
|
||
| activateBackend(contentWindow, {bridge: backendBridge}); | ||
| } | ||
|
|
||
| const appIframeLeft = document.getElementById('iframe-left'); | ||
| const appIframeRight = document.getElementById('iframe-right'); | ||
| const devtoolsContainerLeft = document.getElementById('devtools-left'); | ||
| const devtoolsContainerRight = document.getElementById('devtools-right'); | ||
|
|
||
| init(appIframeLeft, devtoolsContainerLeft, 'dist/multi-left.js'); | ||
| init(appIframeRight, devtoolsContainerRight, 'dist/multi-right.js'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import * as React from 'react'; | ||
| import {useState} from 'react'; | ||
| import {createRoot} from 'react-dom'; | ||
|
|
||
| function createContainer() { | ||
| const container = document.createElement('div'); | ||
|
|
||
| ((document.body: any): HTMLBodyElement).appendChild(container); | ||
|
|
||
| return container; | ||
| } | ||
|
|
||
| function StatefulCounter() { | ||
| const [count, setCount] = useState(0); | ||
| const handleClick = () => setCount(count + 1); | ||
| return <button onClick={handleClick}>Count {count}</button>; | ||
| } | ||
|
|
||
| createRoot(createContainer()).render(<StatefulCounter />); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import * as React from 'react'; | ||
| import {useLayoutEffect, useRef, useState} from 'react'; | ||
| import {render} from 'react-dom'; | ||
|
|
||
| function createContainer() { | ||
| const container = document.createElement('div'); | ||
|
|
||
| ((document.body: any): HTMLBodyElement).appendChild(container); | ||
|
|
||
| return container; | ||
| } | ||
|
|
||
| function EffectWithState() { | ||
| const [didMount, setDidMount] = useState(0); | ||
|
|
||
| const renderCountRef = useRef(0); | ||
| renderCountRef.current++; | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (!didMount) { | ||
| setDidMount(true); | ||
| } | ||
| }, [didMount]); | ||
|
|
||
| return ( | ||
| <ul> | ||
| <li>Rendered {renderCountRef.current} times</li> | ||
| {didMount && <li>Mounted!</li>} | ||
| </ul> | ||
| ); | ||
| } | ||
|
|
||
| render(<EffectWithState />, createContainer()); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.