-
Notifications
You must be signed in to change notification settings - Fork 47k
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
refactor: allow custom impl of backend realod-to-profile support check #31048
refactor: allow custom impl of backend realod-to-profile support check #31048
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -181,8 +181,7 @@ export type BackendEvents = { | |||
fastRefreshScheduled: [], | |||
getSavedPreferences: [], | |||
inspectedElement: [InspectedElementPayload], | |||
isBackendStorageAPISupported: [boolean], | |||
isSynchronousXHRSupported: [boolean], | |||
isReloadAndProfileSupported: [boolean], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can it be backendSupportsReloadAndProfile
or isReloadAndProfileSupportedByBackend
?
The final version is up to you, I just want it to explicitly mention that this is about backend.
@@ -355,6 +373,7 @@ export function connectWithCustomMessagingProtocol({ | |||
} | |||
|
|||
const agent = new Agent(bridge); | |||
bridge.send('isReloadAndProfileSupported', isReloadAndProfileSupported()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should move it somewhere. Can this actually be an argument on initBackend
?
Notice that you don't have any changes related to the browser extension, this is because it has its own logic for backend initialization:
react/packages/react-devtools-extensions/src/contentScripts/backendManager.js
Lines 99 to 161 in fc4a33e
function activateBackend(version: string, hook: DevToolsHook) { | |
const backend = hook.backends.get(version); | |
if (!backend) { | |
throw new Error(`Could not find backend for version "${version}"`); | |
} | |
const {Agent, Bridge, initBackend, setupNativeStyleEditor} = backend; | |
const bridge = new Bridge({ | |
listen(fn) { | |
const listener = (event: $FlowFixMe) => { | |
if ( | |
event.source !== window || | |
!event.data || | |
event.data.source !== 'react-devtools-content-script' || | |
!event.data.payload | |
) { | |
return; | |
} | |
fn(event.data.payload); | |
}; | |
window.addEventListener('message', listener); | |
return () => { | |
window.removeEventListener('message', listener); | |
}; | |
}, | |
send(event: string, payload: any, transferable?: Array<any>) { | |
window.postMessage( | |
{ | |
source: 'react-devtools-bridge', | |
payload: {event, payload}, | |
}, | |
'*', | |
transferable, | |
); | |
}, | |
}); | |
const agent = new Agent(bridge); | |
agent.addListener('shutdown', () => { | |
// If we received 'shutdown' from `agent`, we assume the `bridge` is already shutting down, | |
// and that caused the 'shutdown' event on the `agent`, so we don't need to call `bridge.shutdown()` here. | |
hook.emit('shutdown'); | |
}); | |
initBackend(hook, agent, window); | |
// Setup React Native style editor if a renderer like react-native-web has injected it. | |
if (typeof setupNativeStyleEditor === 'function' && hook.resolveRNStyle) { | |
setupNativeStyleEditor( | |
bridge, | |
agent, | |
hook.resolveRNStyle, | |
hook.nativeStyleEditorValidAttributes, | |
); | |
} | |
// Let the frontend know that the backend has attached listeners and is ready for messages. | |
// This covers the case of syncing saved values after reloading/navigating while DevTools remain open. | |
bridge.send('extensionBackendInitialized'); | |
// this backend is activated | |
requiredBackends.delete(version); | |
} |
Then, inside initBackend
, we could do something like:
if (isReloadAndProfileSupported()) {
agent.onReloadAndProfileSupportedByHost();
}
and inside Agent
this method would just send this event:
this._bridge.send('isReloadAndProfileSupported', true);
and the only remaining thing is hardcoding true
for initBackend
call in the extension code, which I've referenced above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will allow us to reuse as much as possible, and reduce the fragmentation: instead of having custom logic, like here in react-devtools-core/src/backend.js
, we would pass an argument to initBackend
, which is from react-devtools-shared
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea! Much cleaner to have this logic back into Agent
/initBackend
. Updated
32a04cc
to
19ad6d1
Compare
@@ -94,7 +95,27 @@ export function initBackend( | |||
} | |||
}); | |||
|
|||
const getIsReloadAndProfileSupported = | |||
hook.settings?.getIsReloadAndProfileSupported ?? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't do anything, because you don't store it on the Hook
object, and you don't need to.
Can you add boolean optional parameter to initBackend
here?
react/packages/react-devtools-shared/src/backend/index.js
Lines 16 to 20 in a15bbe1
export function initBackend( | |
hook: DevToolsHook, | |
agent: Agent, | |
global: Object, | |
): () => void { |
Then, anywhere where you are calling initBackend
: browser extension content script, react-devtools-core
, react-devtools-inline
, you will provide this argument.
For browser extension it will be true
by default, for react-devtools-inline
it will be false
(we don't support it), for react-devtools-core
this will be the return of the function isReloadAndProfileSupported
, which is passed from the Host (React Native in our case)
3f6d608
to
9a7cec7
Compare
|
||
export type InitBackend = typeof initBackend; | ||
|
||
export function initBackend( | ||
hook: DevToolsHook, | ||
agent: Agent, | ||
global: Object, | ||
getIsReloadAndProfileSupported: () => boolean = defaultGetIsReloadAndProfileSupported, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be boolean with a default value false
? And then wrappers around it are going to pass just a boolean value, and react-devtools-core
is going to pass getIsReloadAndProfileSupported()
.
With a current implementation, the extension is going to use defaultGetIsReloadAndProfileSupported
, but actually it should just receive a static true
value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't the extension still need to check the local/session storage capabilities, e.g. incognito mode?
Edit: I'm inclined to keep this PR focused on just the refactoring, i.e. keeping existing things working the way it has been, even if it's suboptimal/obsolete. We can follow up and remove the checks in a separate PR as needed
9a7cec7
to
a474aa8
Compare
## Summary Add reload to profile for Fusebox Stacked on #31048. See 6be1977 ## How did you test this change? Test E2E in [D63233256](https://www.internalfb.com/diff/D63233256)
**breaking change for canary users: Bumps peer dependency of React from `19.0.0-rc-778e1ed2-20240926` to `19.0.0-rc-3edc000d-20240926`** [diff facebook/react@778e1ed2...3edc000d](facebook/react@778e1ed...3edc000) <details> <summary>React upstream changes</summary> - facebook/react#31078 - facebook/react#31083 - facebook/react#31079 - facebook/react#31080 - facebook/react#31076 - facebook/react#31021 - facebook/react#31069 - facebook/react#31074 - facebook/react#31073 - facebook/react#31047 - facebook/react#31046 - facebook/react#31045 - facebook/react#31072 - facebook/react#30980 - facebook/react#30463 - facebook/react#30694 - facebook/react#31039 - facebook/react#31048 - facebook/react#31068 </details> --------- Co-authored-by: Josh Story <[email protected]>
…#70560) **breaking change for canary users: Bumps peer dependency of React from `19.0.0-rc-778e1ed2-20240926` to `19.0.0-rc-3edc000d-20240926`** [diff facebook/react@778e1ed2...3edc000d](facebook/react@778e1ed...3edc000) <details> <summary>React upstream changes</summary> - facebook/react#31078 - facebook/react#31083 - facebook/react#31079 - facebook/react#31080 - facebook/react#31076 - facebook/react#31021 - facebook/react#31069 - facebook/react#31074 - facebook/react#31073 - facebook/react#31047 - facebook/react#31046 - facebook/react#31045 - facebook/react#31072 - facebook/react#30980 - facebook/react#30463 - facebook/react#30694 - facebook/react#31039 - facebook/react#31048 - facebook/react#31068 </details> --------- Co-authored-by: Josh Story <[email protected]>
Changes in this release: * Fix React Compiler badging ([poteto](https://github.com/poteto) in [#31196](#31196)) * fix[react-devtools]: fixed timeline profiler tests ([hoxyq](https://github.com/hoxyq) in [#31261](#31261)) * fix[react-devtools]: record timeline data only when supported ([hoxyq](https://github.com/hoxyq) in [#31154](#31154)) * refactor[react-devtools]: flatten reload and profile config ([hoxyq](https://github.com/hoxyq) in [#31132](#31132)) * fix[react-devtools]: remove all listeners when Agent is shutdown ([hoxyq](https://github.com/hoxyq) in [#31151](#31151)) * fix[react-devtools]: removed redundant startProfiling call ([hoxyq](https://github.com/hoxyq) in [#31131](#31131)) * refactor[react-devtools/fiber/renderer]: optimize durations resolution ([hoxyq](https://github.com/hoxyq) in [#31118](#31118)) * fix[react-devtools]: update profiling status before receiving response from backend ([hoxyq](https://github.com/hoxyq) in [#31117](#31117)) * fix[react-devtools]: wrap key string in preformatted text html element ([hoxyq](https://github.com/hoxyq) in [#31153](#31153)) * chore[react-devtools]: drop legacy context tests ([hoxyq](https://github.com/hoxyq) in [#31059](#31059)) * chore[react-devtools]: add legacy mode error message to the ignore list for tests ([hoxyq](https://github.com/hoxyq) in [#31060](#31060)) * fix[react-devtools]: request hook initialization inside http server response ([hoxyq](https://github.com/hoxyq) in [#31102](#31102)) * [Flight] Serialize Server Components Props in DEV ([sebmarkbage](https://github.com/sebmarkbage) in [#31105](#31105)) * Add: reload to profile for Fusebox ([EdmondChuiHW](https://github.com/EdmondChuiHW) in [#31021](#31021)) * refactor: allow custom impl of backend realod-to-profile support check ([EdmondChuiHW](https://github.com/EdmondChuiHW) in [#31048](#31048)) * fix: use public instance in Fiber renderer and expose it from getInspectorDataForViewAtPoint ([hoxyq](https://github.com/hoxyq) in [#31068](#31068))
Summary
In preparation to support reload-to-profile in Fusebox (#31021), we need a way to check capability of different backends, e.g. web vs React Native.
How did you test this change?