mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
1d8d12005f
Based on https://github.com/facebook/react/pull/31049, credits to @EdmondChuiHW. What is happening here: 1. Once Agent is destroyed, unsubscribe own listeners and bridge listeners. 2. [Browser extension only] Once Agent is destroyed, unsubscribe listeners from BackendManager. 3. [Browser extension only] I've discovered that `backendManager.js` content script can get injected multiple times by the browser. When Frontend is initializing, it will create Store first, and then execute a content script for bootstraping backend manager. If Frontend was destroyed somewhere between these 2 steps, Backend won't be notified, because it is not initialized yet, so it will not unsubscribe listeners correctly. We might end up duplicating listeners, and the next time Frontend is launched, it will report an issues "Cannot add / remove node ...", because same operations are emitted twice. To reproduce 3 you can do the following: 1. Click reload-to-profile 2. Right after when both app and Chrome DevTools panel are reloaded, close Chrome DevTools. 3. Open Chrome DevTools again, open Profiler panel and observe "Cannot add / remove node ..." error in the UI.
103 lines
2.7 KiB
JavaScript
103 lines
2.7 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import Agent from './agent';
|
|
|
|
import type {DevToolsHook, RendererID, RendererInterface} from './types';
|
|
|
|
export type InitBackend = typeof initBackend;
|
|
|
|
export function initBackend(
|
|
hook: DevToolsHook,
|
|
agent: Agent,
|
|
global: Object,
|
|
isReloadAndProfileSupported: boolean,
|
|
): () => void {
|
|
if (hook == null) {
|
|
// DevTools didn't get injected into this page (maybe b'c of the contentType).
|
|
return () => {};
|
|
}
|
|
|
|
function registerRendererInterface(
|
|
id: RendererID,
|
|
rendererInterface: RendererInterface,
|
|
) {
|
|
agent.registerRendererInterface(id, rendererInterface);
|
|
|
|
// Now that the Store and the renderer interface are connected,
|
|
// it's time to flush the pending operation codes to the frontend.
|
|
rendererInterface.flushInitialOperations();
|
|
}
|
|
|
|
const subs = [
|
|
hook.sub(
|
|
'renderer-attached',
|
|
({
|
|
id,
|
|
rendererInterface,
|
|
}: {
|
|
id: number,
|
|
rendererInterface: RendererInterface,
|
|
}) => {
|
|
registerRendererInterface(id, rendererInterface);
|
|
},
|
|
),
|
|
hook.sub('unsupported-renderer-version', () => {
|
|
agent.onUnsupportedRenderer();
|
|
}),
|
|
|
|
hook.sub('fastRefreshScheduled', agent.onFastRefreshScheduled),
|
|
hook.sub('operations', agent.onHookOperations),
|
|
hook.sub('traceUpdates', agent.onTraceUpdates),
|
|
hook.sub('settingsInitialized', agent.onHookSettings),
|
|
|
|
// TODO Add additional subscriptions required for profiling mode
|
|
];
|
|
|
|
agent.addListener('getIfHasUnsupportedRendererVersion', () => {
|
|
if (hook.hasUnsupportedRendererAttached) {
|
|
agent.onUnsupportedRenderer();
|
|
}
|
|
});
|
|
|
|
hook.rendererInterfaces.forEach((rendererInterface, id) => {
|
|
registerRendererInterface(id, rendererInterface);
|
|
});
|
|
|
|
hook.emit('react-devtools', agent);
|
|
hook.reactDevtoolsAgent = agent;
|
|
|
|
const onAgentShutdown = () => {
|
|
subs.forEach(fn => fn());
|
|
hook.rendererInterfaces.forEach(rendererInterface => {
|
|
rendererInterface.cleanup();
|
|
});
|
|
hook.reactDevtoolsAgent = null;
|
|
};
|
|
|
|
// Agent's event listeners are cleaned up by Agent in `shutdown` implementation.
|
|
agent.addListener('shutdown', onAgentShutdown);
|
|
agent.addListener('updateHookSettings', settings => {
|
|
hook.settings = settings;
|
|
});
|
|
agent.addListener('getHookSettings', () => {
|
|
if (hook.settings != null) {
|
|
agent.onHookSettings(hook.settings);
|
|
}
|
|
});
|
|
|
|
if (isReloadAndProfileSupported) {
|
|
agent.onReloadAndProfileSupportedByHost();
|
|
}
|
|
|
|
return () => {
|
|
subs.forEach(fn => fn());
|
|
};
|
|
}
|