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 tickMsg struct{}
type tickMsg time.Time
func main() {
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) {
case tickMsg:
m--
if m <= 0 {
if m == 0 {
return m, tea.Quit
}
}
@ -53,14 +53,11 @@ func view(mdl tea.Model) string {
// Subscribe to events
func subscriptions(_ tea.Model) 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].

View File

@ -4,7 +4,7 @@ import (
"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.
type NewEveryMsg func(time.Time) Msg