GoTomato/internal/pomodoro/timer.go

44 lines
1.1 KiB
Go

package pomodoro
import (
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/gorilla/websocket"
"time"
)
// startTimer runs the countdown and broadcasts every second.
func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int, mode string, session int) bool {
for remainingSeconds > 0 {
select {
case <-pomodoroResetChannel:
return false
case <-pomodoroPauseChannel:
// Nothing to set here, just waiting for the signal
case <-pomodoroResumeChannel:
// Nothing to set here, just waiting for the signal
default:
// Broadcast the current state to all clients
if !IsPomodoroPaused() {
broadcast.BroadcastMessage(clients, models.ServerMessage{
Mode: mode,
Session: session,
MaxSession: PomodoroConfig.Sessions,
TimeLeft: remainingSeconds,
})
time.Sleep(time.Second)
remainingSeconds--
}
}
}
// Final broadcast when time reaches zero
broadcast.BroadcastMessage(clients, models.ServerMessage{
Mode: mode,
Session: session,
MaxSession: PomodoroConfig.Sessions,
TimeLeft: 0,
})
return true
}