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"
)
// 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
// duration, similar to cron. It's particularly useful if you have several
// subscriptions that need to run in sync.
//
// TODO: make it cancelable.
func Every(duration time.Duration, newMsg NewEveryMsg) Sub {
func Every(duration time.Duration, newMsg func(time.Time) Msg) Sub {
return func() Msg {
n := time.Now()
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
// at the given duration. That is, it begins precisely when invoked.
//
// TODO: make it cancelable.
func Tick(d time.Duration, newMsg NewEveryMsg) Sub {
func Tick(d time.Duration, newMsg func(time.Time) Msg) Sub {
return func() Msg {
t := time.NewTimer(d)
select {