Built-in subscriptions are clearer without the extra type definition

This commit is contained in:
Christian Rocha 2020-05-11 22:13:34 -04:00
parent b6eeef2127
commit 767f4bec2d
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 2 additions and 10 deletions

View File

@ -4,16 +4,10 @@ import (
"time" "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
// Every is a subscription that ticks with the system clock at the given // Every is a subscription that ticks with the system clock at the given
// duration, similar to cron. It's particularly useful if you have several // duration, similar to cron. It's particularly useful if you have several
// subscriptions that need to run in sync. // subscriptions that need to run in sync.
// func Every(duration time.Duration, newMsg func(time.Time) Msg) Sub {
// TODO: make it cancelable.
func Every(duration time.Duration, newMsg NewEveryMsg) Sub {
return func() Msg { return func() Msg {
n := time.Now() n := time.Now()
d := n.Truncate(duration).Add(duration).Sub(n) d := n.Truncate(duration).Add(duration).Sub(n)
@ -27,9 +21,7 @@ func Every(duration time.Duration, newMsg NewEveryMsg) Sub {
// Tick is a subscription that at an interval independent of the system clock // Tick is a subscription that at an interval independent of the system clock
// at the given duration. That is, it begins precisely when invoked. // at the given duration. That is, it begins precisely when invoked.
// func Tick(d time.Duration, newMsg func(time.Time) Msg) Sub {
// TODO: make it cancelable.
func Tick(d time.Duration, newMsg NewEveryMsg) Sub {
return func() Msg { return func() Msg {
t := time.NewTimer(d) t := time.NewTimer(d)
select { select {