Update example in README to use tea.Every

This commit is contained in:
Christian Rocha 2020-05-11 12:54:44 -04:00
parent d503d5dbf6
commit 7b887b0a05
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
2 changed files with 6 additions and 9 deletions

View File

@ -21,7 +21,7 @@ import (
type model int type model int
type tickMsg struct{} type tickMsg time.Time
func main() { func main() {
p := tea.NewProgram(init, update, view, subscriptions) p := tea.NewProgram(init, update, view, subscriptions)
@ -37,7 +37,7 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
switch msg.(type) { switch msg.(type) {
case tickMsg: case tickMsg:
m-- m--
if m <= 0 { if m == 0 {
return m, tea.Quit return m, tea.Quit
} }
} }
@ -53,14 +53,11 @@ func view(mdl tea.Model) string {
// Subscribe to events // Subscribe to events
func subscriptions(_ tea.Model) tea.Subs { func subscriptions(_ tea.Model) tea.Subs {
return tea.Subs{ return tea.Subs{
"tick": tick, "tick": time.Every(time.Second, func(t time.Time) tea.Msg {
return tickMsg(t)
},
} }
} }
func tick(_ tea.Model) tea.Msg {
time.Sleep(time.Second)
return tickMsg{}
}
``` ```
Hungry for more? See the [other examples][examples]. Hungry for more? See the [other examples][examples].

View File

@ -4,7 +4,7 @@ import (
"time" "time"
) )
// NewEverMsg is used by Every to create a new message. It contains the time // NewEveryMsg is used by Every to create a new message. It contains the time
// at which the timer finished. // at which the timer finished.
type NewEveryMsg func(time.Time) Msg type NewEveryMsg func(time.Time) Msg