Compare commits

..

No commits in common. "bdfd5c3b8448cff5d7903ec136bfcb8d32afa2fa" and "d0b1260f6243a6b63ca26a025073b52aac920401" have entirely different histories.

5 changed files with 16 additions and 17 deletions

View file

@ -42,7 +42,7 @@ func RunPomodoro() {
// Work // Work
shared.State.Mode = "Work" shared.State.Mode = "Work"
timer.StartAsync(pomodoroConfig.Work) go timer.Start(pomodoroConfig.Work)
if !waitForTimer(timer) { if !waitForTimer(timer) {
break break
} }
@ -50,13 +50,13 @@ func RunPomodoro() {
// Breaks // Breaks
if session < pomodoroConfig.Sessions { if session < pomodoroConfig.Sessions {
shared.State.Mode = "ShortBreak" shared.State.Mode = "ShortBreak"
timer.StartAsync(pomodoroConfig.ShortBreak) go timer.Start(pomodoroConfig.ShortBreak)
if !waitForTimer(timer) { if !waitForTimer(timer) {
break break
} }
} else { // last phase, prepare for finish } else { // last phase, prepare for finish
shared.State.Mode = "LongBreak" shared.State.Mode = "LongBreak"
timer.StartAsync(pomodoroConfig.LongBreak) go timer.Start(pomodoroConfig.LongBreak)
if !waitForTimer(timer) { if !waitForTimer(timer) {
break break
} }

View file

@ -24,12 +24,7 @@ func (t Timer) Init() Timer {
} }
} }
// Start the timer (goroutine) // Start the timer
func (t *Timer) StartAsync(duration int) {
go t.Start(duration)
}
// Start the timer (blocking)
func (t *Timer) Start(duration int) { func (t *Timer) Start(duration int) {
tick := time.NewTicker(time.Second) tick := time.NewTicker(time.Second)
for timeLeft := duration; timeLeft > 0; { for timeLeft := duration; timeLeft > 0; {

View file

@ -3,6 +3,7 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/GoTomato/internal/pomodoro" "git.smsvc.net/pomodoro/GoTomato/internal/pomodoro"
"git.smsvc.net/pomodoro/GoTomato/internal/shared" "git.smsvc.net/pomodoro/GoTomato/internal/shared"
@ -10,8 +11,7 @@ import (
) )
// Listens for commands from a client and handles them // Listens for commands from a client and handles them
func handleClientCommands(c models.Client) { func handleClientCommands(ws *websocket.Conn) {
ws := c.Conn
for { for {
var clientCommand models.ClientCommand var clientCommand models.ClientCommand

View file

@ -31,13 +31,12 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
log.Info("Client connected", "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)+1) log.Info("Client connected", "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)+1)
// Register the new client // Register the new client
client := models.Client{
Conn: ws,
}
mu.Lock() mu.Lock()
Clients[ws] = &client Clients[ws] = &models.Client{
Conn: ws, // Store the WebSocket connection
}
mu.Unlock() mu.Unlock()
// Listen for commands from the connected client // Listen for commands from the connected client
handleClientCommands(client) handleClientCommands(ws)
} }

View file

@ -3,6 +3,7 @@ package models
import ( import (
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"sync"
) )
// Represents a command from the client (start/stop) // Represents a command from the client (start/stop)
@ -14,12 +15,16 @@ type ClientCommand struct {
// Represents a single client // Represents a single client
type Client struct { type Client struct {
Conn *websocket.Conn Conn *websocket.Conn // Websocket connection of the client
Mutex sync.Mutex // Mutex used to lock
} }
// Sends a message to the websocket. // Sends a message to the websocket.
// Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time. // Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time.
func (c *Client) SendMessage(messageType int, data []byte) error { func (c *Client) SendMessage(messageType int, data []byte) error {
c.Mutex.Lock()
defer c.Mutex.Unlock()
err := c.Conn.WriteMessage(messageType, data) err := c.Conn.WriteMessage(messageType, data)
if err != nil { if err != nil {
log.Error("Error writing to WebSocket:", "msg", err) log.Error("Error writing to WebSocket:", "msg", err)