Skip to content
Myr

Add Tick to run timers independent of the system clock

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

authored by Christian Rocha · committed by Christian Rocha · parent 7b887b0 · Browse files · 1 file+17 added−2 removed
This commit

State

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

Diff stats

1 file+17 added−2 removed

  • other:+17 added−2 removed

1 file+17 added−2 removed

modified:subscriptions.go+17 added-2 removed
7 unmodified lines
8// at which the timer finished.9type NewEveryMsg func(time.Time) Msg1011// Every is a subscription that ticks with the system clock at the given12// duration.12// duration, similar to cron. It's particularly useful if you have several13// subscriptions that need to run in sync.14//14// TODO: make it cancelable15// TODO: make it cancelable.16func Every(duration time.Duration, newMsg NewEveryMsg) Sub {17 return func() Msg {18 n := time.Now()19 d := n.Truncate(duration).Add(duration).Sub(n)
3 unmodified lines
23 return newMsg(now)24 }25 }26}2728// Tick is a subscription that at an interval independent of the system clock29// at the given duration. That is, it begins precisely when invoked.30//31// TODO: make it cancelable.32func Tick(d time.Duration, newMsg NewEveryMsg) Sub {33 return func() Msg {34 t := time.NewTimer(d)35 select {36 case now := <-t.C:37 return newMsg(now)38 }39 }40}