From 337e5b0ed3dfe78d9eecc7b1f5f36fa385516f8d Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 20 Oct 2024 21:59:18 +0200 Subject: [PATCH 1/2] refactor: rename BroadcastMessage model to ServerMessage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 --- internal/broadcast/broadcast.go | 2 +- internal/pomodoro/pomodoro.go | 2 +- internal/pomodoro/timer.go | 4 ++-- pkg/models/{broadcast.go => server.go} | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) rename pkg/models/{broadcast.go => server.go} (74%) diff --git a/internal/broadcast/broadcast.go b/internal/broadcast/broadcast.go index 229ae67..1be04ce 100644 --- a/internal/broadcast/broadcast.go +++ b/internal/broadcast/broadcast.go @@ -8,7 +8,7 @@ import ( ) // BroadcastMessage sends a message to all connected WebSocket clients. -func BroadcastMessage(clients map[*websocket.Conn]*models.Client, message models.BroadcastMessage) { +func BroadcastMessage(clients map[*websocket.Conn]*models.Client, message models.ServerMessage) { // Marshal the message into JSON format jsonMessage, err := json.Marshal(message) if err != nil { diff --git a/internal/pomodoro/pomodoro.go b/internal/pomodoro/pomodoro.go index c030acf..d5e88de 100644 --- a/internal/pomodoro/pomodoro.go +++ b/internal/pomodoro/pomodoro.go @@ -58,7 +58,7 @@ func ResetPomodoro(clients map[*websocket.Conn]*models.Client) { mu.Unlock() // Broadcast the reset message to all clients - broadcast.BroadcastMessage(clients, models.BroadcastMessage{ + broadcast.BroadcastMessage(clients, models.ServerMessage{ Mode: "none", Session: 0, MaxSession: 0, diff --git a/internal/pomodoro/timer.go b/internal/pomodoro/timer.go index e4d95dd..bca8116 100644 --- a/internal/pomodoro/timer.go +++ b/internal/pomodoro/timer.go @@ -20,7 +20,7 @@ func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int default: // Broadcast the current state to all clients if !IsPomodoroPaused() { - broadcast.BroadcastMessage(clients, models.BroadcastMessage{ + broadcast.BroadcastMessage(clients, models.ServerMessage{ Mode: mode, Session: session, MaxSession: PomodoroConfig.Sessions, @@ -33,7 +33,7 @@ func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int } // Final broadcast when time reaches zero - broadcast.BroadcastMessage(clients, models.BroadcastMessage{ + broadcast.BroadcastMessage(clients, models.ServerMessage{ Mode: mode, Session: session, MaxSession: PomodoroConfig.Sessions, diff --git a/pkg/models/broadcast.go b/pkg/models/server.go similarity index 74% rename from pkg/models/broadcast.go rename to pkg/models/server.go index 78463b9..8c68258 100644 --- a/pkg/models/broadcast.go +++ b/pkg/models/server.go @@ -1,7 +1,7 @@ package models -// BroadcastMessage represents the data sent to the client via WebSocket. -type BroadcastMessage struct { +// ServerMessage represents the data sent to the client via WebSocket. +type ServerMessage struct { Mode string `json:"mode"` // "Work", "ShortBreak", or "LongBreak" Session int `json:"session"` // Current session number MaxSession int `json:"max_session"` // Total number of sessions From f098c1f6bf745295c87c417bf5ce4a2a0916feba Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 20 Oct 2024 22:00:54 +0200 Subject: [PATCH 2/2] refactor: rename RunPomodoroTimer() to RunPomodoro() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 --- internal/pomodoro/pomodoro.go | 4 ++-- internal/websocket/client_commands.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/pomodoro/pomodoro.go b/internal/pomodoro/pomodoro.go index d5e88de..e75f679 100644 --- a/internal/pomodoro/pomodoro.go +++ b/internal/pomodoro/pomodoro.go @@ -23,8 +23,8 @@ var pomodoroResumeChannel = make(chan bool, 1) var mu sync.Mutex // to synchronize access to shared state -// RunPomodoroTimer iterates the Pomodoro work/break sessions. -func RunPomodoroTimer(clients map[*websocket.Conn]*models.Client) { +// RunPomodoro iterates the Pomodoro work/break sessions. +func RunPomodoro(clients map[*websocket.Conn]*models.Client) { mu.Lock() pomodoroOngoing = true pomodoroPaused = false diff --git a/internal/websocket/client_commands.go b/internal/websocket/client_commands.go index b88ca78..2938a6b 100644 --- a/internal/websocket/client_commands.go +++ b/internal/websocket/client_commands.go @@ -30,7 +30,7 @@ func handleClientCommands(ws *websocket.Conn) { switch command.Command { case "start": if !pomodoro.IsPomodoroOngoing() { - go pomodoro.RunPomodoroTimer(Clients) // Start the timer with the list of clients + go pomodoro.RunPomodoro(Clients) // Start the timer with the list of clients } case "stop": if pomodoro.IsPomodoroOngoing() {