Skip to content

Commit 6ef7062

Browse files
try and fix issue with client disconnects
Signed-off-by: Jonathan Irvin <djfoxyslpr@gmail.com>
1 parent c22cc0e commit 6ef7062

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

main.py

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
# Keys can be "home" and "stream". Each value is a tuple: (container, tile_buttons).
5151
board_views = {}
5252

53+
# We won't try to track active clients as it's not reliable across all NiceGUI versions
5354
board_iteration = 1
5455

5556
# Global set to track winning patterns (rows, columns, & diagonals)
@@ -321,9 +322,24 @@ def check_winner():
321322
ui.notify(sp_message, color="blue", duration=5)
322323

323324
def sync_board_state():
324-
# Update tile styles in every board view (e.g., home and stream)
325-
for view_key, (container, tile_buttons_local) in board_views.items():
326-
update_tile_styles(tile_buttons_local)
325+
"""
326+
Update tile styles in every board view (e.g., home and stream).
327+
"""
328+
try:
329+
# Update tile styles in every board view (e.g., home and stream)
330+
for view_key, (container, tile_buttons_local) in board_views.items():
331+
update_tile_styles(tile_buttons_local)
332+
333+
# Safely run JavaScript
334+
try:
335+
ui.run_javascript(
336+
"fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });"
337+
"fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });"
338+
)
339+
except Exception as e:
340+
logging.debug(f"JavaScript execution failed (likely disconnected client): {e}")
341+
except Exception as e:
342+
logging.debug(f"Error in sync_board_state: {e}")
327343

328344
def create_board_view(background_color: str, is_global: bool):
329345
"""
@@ -334,20 +350,30 @@ def create_board_view(background_color: str, is_global: bool):
334350
setup_head(background_color)
335351
# Create the board container. For the home view, assign an ID to capture it.
336352
if is_global:
337-
container = ui.element("div").classes("home-board-container flex justify-center items-center w-full")
338-
ui.run_javascript("document.querySelector('.home-board-container').id = 'board-container'")
353+
container = ui.element("div").classes("home-board-container flex justify-center items-center w-full")
354+
try:
355+
ui.run_javascript("document.querySelector('.home-board-container').id = 'board-container'")
356+
except Exception as e:
357+
logging.debug(f"Setting board container ID failed: {e}")
339358
else:
340-
container = ui.element("div").classes("stream-board-container flex justify-center items-center w-full")
341-
ui.run_javascript("document.querySelector('.stream-board-container').id = 'board-container-stream'")
359+
container = ui.element("div").classes("stream-board-container flex justify-center items-center w-full")
360+
try:
361+
ui.run_javascript("document.querySelector('.stream-board-container').id = 'board-container-stream'")
362+
except Exception as e:
363+
logging.debug(f"Setting stream container ID failed: {e}")
342364

343365
if is_global:
344366
global home_board_container, tile_buttons, seed_label
345367
home_board_container = container
346368
tile_buttons = {} # Start with an empty dictionary.
347369
build_board(container, tile_buttons, toggle_tile)
348370
board_views["home"] = (container, tile_buttons)
349-
# Add timers for synchronizing the global board.
350-
ui.timer(1, check_phrases_file_change)
371+
# Add timers for synchronizing the global board
372+
try:
373+
check_timer = ui.timer(1, check_phrases_file_change)
374+
except Exception as e:
375+
logging.warning(f"Error setting up timer: {e}")
376+
351377
global seed_label
352378
with ui.row().classes("w-full mt-4 items-center justify-center gap-4"):
353379
with ui.button("", icon="refresh", on_click=reset_board).classes("rounded-full w-12 h-12") as reset_btn:
@@ -365,12 +391,20 @@ def create_board_view(background_color: str, is_global: bool):
365391
@ui.page("/")
366392
def home_page():
367393
create_board_view(HOME_BG_COLOR, True)
368-
ui.timer(0.1, sync_board_state)
394+
try:
395+
# Create a timer that deactivates when the client disconnects
396+
timer = ui.timer(0.1, sync_board_state)
397+
except Exception as e:
398+
logging.warning(f"Error creating timer: {e}")
369399

370400
@ui.page("/stream")
371401
def stream_page():
372402
create_board_view(STREAM_BG_COLOR, False)
373-
ui.timer(0.1, sync_board_state)
403+
try:
404+
# Create a timer that deactivates when the client disconnects
405+
timer = ui.timer(0.1, sync_board_state)
406+
except Exception as e:
407+
logging.warning(f"Error creating timer: {e}")
374408

375409
def setup_head(background_color: str):
376410
"""
@@ -532,10 +566,15 @@ def update_tile_styles(tile_buttons_dict: dict):
532566
# Update inline style (which may now use a new color due to tile click state).
533567
lbl.style(new_label_style)
534568
lbl.update()
535-
ui.run_javascript(
536-
"fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });"
537-
"fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });"
538-
)
569+
570+
# Safely run JavaScript
571+
try:
572+
ui.run_javascript(
573+
"fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });"
574+
"fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });"
575+
)
576+
except Exception as e:
577+
logging.debug(f"JavaScript execution failed (likely disconnected client): {e}")
539578

540579
def check_phrases_file_change():
541580
"""
@@ -589,10 +628,15 @@ def has_too_many_repeats(phrase, threshold=0.5):
589628
tile_buttons_local.clear() # Clear local board dictionary.
590629
build_board(container, tile_buttons_local, toggle_tile)
591630
container.update() # Force update so new styles are applied immediately.
592-
ui.run_javascript(
593-
"fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });"
594-
"fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });"
595-
)
631+
632+
# Safely run JavaScript
633+
try:
634+
ui.run_javascript(
635+
"fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });"
636+
"fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });"
637+
)
638+
except Exception as e:
639+
logging.debug(f"JavaScript execution failed (likely disconnected client): {e}")
596640

597641
def reset_board():
598642
"""

0 commit comments

Comments
 (0)