Files
react/packages/react-devtools-extensions/src/contentScripts/installHook.js
T
Ruslan Lesiutin bfe91fbecf refactor[react-devtools]: flatten reload and profile config (#31132)
Stacked on https://github.com/facebook/react/pull/31131. See last
commit.

This is a clean-up and a pre-requisite for next changes:
1. `ReloadAndProfileConfig` is now split into boolean value and settings
object. This is mainly because I will add one more setting soon, and
also because settings might be persisted for a longer time than the flag
which signals if the Backend was reloaded for profiling. Ideally, this
settings should probably be moved to the global Hook object, same as we
did for console patching.
2. Host is now responsible for reseting the cached values, Backend will
execute provided `onReloadAndProfileFlagsReset` callback.
2024-10-09 13:57:02 +01:00

68 lines
1.8 KiB
JavaScript

import {installHook} from 'react-devtools-shared/src/hook';
import {
getIfReloadedAndProfiling,
getProfilingSettings,
} from 'react-devtools-shared/src/utils';
let resolveHookSettingsInjection;
function messageListener(event: MessageEvent) {
if (event.source !== window) {
return;
}
if (event.data.source === 'react-devtools-hook-settings-injector') {
// In case handshake message was sent prior to hookSettingsInjector execution
// We can't guarantee order
if (event.data.payload.handshake) {
window.postMessage({
source: 'react-devtools-hook-installer',
payload: {handshake: true},
});
} else if (event.data.payload.settings) {
window.removeEventListener('message', messageListener);
resolveHookSettingsInjection(event.data.payload.settings);
}
}
}
// Avoid double execution
if (!window.hasOwnProperty('__REACT_DEVTOOLS_GLOBAL_HOOK__')) {
const hookSettingsPromise = new Promise(resolve => {
resolveHookSettingsInjection = resolve;
});
window.addEventListener('message', messageListener);
window.postMessage({
source: 'react-devtools-hook-installer',
payload: {handshake: true},
});
const shouldStartProfiling = getIfReloadedAndProfiling();
const profilingSettings = getProfilingSettings();
// Can't delay hook installation, inject settings lazily
installHook(
window,
hookSettingsPromise,
shouldStartProfiling,
profilingSettings,
);
// Detect React
window.__REACT_DEVTOOLS_GLOBAL_HOOK__.on(
'renderer',
function ({reactBuildType}) {
window.postMessage(
{
source: 'react-devtools-hook',
payload: {
type: 'react-renderer-attached',
reactBuildType,
},
},
'*',
);
},
);
}