Compare commits
No commits in common. "636f8c2feb277d71f89387673abacbd0a09caae2" and "d256235c1b927e1f873fc45c1bf5d50d6ee449df" have entirely different histories.
636f8c2feb
...
d256235c1b
4 changed files with 43 additions and 83 deletions
|
@ -1,3 +1,3 @@
|
|||
package metadata
|
||||
|
||||
const GoTomatoVersion = "v0.0.3" // The GoTomato Version
|
||||
const GoTomatoVersion = "v0.0.2" // The GoTomato Version
|
||||
|
|
|
@ -6,21 +6,11 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var mu sync.Mutex // to synchronize access to shared state
|
||||
var timer Timer
|
||||
var pomodoroResetChannel = make(chan bool, 1)
|
||||
var pomodoroPauseChannel = make(chan bool, 1)
|
||||
var pomodoroResumeChannel = make(chan bool, 1)
|
||||
|
||||
func waitForTimer(t Timer) bool {
|
||||
for {
|
||||
select {
|
||||
case <-t.End:
|
||||
return true
|
||||
case <-t.Abort:
|
||||
return false
|
||||
case shared.Message.TimeLeft = <-t.TimeLeft:
|
||||
// do nothing, just let the timer update the message
|
||||
}
|
||||
}
|
||||
}
|
||||
var mu sync.Mutex // to synchronize access to shared state
|
||||
|
||||
// RunPomodoro iterates the Pomodoro work/break sessions.
|
||||
func RunPomodoro() {
|
||||
|
@ -32,33 +22,27 @@ func RunPomodoro() {
|
|||
pomodoroConfig := shared.Message.PomodoroSettings
|
||||
|
||||
for session := 1; session <= pomodoroConfig.Sessions; session++ {
|
||||
timer = timer.Init()
|
||||
|
||||
shared.Message.Session = session
|
||||
|
||||
shared.Message.Mode = "Work"
|
||||
go timer.Start(pomodoroConfig.Work)
|
||||
if !waitForTimer(timer) {
|
||||
if !startTimer(pomodoroConfig.Work) {
|
||||
break
|
||||
}
|
||||
|
||||
if session < pomodoroConfig.Sessions {
|
||||
shared.Message.Mode = "ShortBreak"
|
||||
go timer.Start(pomodoroConfig.ShortBreak)
|
||||
if !waitForTimer(timer) {
|
||||
break
|
||||
}
|
||||
} else { // last phase, prepare for finish
|
||||
if session == pomodoroConfig.Sessions {
|
||||
shared.Message.Mode = "LongBreak"
|
||||
go timer.Start(pomodoroConfig.LongBreak)
|
||||
if !waitForTimer(timer) {
|
||||
if !startTimer(pomodoroConfig.LongBreak) {
|
||||
break
|
||||
}
|
||||
} else {
|
||||
shared.Message.Mode = "ShortBreak"
|
||||
if !startTimer(pomodoroConfig.ShortBreak) {
|
||||
break
|
||||
}
|
||||
shared.Message.Mode = "End"
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
shared.Message.Mode = "End"
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
mu.Lock()
|
||||
shared.Message.Ongoing = false
|
||||
shared.Message.Paused = false
|
||||
|
@ -69,8 +53,12 @@ func RunPomodoro() {
|
|||
shared.Message.TimeLeft = shared.Message.PomodoroSettings.Work
|
||||
}
|
||||
|
||||
// Stops and resets the running Pomodoro
|
||||
func ResetPomodoro() {
|
||||
timer.Stop()
|
||||
shared.Message.Mode = "Idle"
|
||||
shared.Message.Session = 0
|
||||
shared.Message.TimeLeft = shared.Message.PomodoroSettings.Work
|
||||
pomodoroResetChannel <- true
|
||||
}
|
||||
|
||||
func PausePomodoro() {
|
||||
|
@ -78,14 +66,14 @@ func PausePomodoro() {
|
|||
shared.Message.Paused = true
|
||||
mu.Unlock()
|
||||
|
||||
timer.Pause()
|
||||
pomodoroPauseChannel <- true
|
||||
}
|
||||
|
||||
func ResumePomodoro() {
|
||||
mu.Lock()
|
||||
shared.Message.Paused = false
|
||||
mu.Unlock()
|
||||
timer.Resume()
|
||||
pomodoroResumeChannel <- true
|
||||
}
|
||||
|
||||
func IsPomodoroOngoing() bool {
|
||||
|
|
|
@ -1,59 +1,32 @@
|
|||
package pomodoro
|
||||
|
||||
import (
|
||||
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Timer struct {
|
||||
TimeLeft chan int // time left
|
||||
End chan bool // signal on successful end
|
||||
Abort chan bool // signal on premature end
|
||||
stop chan bool
|
||||
wait chan bool
|
||||
resume chan bool
|
||||
}
|
||||
|
||||
func (t Timer) Init() Timer {
|
||||
return Timer{
|
||||
TimeLeft: make(chan int),
|
||||
End: make(chan bool, 1),
|
||||
Abort: make(chan bool, 1),
|
||||
stop: make(chan bool, 1),
|
||||
wait: make(chan bool, 1),
|
||||
resume: make(chan bool, 1),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Timer) Start(duration int) {
|
||||
tick := time.NewTicker(time.Second)
|
||||
for timeLeft := duration; timeLeft > 0; {
|
||||
// runs the countdown and updates shared.Message.TimeLeft every second.
|
||||
func startTimer(remainingSeconds int) bool {
|
||||
for remainingSeconds > 0 {
|
||||
select {
|
||||
case <-t.stop:
|
||||
t.Abort <- true
|
||||
return
|
||||
case <-t.wait:
|
||||
<-t.resume
|
||||
continue
|
||||
case <-tick.C:
|
||||
timeLeft--
|
||||
case <-pomodoroResetChannel:
|
||||
return false
|
||||
case <-pomodoroPauseChannel:
|
||||
// Nothing to set here, just waiting for the signal
|
||||
case <-pomodoroResumeChannel:
|
||||
// Nothing to set here, just waiting for the signal
|
||||
default:
|
||||
t.TimeLeft <- timeLeft
|
||||
// Broadcast the current state to all clients
|
||||
if !IsPomodoroPaused() {
|
||||
shared.Message.TimeLeft = remainingSeconds
|
||||
time.Sleep(time.Second)
|
||||
remainingSeconds--
|
||||
}
|
||||
}
|
||||
}
|
||||
t.TimeLeft <- 0
|
||||
|
||||
t.End <- true
|
||||
}
|
||||
// Final shared.Message when time reaches zero
|
||||
shared.Message.TimeLeft = remainingSeconds
|
||||
|
||||
func (t *Timer) Stop() {
|
||||
t.resume <- true
|
||||
t.stop <- true
|
||||
}
|
||||
|
||||
func (t *Timer) Pause() {
|
||||
t.wait <- true
|
||||
}
|
||||
|
||||
func (t *Timer) Resume() {
|
||||
t.resume <- true
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ import (
|
|||
|
||||
// sends continous messages to all connected WebSocket clients.
|
||||
func SendPermanentBroadCastMessage() {
|
||||
tick := time.NewTicker(time.Second)
|
||||
for {
|
||||
// Marshal the message into JSON format
|
||||
jsonMessage, err := json.Marshal(shared.Message)
|
||||
|
@ -26,6 +25,6 @@ func SendPermanentBroadCastMessage() {
|
|||
// The client is responsible for closing itself on error
|
||||
}
|
||||
}
|
||||
<-tick.C
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue