Skip to content

Commit

Permalink
refactor[Agent/Store]: Store to send messages only after Agent is ini…
Browse files Browse the repository at this point in the history
…tialized
  • Loading branch information
hoxyq committed Sep 11, 2024
1 parent 66cf2cf commit 1681ced
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
17 changes: 6 additions & 11 deletions packages/react-devtools-shared/src/backend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,16 @@ export default class Agent extends EventEmitter<{
bridge.addListener('overrideProps', this.overrideProps);
bridge.addListener('overrideState', this.overrideState);

setupHighlighter(bridge, this);
setupTraceUpdates(this);

// By this time, Store should already be initialized and intercept events
bridge.send('backendInitialized');

if (this._isProfiling) {
bridge.send('profilingStatus', true);
}

// Send the Bridge protocol and backend versions, after initialization, in case the frontend has already requested it.
// The Store may be instantiated beore the agent.
const version = process.env.DEVTOOLS_VERSION;
if (version) {
this._bridge.send('backendVersion', version);
}
this._bridge.send('bridgeProtocol', currentBridgeProtocol);

// Notify the frontend if the backend supports the Storage API (e.g. localStorage).
// If not, features like reload-and-profile will not work correctly and must be disabled.
let isBackendStorageAPISupported = false;
Expand All @@ -251,9 +249,6 @@ export default class Agent extends EventEmitter<{
} catch (error) {}
bridge.send('isBackendStorageAPISupported', isBackendStorageAPISupported);
bridge.send('isSynchronousXHRSupported', isSynchronousXHRSupported());

setupHighlighter(bridge, this);
setupTraceUpdates(this);
}

get rendererInterfaces(): {[key: RendererID]: RendererInterface, ...} {
Expand Down
41 changes: 25 additions & 16 deletions packages/react-devtools-shared/src/devtools/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ export default class Store extends EventEmitter<{
// Used for windowing purposes.
_weightAcrossRoots: number = 0;

_shouldCheckBridgeProtocolCompatibility: boolean = false;

constructor(bridge: FrontendBridge, config?: Config) {
super();

Expand Down Expand Up @@ -218,6 +220,7 @@ export default class Store extends EventEmitter<{
supportsReloadAndProfile,
supportsTimeline,
supportsTraceUpdates,
checkBridgeProtocolCompatibility,
} = config;
if (supportsInspectMatchingDOMElement) {
this._supportsInspectMatchingDOMElement = true;
Expand All @@ -234,6 +237,9 @@ export default class Store extends EventEmitter<{
if (supportsTraceUpdates) {
this._supportsTraceUpdates = true;
}
if (checkBridgeProtocolCompatibility) {
this._shouldCheckBridgeProtocolCompatibility = true;
}
}

this._bridge = bridge;
Expand Down Expand Up @@ -262,24 +268,9 @@ export default class Store extends EventEmitter<{

this._profilerStore = new ProfilerStore(bridge, this, isProfiling);

// Verify that the frontend version is compatible with the connected backend.
// See github.com/facebook/react/issues/21326
if (config != null && config.checkBridgeProtocolCompatibility) {
// Older backends don't support an explicit bridge protocol,
// so we should timeout eventually and show a downgrade message.
this._onBridgeProtocolTimeoutID = setTimeout(
this.onBridgeProtocolTimeout,
10000,
);

bridge.addListener('bridgeProtocol', this.onBridgeProtocol);
bridge.send('getBridgeProtocol');
}

bridge.addListener('backendVersion', this.onBridgeBackendVersion);
bridge.send('getBackendVersion');

bridge.addListener('saveToClipboard', this.onSaveToClipboard);
bridge.addListener('backendInitialized', this.onBackendInitialized);
}

// This is only used in tests to avoid memory leaks.
Expand Down Expand Up @@ -1493,6 +1484,24 @@ export default class Store extends EventEmitter<{
copy(text);
};

onBackendInitialized: () => void = () => {
// Verify that the frontend version is compatible with the connected backend.
// See github.com/facebook/react/issues/21326
if (this._shouldCheckBridgeProtocolCompatibility) {
// Older backends don't support an explicit bridge protocol,
// so we should timeout eventually and show a downgrade message.
this._onBridgeProtocolTimeoutID = setTimeout(
this.onBridgeProtocolTimeout,
10000,
);

bridge.addListener('bridgeProtocol', this.onBridgeProtocol);

Check failure on line 1498 in packages/react-devtools-shared/src/devtools/store.js

View workflow job for this annotation

GitHub Actions / Run eslint

'bridge' is not defined
bridge.send('getBridgeProtocol');

Check failure on line 1499 in packages/react-devtools-shared/src/devtools/store.js

View workflow job for this annotation

GitHub Actions / Run eslint

'bridge' is not defined
}

bridge.send('getBackendVersion');

Check failure on line 1502 in packages/react-devtools-shared/src/devtools/store.js

View workflow job for this annotation

GitHub Actions / Run eslint

'bridge' is not defined
};

// The Store should never throw an Error without also emitting an event.
// Otherwise Store errors will be invisible to users,
// but the downstream errors they cause will be reported as bugs.
Expand Down

0 comments on commit 1681ced

Please sign in to comment.