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