Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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

authored by Carlos Alexandro Becker · committed by GitHub · parent a5f28a3 · Browse files · 1 file+60 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Christian Rocha <christian@rocha.is>

Diff stats

1 file+60 added−4 removed

  • other:+60 added−4 removed

1 file+60 added−4 removed

modified:commands.go+60 added-4 removed
22 unmodified lines
23//24// cmd := Every(time.Second, func(t time.Time) Msg {25// return TickMsg(t)26// })27//28// Beginners' note: Every sends a single message and won't automatically29// dispatch messages at an interval. To do that, you'll want to return another30// Every command after receiving your tick message. For example:31//32// type TickMsg time.Time33//34// // Send a message every second.35// func tickEvery() Cmd {36// return Every(time.Second, func(t time.Time) Msg {37// return TickMsg(t)38// })39// }40//41// func (m model) Init() Cmd {42// // Start ticking.43// return tickEvery()44// }45//46// func (m model) Update(msg Msg) (Model, Cmd) {47// switch msg.(type) {48// case TickMsg:49// // Return your Every command again to loop.50// return m, tickEvery()51// }52// return m, nil53// }54//55// Every is analogous to Tick in the Elm Architecture.56func Every(duration time.Duration, fn func(time.Time) Msg) Cmd {57 return func() Msg {58 n := time.Now()59 d := n.Truncate(duration).Add(duration).Sub(n)
13 unmodified lines
73//74// cmd := Tick(time.Second, func(t time.Time) Msg {75// return TickMsg(t)76// })77//78// Beginners' note: Tick sends a single message and won't automatically79// dispatch messages at an interval. To do that, you'll want to return another80// Tick command after receiving your tick message. For example:81//82// type TickMsg time.Time83//84// func doTick() Cmd {85// return Tick(time.Second, func(t time.Time) Msg {86// return TickMsg(t)87// })88// }89//90// func (m model) Init() Cmd {91// // Start ticking.92// return doTick()93// }94//95// func (m model) Update(msg Msg) (Model, Cmd) {96// switch msg.(type) {97// case TickMsg:98// // Return your Tick command again to loop.99// return m, doTick()100// }101// return m, nil102// }103//104func Tick(d time.Duration, fn func(time.Time) Msg) Cmd {105 return func() Msg {106 t := time.NewTimer(d)107 return fn(<-t.C)
4 unmodified lines
112// commands.113// The Msg returned is the first non-nil message returned by a Cmd.114//115// func saveStateCmd() Msg {60// if err := save(); err != nil {61// return errMsg{err}62// }63// return nil116// if err := save(); err != nil {117// return errMsg{err}118// }119// return nil120// }121//122// cmd := Sequentially(saveStateCmd, Quit)123//
13 unmodified lines