refactor: move timer function to method

- introduce Timer struct to manage timer state
- update RunPomodoro to use Timer methods for starting, stopping, and pausing
- remove unused pomodoro channels
This commit is contained in:
Sebastian Mark 2024-10-25 12:30:43 +02:00
parent 62fbb1d356
commit aa5c24f06d
2 changed files with 58 additions and 41 deletions

View file

@ -6,11 +6,8 @@ import (
"time" "time"
) )
var pomodoroResetChannel = make(chan bool, 1)
var pomodoroPauseChannel = make(chan bool, 1)
var pomodoroResumeChannel = make(chan bool, 1)
var mu sync.Mutex // to synchronize access to shared state var mu sync.Mutex // to synchronize access to shared state
var timer Timer
// RunPomodoro iterates the Pomodoro work/break sessions. // RunPomodoro iterates the Pomodoro work/break sessions.
func RunPomodoro() { func RunPomodoro() {
@ -22,26 +19,29 @@ func RunPomodoro() {
pomodoroConfig := shared.Message.PomodoroSettings pomodoroConfig := shared.Message.PomodoroSettings
for session := 1; session <= pomodoroConfig.Sessions; session++ { for session := 1; session <= pomodoroConfig.Sessions; session++ {
timer = timer.Init()
shared.Message.Session = session shared.Message.Session = session
shared.Message.Mode = "Work" shared.Message.Mode = "Work"
if !startTimer(pomodoroConfig.Work) { if !timer.Start(pomodoroConfig.Work) {
break break
} }
if session == pomodoroConfig.Sessions {
shared.Message.Mode = "LongBreak"
if !startTimer(pomodoroConfig.LongBreak) {
break
}
} else {
shared.Message.Mode = "ShortBreak"
if !startTimer(pomodoroConfig.ShortBreak) {
break
}
}
shared.Message.Mode = "End"
}
time.Sleep(time.Second) if session > pomodoroConfig.Sessions {
shared.Message.Mode = "ShortBreak"
if !timer.Start(pomodoroConfig.ShortBreak) {
break
}
} else { // last phase, prepare for finish
shared.Message.Mode = "LongBreak"
if !timer.Start(pomodoroConfig.LongBreak) {
break
}
shared.Message.Mode = "End"
time.Sleep(time.Second)
}
}
mu.Lock() mu.Lock()
shared.Message.Ongoing = false shared.Message.Ongoing = false
@ -53,12 +53,8 @@ func RunPomodoro() {
shared.Message.TimeLeft = shared.Message.PomodoroSettings.Work shared.Message.TimeLeft = shared.Message.PomodoroSettings.Work
} }
// Stops and resets the running Pomodoro
func ResetPomodoro() { func ResetPomodoro() {
shared.Message.Mode = "Idle" timer.Stop()
shared.Message.Session = 0
shared.Message.TimeLeft = shared.Message.PomodoroSettings.Work
pomodoroResetChannel <- true
} }
func PausePomodoro() { func PausePomodoro() {
@ -66,14 +62,14 @@ func PausePomodoro() {
shared.Message.Paused = true shared.Message.Paused = true
mu.Unlock() mu.Unlock()
pomodoroPauseChannel <- true timer.Pause()
} }
func ResumePomodoro() { func ResumePomodoro() {
mu.Lock() mu.Lock()
shared.Message.Paused = false shared.Message.Paused = false
mu.Unlock() mu.Unlock()
pomodoroResumeChannel <- true timer.Resume()
} }
func IsPomodoroOngoing() bool { func IsPomodoroOngoing() bool {

View file

@ -5,28 +5,49 @@ import (
"time" "time"
) )
// runs the countdown and updates shared.Message.TimeLeft every second. type Timer struct {
func startTimer(remainingSeconds int) bool { abort chan bool
for remainingSeconds > 0 { wait chan bool
resume chan bool
}
func (t Timer) Init() Timer {
return Timer{
abort: make(chan bool, 1),
wait: make(chan bool, 1),
resume: make(chan bool, 1),
}
}
func (t *Timer) Start(duration int) bool {
for timeLeft := duration; timeLeft > 0; {
select { select {
case <-pomodoroResetChannel: case <-t.abort:
return false return false
case <-pomodoroPauseChannel: case <-t.wait:
// Nothing to set here, just waiting for the signal <-t.resume
case <-pomodoroResumeChannel:
// Nothing to set here, just waiting for the signal
default: default:
// Broadcast the current state to all clients shared.Message.TimeLeft = timeLeft
if !IsPomodoroPaused() { time.Sleep(time.Second)
shared.Message.TimeLeft = remainingSeconds timeLeft--
time.Sleep(time.Second)
remainingSeconds--
}
} }
} }
// Final shared.Message when time reaches zero // Final shared.Message when time reaches zero
shared.Message.TimeLeft = remainingSeconds shared.Message.TimeLeft = 0
return true return true
} }
func (t *Timer) Stop() {
t.resume <- true
t.abort <- true
}
func (t *Timer) Pause() {
t.wait <- true
}
func (t *Timer) Resume() {
t.resume <- true
}