feat: implement asynchronous timer start

- add new StartAsync method to Timer for non-blocking execution
- use new method in RunPomodoro()

🤖
This commit is contained in:
Sebastian Mark 2024-10-30 07:46:29 +01:00
parent d0b1260f62
commit 94b6786c7c
2 changed files with 9 additions and 4 deletions

View file

@ -42,7 +42,7 @@ func RunPomodoro() {
// Work // Work
shared.State.Mode = "Work" shared.State.Mode = "Work"
go timer.Start(pomodoroConfig.Work) timer.StartAsync(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"
go timer.Start(pomodoroConfig.ShortBreak) timer.StartAsync(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"
go timer.Start(pomodoroConfig.LongBreak) timer.StartAsync(pomodoroConfig.LongBreak)
if !waitForTimer(timer) { if !waitForTimer(timer) {
break break
} }

View file

@ -24,7 +24,12 @@ func (t Timer) Init() Timer {
} }
} }
// Start the timer // Start the timer (goroutine)
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; {