Every sends the time at which the timer ticked

This commit is contained in:
Christian Rocha 2020-05-05 14:26:06 -04:00
parent ac67237eab
commit d503d5dbf6
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
4 changed files with 49 additions and 27 deletions

View File

@ -12,7 +12,11 @@ import (
type model int
type tickMsg struct{}
type tickMsg time.Time
func newTickMsg(t time.Time) tea.Msg {
return tickMsg(t)
}
func main() {
tea.AltScreen()
@ -55,7 +59,7 @@ func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
func subscriptions(_ tea.Model) tea.Subs {
return tea.Subs{
"tick": tea.Every(time.Second, tickMsg{}),
"tick": tea.Every(time.Second, newTickMsg),
}
}

View File

@ -17,7 +17,7 @@ type Model int
// Messages are events that we respond to in our Update function. This
// particular one indicates that the timer has ticked.
type TickMsg struct{}
type tickMsg time.Time
func main() {
// Initialize our program
@ -41,7 +41,7 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
switch msg.(type) {
case tea.KeyMsg:
return m, tea.Quit
case TickMsg:
case tickMsg:
m -= 1
if m <= 0 {
return m, tea.Quit
@ -61,6 +61,8 @@ func view(model tea.Model) string {
// second, sends a tick, and then restarts.
func subscriptions(_ tea.Model) tea.Subs {
return tea.Subs{
"tick": tea.Every(time.Second, TickMsg{}),
"tick": tea.Every(time.Second, func(t time.Time) tea.Msg {
return tickMsg(t)
}),
}
}

View File

@ -12,20 +12,6 @@ import (
"github.com/fogleman/ease"
)
// Model contains the data for our application.
type Model struct {
Choice int
Chosen bool
Ticks int
Frames int
Progress float64
Loaded bool
}
type tickMsg struct{}
type frameMsg struct{}
func main() {
p := tea.NewProgram(
initialize,
@ -38,6 +24,32 @@ func main() {
}
}
// MSG
type tickMsg time.Time
func newTickMsg(t time.Time) tea.Msg {
return tickMsg(t)
}
type frameMsg time.Time
func newFrameMsg(t time.Time) tea.Msg {
return frameMsg(t)
}
// MODEL
// Model contains the data for our application.
type Model struct {
Choice int
Chosen bool
Ticks int
Frames int
Progress float64
Loaded bool
}
// INIT
func initialize() (tea.Model, tea.Cmd) {
@ -50,15 +62,15 @@ func subscriptions(model tea.Model) tea.Subs {
m, _ := model.(Model)
if !m.Chosen || m.Loaded {
return tea.Subs{
"tick": tea.Every(time.Second, tickMsg{}),
"tick": tea.Every(time.Second, newTickMsg),
}
}
return tea.Subs{
"frame": tea.Every(time.Second/60, frameMsg{}),
"frame": tea.Every(time.Second/60, newFrameMsg),
}
}
// UPDATES
// UPDATE
func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
m, _ := model.(Model)
@ -145,7 +157,7 @@ func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {
return m, nil
}
// VIEWS
// VIEW
func view(model tea.Model) string {
m, _ := model.(Model)

View File

@ -4,18 +4,22 @@ import (
"time"
)
// NewEverMsg is used by Every to create a new message. It contains the time
// at which the timer finished.
type NewEveryMsg func(time.Time) Msg
// Every is a subscription that ticks with the system clock at the given
// duration
// duration.
//
// TODO: make it cancelable
func Every(duration time.Duration, msg Msg) Sub {
func Every(duration time.Duration, newMsg NewEveryMsg) Sub {
return func() Msg {
n := time.Now()
d := n.Truncate(duration).Add(duration).Sub(n)
t := time.NewTimer(d)
select {
case <-t.C:
return msg
case now := <-t.C:
return newMsg(now)
}
}
}