diff --git a/static_src/timer.js b/static_src/timer.js index 8f39ea3558e78c4475a5bd0aaccd86d90bf680b3..6d73c6d0935a6996b93059c0563488dc9acc564e 100644 --- a/static_src/timer.js +++ b/static_src/timer.js @@ -152,19 +152,12 @@ $(window).ready( if ("is_running" in data) { if (data["is_running"]) { - // Reset if we are playing again. + // Don't do anything if we have reached an inconsistent state, + // where the timer is at 0 but server still reports it playing. + // This will be resolved within a few milliseconds. const remainingTime = window.timer.getTimeValues() if (remainingTime.seconds === 0 && remainingTime.minutes === 0) { - window.timer = new Timer({ - countdown: true, - startValues: { - minutes: window.startingTime.minutes, - seconds: window.startingTime.seconds, - } - }) - - assignEventListeners() } window.timer.start() diff --git a/timer/consumers.py b/timer/consumers.py index 3eef521cbed50aac1850577269c1addeba37d93f..057d7c38c2eb1058dc1ef92f18b3098d48c16905 100644 --- a/timer/consumers.py +++ b/timer/consumers.py @@ -11,6 +11,20 @@ from channels.generic.websocket import AsyncWebsocketConsumer running_timer_threads = [] +def handle_expired_timer(timer) -> bool: + if timer.minutes == 0 and timer.seconds == 0: + # Stop the thread if we have reached the end + + timer.is_running = False + timer.save() + + running_timer_threads.remove(timer.id) + + return True + + return False + + def tick_timer(timer, iteration: int, total_seconds: int) -> None: second_compensation = 0 @@ -32,26 +46,10 @@ def tick_timer(timer, iteration: int, total_seconds: int) -> None: running_timer_threads.remove(timer.id) return - if timer.minutes == 0 and timer.seconds == 0: - # Stop the thread if we have reached the end - - running_timer_threads.remove(timer.id) - return + handle_expired_timer(timer) start_time = timeit.default_timer() - print(timer.minutes, timer.seconds) - - if timer.minutes == 0 and timer.seconds == 0: - # Stop the thread if we have reached the end - - timer.is_running = False - timer.save() - - running_timer_threads.remove(timer.id) - - return - if iteration != timer.iteration: # Stop the thread if there is a new timer @@ -66,6 +64,8 @@ def tick_timer(timer, iteration: int, total_seconds: int) -> None: timer.minutes = minutes timer.seconds = seconds + handle_expired_timer(timer) + timer.save() end_time = timeit.default_timer()