refactor: move broadcast.Message to shared.Message

- create a new shared state file to manage message state
- replace broadcast.Message with shared.Message in pomodoro logic
- update websocket client handling to use shared.Clients
- remove unnecessary broadcast imports from various files
- ensure consistent message handling across the application

🤖
This commit is contained in:
Sebastian Mark 2024-10-21 09:20:58 +02:00
parent 234f3c17dc
commit aab6896c7d
6 changed files with 44 additions and 40 deletions

View file

@ -1,7 +1,7 @@
package pomodoro
import (
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"sync"
)
@ -15,14 +15,14 @@ var mu sync.Mutex // to synchronize access to shared state
// RunPomodoro iterates the Pomodoro work/break sessions.
func RunPomodoro(config models.GoTomatoPomodoroConfig) {
mu.Lock()
broadcast.Message.Ongoing = true
broadcast.Message.Paused = false
shared.Message.Ongoing = true
shared.Message.Paused = false
mu.Unlock()
broadcast.Message.TotalSession = config.Sessions
shared.Message.TotalSession = config.Sessions
for session := 1; session <= config.Sessions; session++ {
broadcast.Message.Session = session
shared.Message.Session = session
if !startTimer(config.Work, "Work") {
break
}
@ -38,7 +38,7 @@ func RunPomodoro(config models.GoTomatoPomodoroConfig) {
}
mu.Lock()
broadcast.Message.Ongoing = false
shared.Message.Ongoing = false
mu.Unlock()
}
@ -48,20 +48,20 @@ func ResetPomodoro() {
pomodoroResetChannel <- true
mu.Lock()
broadcast.Message.Ongoing = false
broadcast.Message.Paused = false
shared.Message.Ongoing = false
shared.Message.Paused = false
mu.Unlock()
// Reset message
broadcast.Message.Mode = "none"
broadcast.Message.Session = 0
broadcast.Message.TotalSession = 0
broadcast.Message.TimeLeft = 0
shared.Message.Mode = "none"
shared.Message.Session = 0
shared.Message.TotalSession = 0
shared.Message.TimeLeft = 0
}
func PausePomodoro() {
mu.Lock()
broadcast.Message.Paused = true
shared.Message.Paused = true
mu.Unlock()
pomodoroPauseChannel <- true
@ -69,7 +69,7 @@ func PausePomodoro() {
func ResumePomodoro() {
mu.Lock()
broadcast.Message.Paused = false
shared.Message.Paused = false
mu.Unlock()
pomodoroResumeChannel <- true
}
@ -77,11 +77,11 @@ func ResumePomodoro() {
func IsPomodoroOngoing() bool {
mu.Lock()
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
return broadcast.Message.Ongoing
return shared.Message.Ongoing
}
func IsPomodoroPaused() bool {
mu.Lock()
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
return broadcast.Message.Paused
return shared.Message.Paused
}