forked from Mirrors/bubbletea
Update example in README to use tea.Every
This commit is contained in:
parent
d503d5dbf6
commit
7b887b0a05
13
README.md
13
README.md
|
@ -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].
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue