refactor: add a stop channel

This commit is contained in:
Dan Anglin 2020-01-19 21:26:46 +00:00
parent 45ad772818
commit c8a1d0adfa
No known key found for this signature in database
GPG key ID: 7AC2B18EC1D09F27
4 changed files with 17 additions and 8 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ bin/*
!bin/.gitkeep
dist/
Pominal
cover.out

View file

@ -43,6 +43,7 @@ var pominalTestCases = []struct {
shortBreak: 300,
longBreak: 600,
label: "",
stopChan: nil,
},
},
{
@ -61,6 +62,7 @@ var pominalTestCases = []struct {
shortBreak: 1536,
longBreak: 1200,
label: "",
stopChan: nil,
},
},
{
@ -79,6 +81,7 @@ var pominalTestCases = []struct {
shortBreak: 60,
longBreak: 60,
label: "",
stopChan: nil,
},
},
{
@ -97,6 +100,7 @@ var pominalTestCases = []struct {
shortBreak: 300,
longBreak: 1200,
label: "",
stopChan: nil,
},
},
}

View file

@ -93,5 +93,4 @@ func main() {
if err := app.SetRoot(flex, true).SetFocus(flex).Run(); err != nil {
panic(err)
}
}

View file

@ -19,9 +19,6 @@
package main
import (
"os"
"os/signal"
"syscall"
"time"
"github.com/rivo/tview"
@ -55,6 +52,9 @@ type Pominal struct {
// label labels Pominal based on the session.
label string
// stopChan is a no-data channel used to stop Pominal.
stopChan chan struct{}
}
// NewPominal creates a new pominal instance
@ -67,22 +67,21 @@ func NewPominal(w, s, l float64, m int) Pominal {
work: setSessionTime(w),
shortBreak: setSessionTime(s),
longBreak: setSessionTime(l),
stopChan: nil,
}
}
// Run Pominal
func (p *Pominal) Run(infoUI, timerUI *tview.TextView) {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
p.UpdateSession(infoUI)
t := time.NewTicker(1 * time.Second)
p.stopChan = make(chan struct{})
infinite:
for {
select {
case <-s:
// fmt.Printf("\n\nReceived signal '%s'. Closing Pominal.\n", sig)
case <-p.stopChan:
t.Stop()
break infinite
case <-t.C:
@ -99,6 +98,12 @@ infinite:
}
}
// Stop sends a signal to the Run method to stop the timer.
// Afterwards the process returns from the Run method.
func (p *Pominal) Stop() {
close(p.stopChan)
}
// UpdateSession resets Pominal's countdown
// based on the next session. If a 'short break'
// session is over the work session counter is incremented.