From 4dc89a3518e1a2276c4a659fb6c98bcbbf8822ae Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sat, 19 Oct 2024 18:05:10 +0200 Subject: [PATCH] feat(pomodoro): update timer management MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - rename timerRunning to pomodoroRunning - move function and timer definition to correct source file 🤖 --- internal/pomodoro/pomodoro.go | 10 ++++++++-- internal/pomodoro/timer.go | 6 ------ internal/websocket/client_commands.go | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/pomodoro/pomodoro.go b/internal/pomodoro/pomodoro.go index 02325ac..65705fe 100644 --- a/internal/pomodoro/pomodoro.go +++ b/internal/pomodoro/pomodoro.go @@ -12,12 +12,13 @@ const ( sessions = 4 ) +var pomodoroRunning bool var mu sync.Mutex // to synchronize access to shared state // RunPomodoroTimer iterates the Pomodoro work/break sessions. func RunPomodoroTimer(clients map[*websocket.Conn]bool) { mu.Lock() - timerRunning = true + pomodoroRunning = true for session := 1; session <= sessions; session++ { if !startTimer(clients, workDuration, "Work", session) { @@ -34,6 +35,11 @@ func RunPomodoroTimer(clients map[*websocket.Conn]bool) { } } - timerRunning = false + pomodoroRunning = false mu.Unlock() } + +// IsPomodoroRunning returns the status of the timer. +func IsPomodoroRunning() bool { + return pomodoroRunning +} diff --git a/internal/pomodoro/timer.go b/internal/pomodoro/timer.go index 13364a1..c7d931b 100644 --- a/internal/pomodoro/timer.go +++ b/internal/pomodoro/timer.go @@ -7,7 +7,6 @@ import ( "time" ) -var timerRunning bool var timerStopChannel = make(chan bool, 1) // startTimer runs the countdown and broadcasts every second. @@ -44,8 +43,3 @@ func startTimer(clients map[*websocket.Conn]bool, remainingSeconds int, mode str func StopTimer() { timerStopChannel <- true } - -// IsTimerRunning returns the status of the timer. -func IsTimerRunning() bool { - return timerRunning -} diff --git a/internal/websocket/client_commands.go b/internal/websocket/client_commands.go index fe79513..41855bf 100644 --- a/internal/websocket/client_commands.go +++ b/internal/websocket/client_commands.go @@ -29,11 +29,11 @@ func handleClientCommands(ws *websocket.Conn) { // Process the command switch command.Command { case "start": - if !pomodoro.IsTimerRunning() { + if !pomodoro.IsPomodoroRunning() { go pomodoro.RunPomodoroTimer(Clients) // Start the timer with the list of clients } case "stop": - if pomodoro.IsTimerRunning() { + if pomodoro.IsPomodoroRunning() { pomodoro.StopTimer() // Stop the timer in the Pomodoro package } }