Compare commits
No commits in common. "32a33dc68492cb27918afc244a8c0dcfa954ed91" and "0b2f83cd35bbc73fcedd5944277297c351a835db" have entirely different histories.
32a33dc684
...
0b2f83cd35
3 changed files with 17 additions and 12 deletions
|
@ -14,7 +14,7 @@ var PomodoroConfig = models.GoTomatoTimerConfig{
|
|||
Sessions: 4,
|
||||
}
|
||||
|
||||
var pomodoroOngoing bool
|
||||
var pomodoroRunning bool
|
||||
var pomodoroPaused bool
|
||||
|
||||
var pomodoroResetChannel = make(chan bool, 1)
|
||||
|
@ -26,7 +26,7 @@ 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) {
|
||||
mu.Lock()
|
||||
pomodoroOngoing = true
|
||||
pomodoroRunning = true
|
||||
pomodoroPaused = false
|
||||
mu.Unlock()
|
||||
|
||||
|
@ -46,14 +46,14 @@ func RunPomodoroTimer(clients map[*websocket.Conn]*models.Client) {
|
|||
}
|
||||
|
||||
mu.Lock()
|
||||
pomodoroOngoing = false
|
||||
pomodoroRunning = 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
|
||||
pomodoroRunning = false // Reset the running state
|
||||
pomodoroPaused = false // Reset the paused state
|
||||
mu.Unlock()
|
||||
|
||||
|
@ -84,10 +84,10 @@ func ResumePomodoro() {
|
|||
pomodoroResumeChannel <- true
|
||||
}
|
||||
|
||||
func IsPomodoroOngoing() bool {
|
||||
func IsPomodoroRunning() bool {
|
||||
mu.Lock()
|
||||
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
|
||||
return pomodoroOngoing
|
||||
return pomodoroRunning
|
||||
}
|
||||
|
||||
func IsPomodoroPaused() bool {
|
||||
|
|
|
@ -15,6 +15,13 @@ var mu sync.Mutex // Mutex to protect access to the Clients map
|
|||
|
||||
// handleClientCommands listens for commands from WebSocket clients and dispatches to the timer.
|
||||
func handleClientCommands(ws *websocket.Conn) {
|
||||
// Create a new Client and add it to the Clients map
|
||||
mu.Lock()
|
||||
Clients[ws] = &models.Client{
|
||||
Conn: ws,
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
for {
|
||||
_, message, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
|
@ -34,19 +41,19 @@ func handleClientCommands(ws *websocket.Conn) {
|
|||
// Process the command
|
||||
switch command.Command {
|
||||
case "start":
|
||||
if !pomodoro.IsPomodoroActive() {
|
||||
if !pomodoro.IsPomodoroRunning() {
|
||||
go pomodoro.RunPomodoroTimer(Clients) // Start the timer with the list of clients
|
||||
}
|
||||
case "stop":
|
||||
if pomodoro.IsPomodoroActive() {
|
||||
if pomodoro.IsPomodoroRunning() {
|
||||
pomodoro.ResetPomodoro(Clients) // Reset Pomodoro
|
||||
}
|
||||
case "pause":
|
||||
if pomodoro.IsPomodoroActive() && !pomodoro.IsPomodoroPaused() {
|
||||
if pomodoro.IsPomodoroRunning() && !pomodoro.IsPomodoroPaused() {
|
||||
pomodoro.PausePomodoro() // Pause the timer
|
||||
}
|
||||
case "resume":
|
||||
if pomodoro.IsPomodoroActive() && pomodoro.IsPomodoroPaused() {
|
||||
if pomodoro.IsPomodoroRunning() && pomodoro.IsPomodoroPaused() {
|
||||
pomodoro.ResumePomodoro() // Resume the timer
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,11 +23,9 @@ func HandleConnections(w http.ResponseWriter, r *http.Request) {
|
|||
defer ws.Close()
|
||||
|
||||
// Register the new client
|
||||
mu.Lock()
|
||||
Clients[ws] = &models.Client{
|
||||
Conn: ws, // Store the WebSocket connection
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
// Listen for commands from the connected client
|
||||
handleClientCommands(ws)
|
||||
|
|
Loading…
Reference in a new issue