feat: simplify pomodoro functions by removing client parameter

- remove clients parameter from RunPomodoro function
- update startTimer to no longer require clients parameter
- modify ResetPomodoro to eliminate clients parameter
- adjust client_commands to reflect changes in function signatures

🤖
This commit is contained in:
Sebastian Mark 2024-10-21 08:39:39 +02:00
parent 9615d4d449
commit 234f3c17dc
3 changed files with 8 additions and 11 deletions

View file

@ -3,7 +3,6 @@ package pomodoro
import (
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/gorilla/websocket"
"sync"
)
@ -14,7 +13,7 @@ var pomodoroResumeChannel = make(chan bool, 1)
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) {
func RunPomodoro(config models.GoTomatoPomodoroConfig) {
mu.Lock()
broadcast.Message.Ongoing = true
broadcast.Message.Paused = false
@ -24,15 +23,15 @@ func RunPomodoro(clients map[*websocket.Conn]*models.Client, config models.GoTom
for session := 1; session <= config.Sessions; session++ {
broadcast.Message.Session = session
if !startTimer(clients, config.Work, "Work") {
if !startTimer(config.Work, "Work") {
break
}
if session == config.Sessions {
if !startTimer(clients, config.LongBreak, "LongBreak") {
if !startTimer(config.LongBreak, "LongBreak") {
break
}
} else {
if !startTimer(clients, config.ShortBreak, "ShortBreak") {
if !startTimer(config.ShortBreak, "ShortBreak") {
break
}
}
@ -44,7 +43,7 @@ func RunPomodoro(clients map[*websocket.Conn]*models.Client, config models.GoTom
}
// ResetPomodoro resets the running Pomodoro timer.
func ResetPomodoro(clients map[*websocket.Conn]*models.Client) {
func ResetPomodoro() {
// Send a reset signal to stop any running timers
pomodoroResetChannel <- true

View file

@ -2,13 +2,11 @@ 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) bool {
func startTimer(remainingSeconds int, mode string) bool {
for remainingSeconds > 0 {
select {
case <-pomodoroResetChannel:

View file

@ -43,11 +43,11 @@ func handleClientCommands(ws *websocket.Conn) {
if clientCommand.Config != unsetPomodoroConfig {
pomodoroConfig = clientCommand.Config
}
go pomodoro.RunPomodoro(broadcast.Clients, pomodoroConfig) // Start the timer with the list of clients
go pomodoro.RunPomodoro(pomodoroConfig) // Start the timer with the list of clients
}
case "stop":
if pomodoro.IsPomodoroOngoing() {
pomodoro.ResetPomodoro(broadcast.Clients) // Reset Pomodoro
pomodoro.ResetPomodoro() // Reset Pomodoro
}
case "pause":
if pomodoro.IsPomodoroOngoing() && !pomodoro.IsPomodoroPaused() {