Skip to content

Commit 82d80cd

Browse files
committed
Do not queue websocket events until the websocket is connected
1 parent 07e03ef commit 82d80cd

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

reflex/.templates/web/utils/state.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,8 @@ export const queueEvents = async (
478478
* @param params The params object from React Router
479479
*/
480480
export const processEvent = async (socket, navigate, params) => {
481-
// Only proceed if the socket is up and no event in the queue uses state, otherwise we throw the event into the void
482-
if (!socket && isStateful()) {
481+
// Only proceed if the socket is up or no event in the queue uses state, otherwise we throw the event into the void
482+
if (isStateful() && !(socket && socket.connected)) {
483483
return;
484484
}
485485

@@ -576,11 +576,13 @@ export const connect = async (
576576
};
577577

578578
// Once the socket is open, hydrate the page.
579-
socket.current.on("connect", () => {
579+
socket.current.on("connect", async () => {
580580
setConnectErrors([]);
581581
window.addEventListener("pagehide", pagehideHandler);
582582
window.addEventListener("beforeunload", disconnectTrigger);
583583
window.addEventListener("unload", disconnectTrigger);
584+
// Drain any initial events from the queue.
585+
await processEvent(socket.current, navigate, () => params.current);
584586
});
585587

586588
socket.current.on("connect_error", (error) => {
@@ -893,7 +895,7 @@ export const useEventLoop = (
893895
// Main event loop.
894896
useEffect(() => {
895897
// Skip if the backend is disabled
896-
if (isBackendDisabled()) {
898+
if (isBackendDisabled() || !socket.current || !socket.current.connected) {
897899
return;
898900
}
899901
(async () => {

0 commit comments

Comments
 (0)