refreshable

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Map

func Map[T any, M any](original Refreshable[T], mapFn func(T) M) (Refreshable[M], UnsubscribeFunc)

Map returns a new Refreshable based on the current one that handles updates based on the current Refreshable.

func MapWithError

func MapWithError[T any, M any](original Refreshable[T], mapFn func(T) (M, error)) (Validated[M], UnsubscribeFunc, error)

MapWithError is similar to Validate but allows for the function to return a mapping/mutation of the input object in addition to returning an error. The returned validRefreshable will contain the mapped value. An error is returned if the current original value fails to map.

func NewFromTickerFunc

func NewFromTickerFunc[T any](ctx context.Context, interval time.Duration, provider func(ctx context.Context) (T, bool)) (Ready[T], UnsubscribeFunc)

NewFromTickerFunc returns a Ready Refreshable populated by the result of the provider called each interval. If the providers bool return is false, the value is ignored. The result's ReadyC channel is closed when a new value is populated. The refreshable will stop updating when the provided context is cancelled or the returned UnsubscribeFunc func is called.

func Validate

func Validate[T any](original Refreshable[T], validatingFn func(T) error) (Validated[T], UnsubscribeFunc, error)

Validate returns a new Refreshable that returns the latest original value accepted by the validatingFn. If the upstream value results in an error, it is reported by Validation(). An error is returned if the current original value is invalid.

func Wait

func Wait[T any](ctx context.Context, ready Ready[T]) (T, bool)

Wait waits until the Ready has a current value or the context expires.

Types

type Ready

type Ready[T any] interface {
	Refreshable[T]
	// ReadyC returns a channel which is closed after a value is successfully populated.
	ReadyC() <-chan struct{}
}

Ready extends Refreshable for asynchronous implementations which may not have a value when they are constructed. Callers should check that the Ready channel is closed before using the Current value.

func NewFromChannel

func NewFromChannel[T any](values <-chan T) Ready[T]

NewFromChannel populates an Updatable with the values channel. If an element is already available, the returned Value is guaranteed to be populated. The channel should be closed when no longer used to avoid leaking resources.

type Refreshable

type Refreshable[T any] interface {
	// Current returns the most recent value of this Refreshable.
	// If the value has not been initialized, returns T's zero value.
	Current() T

	// Subscribe calls the consumer function when Value updates until stop is closed.
	// The consumer must be relatively fast: Updatable.Set blocks until all subscribers have returned.
	// Expensive or error-prone responses to refreshed values should be asynchronous.
	// Updates considered no-ops by reflect.DeepEqual may be skipped.
	// When called, consumer is executed with the Current value.
	Subscribe(consumer func(T)) UnsubscribeFunc
}

A Refreshable is a generic container type for a volatile underlying value. It supports atomic access and user-provided callback "subscriptions" on updates.

func MapContext

func MapContext[T any, M any](ctx context.Context, original Refreshable[T], mapFn func(T) M) Refreshable[M]

MapContext is like Map but unsubscribes when the context is cancelled.

type UnsubscribeFunc

type UnsubscribeFunc func()

UnsubscribeFunc removes a subscription from a refreshable's internal tracking and/or stops its update routine. It is safe to call multiple times.

type Updatable

type Updatable[T any] interface {
	Refreshable[T]
	// Update updates the Refreshable with a new T.
	// It blocks until all subscribers have completed.
	Update(T)
}

A Updatable is a Refreshable which supports setting the value with a user-provided value. When a utility returns a (non-Updatable) Refreshable, it implies that value updates are handled internally.

func New

func New[T any](val T) Updatable[T]

New returns a new Updatable that begins with the given value.

type Validated

type Validated[T any] interface {
	Refreshable[T]
	// Validation returns the result of the most recent validation.
	// If the last value was valid, Validation returns the same value as Current and a nil error.
	// If the last value was invalid, it and the error are returned. Current returns the most recent valid value.
	Validation() (T, error)
}

A Validated is a Refreshable capable of rejecting updates according to validation logic. Its Current method returns the most recent value to pass validation.

Jump to

Keyboard shortcuts

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