Skip to content

Commit a4bbe6d

Browse files
fix(reactotron-core-ui): Move commandListener callbacks outside of render cycle (#1544 by @jamonholmgren)
[skip ci]
1 parent 7e2fe7a commit a4bbe6d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

apps/reactotron-app/src/renderer/contexts/Standalone/useStandalone.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,15 @@ export function reducer(state: State, action: Action) {
135135
return
136136
}
137137

138-
draftState.commandListeners.forEach((cl) => cl(action.payload))
139-
140138
const connection = draftState.connections.find(
141139
(c) => c.clientId === action.payload.clientId
142140
)
143141

142+
if (!connection) {
143+
console.error("Command received for unknown connection", action.payload)
144+
return
145+
}
146+
144147
connection.commands = [action.payload, ...connection.commands]
145148
})
146149
case ActionTypes.ClearConnectionCommands:
@@ -205,9 +208,16 @@ function useStandalone() {
205208
}, [])
206209

207210
// Called when commands are flowing in.
208-
const commandReceived = useCallback((command: any) => {
209-
dispatch({ type: ActionTypes.CommandReceived, payload: command })
210-
}, [])
211+
const commandReceived = useCallback(
212+
(command: any) => {
213+
// First dispatch to update state
214+
dispatch({ type: ActionTypes.CommandReceived, payload: command })
215+
216+
// Then notify listeners
217+
state.commandListeners.forEach((cl) => cl(command))
218+
},
219+
[state.commandListeners]
220+
)
211221

212222
// Called when a client disconnects. NOTE: They could be coming back. This could happen with a reload of the simulator!
213223
const connectionDisconnected = useCallback((connection: ReactotronConnection) => {

0 commit comments

Comments
 (0)