Skip to content

Commit 1681ced

Browse files
committed
refactor[Agent/Store]: Store to send messages only after Agent is initialized
1 parent 66cf2cf commit 1681ced

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

packages/react-devtools-shared/src/backend/agent.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,16 @@ export default class Agent extends EventEmitter<{
230230
bridge.addListener('overrideProps', this.overrideProps);
231231
bridge.addListener('overrideState', this.overrideState);
232232

233+
setupHighlighter(bridge, this);
234+
setupTraceUpdates(this);
235+
236+
// By this time, Store should already be initialized and intercept events
237+
bridge.send('backendInitialized');
238+
233239
if (this._isProfiling) {
234240
bridge.send('profilingStatus', true);
235241
}
236242

237-
// Send the Bridge protocol and backend versions, after initialization, in case the frontend has already requested it.
238-
// The Store may be instantiated beore the agent.
239-
const version = process.env.DEVTOOLS_VERSION;
240-
if (version) {
241-
this._bridge.send('backendVersion', version);
242-
}
243-
this._bridge.send('bridgeProtocol', currentBridgeProtocol);
244-
245243
// Notify the frontend if the backend supports the Storage API (e.g. localStorage).
246244
// If not, features like reload-and-profile will not work correctly and must be disabled.
247245
let isBackendStorageAPISupported = false;
@@ -251,9 +249,6 @@ export default class Agent extends EventEmitter<{
251249
} catch (error) {}
252250
bridge.send('isBackendStorageAPISupported', isBackendStorageAPISupported);
253251
bridge.send('isSynchronousXHRSupported', isSynchronousXHRSupported());
254-
255-
setupHighlighter(bridge, this);
256-
setupTraceUpdates(this);
257252
}
258253

259254
get rendererInterfaces(): {[key: RendererID]: RendererInterface, ...} {

packages/react-devtools-shared/src/devtools/store.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ export default class Store extends EventEmitter<{
191191
// Used for windowing purposes.
192192
_weightAcrossRoots: number = 0;
193193

194+
_shouldCheckBridgeProtocolCompatibility: boolean = false;
195+
194196
constructor(bridge: FrontendBridge, config?: Config) {
195197
super();
196198

@@ -218,6 +220,7 @@ export default class Store extends EventEmitter<{
218220
supportsReloadAndProfile,
219221
supportsTimeline,
220222
supportsTraceUpdates,
223+
checkBridgeProtocolCompatibility,
221224
} = config;
222225
if (supportsInspectMatchingDOMElement) {
223226
this._supportsInspectMatchingDOMElement = true;
@@ -234,6 +237,9 @@ export default class Store extends EventEmitter<{
234237
if (supportsTraceUpdates) {
235238
this._supportsTraceUpdates = true;
236239
}
240+
if (checkBridgeProtocolCompatibility) {
241+
this._shouldCheckBridgeProtocolCompatibility = true;
242+
}
237243
}
238244

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

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

265-
// Verify that the frontend version is compatible with the connected backend.
266-
// See github.com/facebook/react/issues/21326
267-
if (config != null && config.checkBridgeProtocolCompatibility) {
268-
// Older backends don't support an explicit bridge protocol,
269-
// so we should timeout eventually and show a downgrade message.
270-
this._onBridgeProtocolTimeoutID = setTimeout(
271-
this.onBridgeProtocolTimeout,
272-
10000,
273-
);
274-
275-
bridge.addListener('bridgeProtocol', this.onBridgeProtocol);
276-
bridge.send('getBridgeProtocol');
277-
}
278-
279271
bridge.addListener('backendVersion', this.onBridgeBackendVersion);
280-
bridge.send('getBackendVersion');
281-
282272
bridge.addListener('saveToClipboard', this.onSaveToClipboard);
273+
bridge.addListener('backendInitialized', this.onBackendInitialized);
283274
}
284275

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

1487+
onBackendInitialized: () => void = () => {
1488+
// Verify that the frontend version is compatible with the connected backend.
1489+
// See github.com/facebook/react/issues/21326
1490+
if (this._shouldCheckBridgeProtocolCompatibility) {
1491+
// Older backends don't support an explicit bridge protocol,
1492+
// so we should timeout eventually and show a downgrade message.
1493+
this._onBridgeProtocolTimeoutID = setTimeout(
1494+
this.onBridgeProtocolTimeout,
1495+
10000,
1496+
);
1497+
1498+
bridge.addListener('bridgeProtocol', this.onBridgeProtocol);
1499+
bridge.send('getBridgeProtocol');
1500+
}
1501+
1502+
bridge.send('getBackendVersion');
1503+
};
1504+
14961505
// The Store should never throw an Error without also emitting an event.
14971506
// Otherwise Store errors will be invisible to users,
14981507
// but the downstream errors they cause will be reported as bugs.

0 commit comments

Comments
 (0)