Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OnTick ¶
func OnTick[TickType any](ticks iter.Seq[TickType], task func(context.Context, TickType) error) error
OnTick calls task on every tick from the ticker. The function returns the last task error when the ticker is stopped, or task fails with [ErrStopped].
Example ¶
This example runs a loop, controlled by a periodic ticker. The called function is wrapped with a logger, that logs on every invocation and on error.
ticker := ticker.NewTimer(time.Second)
time.AfterFunc(2500*time.Millisecond, ticker.Stop)
startTime := time.Now()
// The tick function returns temporary error.
err := OnTick(ticker.Ticks(),
func(_ context.Context, tick time.Time) error {
fmt.Println("tick", tick.Sub(startTime).Round(time.Second))
return fmt.Errorf("oops")
})
fmt.Println(err)
Output: tick 0s tick 1s tick 2s oops
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.