From d503d5dbf6bfb3ed311ebc222e77d6874f7787ff Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 5 May 2020 14:26:06 -0400 Subject: [PATCH] Every sends the time at which the timer ticked --- examples/fullscreen/main.go | 8 +++++-- examples/simple/main.go | 8 ++++--- examples/views/main.go | 48 +++++++++++++++++++++++-------------- subscriptions.go | 12 ++++++---- 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/examples/fullscreen/main.go b/examples/fullscreen/main.go index 1c2cab3..7fe2938 100644 --- a/examples/fullscreen/main.go +++ b/examples/fullscreen/main.go @@ -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), } } diff --git a/examples/simple/main.go b/examples/simple/main.go index 93eabc9..60bf791 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -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) + }), } } diff --git a/examples/views/main.go b/examples/views/main.go index 387dbd7..1021bdf 100644 --- a/examples/views/main.go +++ b/examples/views/main.go @@ -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) diff --git a/subscriptions.go b/subscriptions.go index a6e128d..b428d0a 100644 --- a/subscriptions.go +++ b/subscriptions.go @@ -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) } } }