docs: improve godoc on tick and every (#320)

* docs: improve godoc on tick and every

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* docs: small improvements

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* docs: add loop examples for Tick/Every in GoDocs

* docs: small wording adjustments

* docs: small copy edits

Co-authored-by: Christian Rocha <christian@rocha.is>
This commit is contained in:
Carlos Alexandro Becker 2022-05-29 21:40:57 -03:00 committed by GitHub
parent a5f28a3a04
commit 97050569c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 60 additions and 4 deletions

View File

@ -24,6 +24,35 @@ import (
// cmd := Every(time.Second, func(t time.Time) Msg { // cmd := Every(time.Second, func(t time.Time) Msg {
// return TickMsg(t) // return TickMsg(t)
// }) // })
//
// Beginners' note: Every sends a single message and won't automatically
// dispatch messages at an interval. To do that, you'll want to return another
// Every command after receiving your tick message. For example:
//
// type TickMsg time.Time
//
// // Send a message every second.
// func tickEvery() Cmd {
// return Every(time.Second, func(t time.Time) Msg {
// return TickMsg(t)
// })
// }
//
// func (m model) Init() Cmd {
// // Start ticking.
// return tickEvery()
// }
//
// func (m model) Update(msg Msg) (Model, Cmd) {
// switch msg.(type) {
// case TickMsg:
// // Return your Every command again to loop.
// return m, tickEvery()
// }
// return m, nil
// }
//
// Every is analogous to Tick in the Elm Architecture.
func Every(duration time.Duration, fn func(time.Time) Msg) Cmd { func Every(duration time.Duration, fn func(time.Time) Msg) Cmd {
return func() Msg { return func() Msg {
n := time.Now() n := time.Now()
@ -45,6 +74,33 @@ func Every(duration time.Duration, fn func(time.Time) Msg) Cmd {
// cmd := Tick(time.Second, func(t time.Time) Msg { // cmd := Tick(time.Second, func(t time.Time) Msg {
// return TickMsg(t) // return TickMsg(t)
// }) // })
//
// Beginners' note: Tick sends a single message and won't automatically
// dispatch messages at an interval. To do that, you'll want to return another
// Tick command after receiving your tick message. For example:
//
// type TickMsg time.Time
//
// func doTick() Cmd {
// return Tick(time.Second, func(t time.Time) Msg {
// return TickMsg(t)
// })
// }
//
// func (m model) Init() Cmd {
// // Start ticking.
// return doTick()
// }
//
// func (m model) Update(msg Msg) (Model, Cmd) {
// switch msg.(type) {
// case TickMsg:
// // Return your Tick command again to loop.
// return m, doTick()
// }
// return m, nil
// }
//
func Tick(d time.Duration, fn func(time.Time) Msg) Cmd { func Tick(d time.Duration, fn func(time.Time) Msg) Cmd {
return func() Msg { return func() Msg {
t := time.NewTimer(d) t := time.NewTimer(d)
@ -57,10 +113,10 @@ func Tick(d time.Duration, fn func(time.Time) Msg) Cmd {
// The Msg returned is the first non-nil message returned by a Cmd. // The Msg returned is the first non-nil message returned by a Cmd.
// //
// func saveStateCmd() Msg { // func saveStateCmd() Msg {
// if err := save(); err != nil { // if err := save(); err != nil {
// return errMsg{err} // return errMsg{err}
// } // }
// return nil // return nil
// } // }
// //
// cmd := Sequentially(saveStateCmd, Quit) // cmd := Sequentially(saveStateCmd, Quit)