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

fix[devtools]: fixed duplicated backend activation with multiple renderers #26807

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
1 change: 1 addition & 0 deletions packages/react-devtools-extensions/src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ function setup(hook: ?DevToolsHook) {
initBackend,
setupNativeStyleEditor,
});

hook.emit('devtools-backend-installed', COMPACT_VERSION_NAME);
}
17 changes: 13 additions & 4 deletions packages/react-devtools-extensions/src/backendManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function setup(hook: ?DevToolsHook) {

// register renderers that have already injected themselves.
hook.renderers.forEach(renderer => {
registerRenderer(renderer);
registerRenderer(renderer, hook);
});

// Activate and remove from required all present backends, registered within the hook
Expand All @@ -71,7 +71,7 @@ function setup(hook: ?DevToolsHook) {

// register renderers that inject themselves later.
hook.sub('renderer', ({renderer}) => {
registerRenderer(renderer);
registerRenderer(renderer, hook);
updateRequiredBackends();
});

Expand All @@ -84,19 +84,24 @@ function setup(hook: ?DevToolsHook) {

const requiredBackends = new Set<string>();

function registerRenderer(renderer: ReactRenderer) {
function registerRenderer(renderer: ReactRenderer, hook: DevToolsHook) {
let version = renderer.reconcilerVersion || renderer.version;
if (!hasAssignedBackend(version)) {
version = COMPACT_VERSION_NAME;
}
requiredBackends.add(version);

// Check if required backend is already activated, no need to require again
if (!hook.backends.has(version)) {
requiredBackends.add(version);
}
}

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) {
Expand Down Expand Up @@ -157,6 +162,10 @@ function activateBackend(version: string, hook: DevToolsHook) {

// tell the service worker which versions of backends are needed for the current page
function updateRequiredBackends() {
if (requiredBackends.size === 0) {
return;
}

window.postMessage(
{
source: 'react-devtools-backend-manager',
Expand Down