stopvar

package
v0.0.0-...-4dcfcdd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package stopvar contains helpers that build on both the stopper and notify packages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoWhenChanged

func DoWhenChanged[T comparable](
	ctx *stopper.Context,
	start T,
	source *notify.Var[T],
	fn func(ctx *stopper.Context, old, new T) error,
) (last T, err error)

DoWhenChanged executes the callback when the variable has changed to a different value. That is, if the variable is set to existing value, the callback will not be be invoked. If an error is returned from the callback, the last successfully-processed value will be returned.

func DoWhenChangedOrInterval

func DoWhenChangedOrInterval[T comparable](
	ctx *stopper.Context,
	start T,
	source *notify.Var[T],
	period time.Duration,
	fn func(ctx *stopper.Context, old, new T) error,
) (last T, err error)

DoWhenChangedOrInterval executes the callback when the variable has changed or if the configured period of time has elapsed since the last invocation. This is useful when some activity should be taken in response to a change or at a somewhat regular interval. If an error is returned from the callback, the last successfully-processed value will be returned.

func WaitForChange

func WaitForChange[T comparable](
	ctx *stopper.Context, current T, source *notify.Var[T],
) (next T, changed <-chan struct{})

WaitForChange is a utility function that waits for the source to change to another value. If the context is stopped, the most recent value will be returned.

func WaitForChangeOrDuration

func WaitForChangeOrDuration[T comparable](
	ctx *stopper.Context, current T, source *notify.Var[T], d time.Duration,
) (next T, changed <-chan struct{})

WaitForChangeOrDuration is a utility function that waits for the source to change to another value or for the given duration to elapse.

func WaitForValue

func WaitForValue[T comparable](ctx *stopper.Context, expected T, source *notify.Var[T]) error

WaitForValue is a utility function that waits until the source emits the requested value. This is primarily intended for testing.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL