Skip to content

Commit 301abfe

Browse files
authored
fix(network-activity-plugin): race condition when reloading the app (#235)
## Summary - Adds a `recording-state` event sent by the React Native side to the DevTools UI on every new connection, broadcasting the current recording state. - The DevTools UI listens for this event and re-syncs its recording toggle by sending `network-enable` or `network-disable` if the states diverge. - This fixes a race condition where reloading the app could leave the UI and the native side out of sync (e.g. UI shows recording enabled but native has reset to disabled). ## Test plan - [ ] Start the app with network recording enabled, reload the app, and verify the DevTools UI stays in sync with the native recording state. - [ ] Toggle recording off, reload the app, and confirm the UI reflects the disabled state. - [ ] Verify no regressions in normal network recording start/stop flow.
1 parent 0b373c7 commit 301abfe

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

packages/network-activity-plugin/src/react-native/useNetworkActivityDevTools.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ export const useNetworkActivityDevTools = (
9999
}),
100100
];
101101

102+
// Inform the DevTools UI of the current recording state so it can detect
103+
// and resolve desynchronization (e.g. after an app reload)
104+
client.send('recording-state', {
105+
isRecording: isRecordingEnabledRef.current,
106+
});
107+
102108
// Send initial or changed values live
103109
sendClientUISettings();
104110

packages/network-activity-plugin/src/shared/client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export type NetworkActivityEventMap = {
1414
'network-enable': unknown;
1515
'network-disable': unknown;
1616

17+
// Recording state sync (sent by React Native to inform DevTools UI of current state)
18+
'recording-state': { isRecording: boolean };
19+
1720
// Client UI settings events
1821
'get-client-ui-settings': unknown;
1922
'client-ui-settings': {

packages/network-activity-plugin/src/ui/state/store.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ export const createNetworkActivityStore = () =>
123123
data: NetworkActivityEventMap[K],
124124
) => {
125125
switch (eventType) {
126+
case 'recording-state': {
127+
const eventData =
128+
data as NetworkActivityEventMap['recording-state'];
129+
const { isRecording, _client } = get();
130+
if (_client && isRecording !== eventData.isRecording) {
131+
_client.send(isRecording ? 'network-enable' : 'network-disable', {});
132+
}
133+
break;
134+
}
135+
126136
case 'client-ui-settings': {
127137
const eventData =
128138
data as NetworkActivityEventMap['client-ui-settings'];
@@ -583,6 +593,9 @@ export const createNetworkActivityStore = () =>
583593

584594
// Subscribe to all events using the unified handler
585595
const unsubscribeFunctions = [
596+
client.onMessage('recording-state', (data) =>
597+
handleEvent('recording-state', data)
598+
),
586599
client.onMessage('client-ui-settings', (data) =>
587600
handleEvent('client-ui-settings', data),
588601
),

0 commit comments

Comments
 (0)