From 0056d94d57a52b3fe5a38b60e6734185c79f2115 Mon Sep 17 00:00:00 2001 From: advo-kat <119153319+advo-kat@users.noreply.github.com> Date: Sun, 29 Jan 2023 04:24:16 +1100 Subject: [PATCH] Reconnect websocket on window focus (#366) --- pynecone/compiler/templates.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pynecone/compiler/templates.py b/pynecone/compiler/templates.py index 19c747b1c..879af88c5 100644 --- a/pynecone/compiler/templates.py +++ b/pynecone/compiler/templates.py @@ -164,8 +164,14 @@ USE_EFFECT = join( " if(!isReady) {{", " return;", " }}", - f" if (!{SOCKET}.current) {{{{", - f" connect({SOCKET}, {{state}}, {{set_state}}, {RESULT}, {SET_RESULT}, {ROUTER}, {EVENT_ENDPOINT})", + " const reconnectSocket = () => {{", + f" {SOCKET}.current.reconnect()", + " }}", + f" if (typeof {SOCKET}.current !== 'undefined') {{{{", + f" if (!{SOCKET}.current) {{{{", + f" window.addEventListener('focus', reconnectSocket)", + f" connect({SOCKET}, {{state}}, {{set_state}}, {RESULT}, {SET_RESULT}, {ROUTER}, {EVENT_ENDPOINT})", + " }}", " }}", " const update = async () => {{", f" if ({RESULT}.{STATE} != null) {{{{",