feat: implement permanent broadcast message functionality

- add SendPermanentBroadCastMessage to continuously send updates
- refactor BroadcastMessage to use a shared Message struct
- update pomodoro logic to modify broadcast.Message directly
- adjust client command handling to use broadcast.Clients map
- enhance ServerMessage struct with "Ongoing" and "Paused" fields

🤖
This commit is contained in:
Sebastian Mark 2024-10-21 08:16:26 +02:00
parent eba4065c6f
commit 9615d4d449
8 changed files with 75 additions and 65 deletions

View file

@ -7,9 +7,6 @@ import (
"sync"
)
var pomodoroOngoing bool
var pomodoroPaused bool
var pomodoroResetChannel = make(chan bool, 1)
var pomodoroPauseChannel = make(chan bool, 1)
var pomodoroResumeChannel = make(chan bool, 1)
@ -19,52 +16,53 @@ var mu sync.Mutex // to synchronize access to shared state
// RunPomodoro iterates the Pomodoro work/break sessions.
func RunPomodoro(clients map[*websocket.Conn]*models.Client, config models.GoTomatoPomodoroConfig) {
mu.Lock()
pomodoroOngoing = true
pomodoroPaused = false
broadcast.Message.Ongoing = true
broadcast.Message.Paused = false
mu.Unlock()
broadcast.Message.TotalSession = config.Sessions
for session := 1; session <= config.Sessions; session++ {
if !startTimer(clients, config.Work, "Work", session, config.Sessions) {
broadcast.Message.Session = session
if !startTimer(clients, config.Work, "Work") {
break
}
if session == config.Sessions {
if !startTimer(clients, config.LongBreak, "LongBreak", session, config.Sessions) {
if !startTimer(clients, config.LongBreak, "LongBreak") {
break
}
} else {
if !startTimer(clients, config.ShortBreak, "ShortBreak", session, config.Sessions) {
if !startTimer(clients, config.ShortBreak, "ShortBreak") {
break
}
}
}
mu.Lock()
pomodoroOngoing = false
broadcast.Message.Ongoing = false
mu.Unlock()
}
// ResetPomodoro resets the running Pomodoro timer.
func ResetPomodoro(clients map[*websocket.Conn]*models.Client) {
mu.Lock()
pomodoroOngoing = false // Reset the running state
pomodoroPaused = false // Reset the paused state
mu.Unlock()
// Broadcast the reset message to all clients
broadcast.BroadcastMessage(clients, models.ServerMessage{
Mode: "none",
Session: 0,
TotalSession: 0,
TimeLeft: 0,
})
// Send a reset signal to stop any running timers
pomodoroResetChannel <- true
mu.Lock()
broadcast.Message.Ongoing = false
broadcast.Message.Paused = false
mu.Unlock()
// Reset message
broadcast.Message.Mode = "none"
broadcast.Message.Session = 0
broadcast.Message.TotalSession = 0
broadcast.Message.TimeLeft = 0
}
func PausePomodoro() {
mu.Lock()
pomodoroPaused = true
broadcast.Message.Paused = true
mu.Unlock()
pomodoroPauseChannel <- true
@ -72,7 +70,7 @@ func PausePomodoro() {
func ResumePomodoro() {
mu.Lock()
pomodoroPaused = false
broadcast.Message.Paused = false
mu.Unlock()
pomodoroResumeChannel <- true
}
@ -80,11 +78,11 @@ func ResumePomodoro() {
func IsPomodoroOngoing() bool {
mu.Lock()
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
return pomodoroOngoing
return broadcast.Message.Ongoing
}
func IsPomodoroPaused() bool {
mu.Lock()
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
return pomodoroPaused
return broadcast.Message.Paused
}