Documentation ¶
Index ¶
- func WatchChanges[T comparable](ctx context.Context, initialVal T, ctr Watchable[T], ...) error
- type CContainer
- func (c *CContainer[T]) GetValue() T
- func (c *CContainer[T]) SetValue(val T)
- func (c *CContainer[T]) SwapValue(cb func(val T) T) T
- func (c *CContainer[T]) WaitValue(ctx context.Context, errCh <-chan error) (T, error)
- func (c *CContainer[T]) WaitValueChange(ctx context.Context, old T, errCh <-chan error) (T, error)
- func (c *CContainer[T]) WaitValueEmpty(ctx context.Context, errCh <-chan error) error
- func (c *CContainer[T]) WaitValueWithValidator(ctx context.Context, valid func(v T) (bool, error), errCh <-chan error) (T, error)
- type Watchable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WatchChanges ¶ added in v1.0.4
func WatchChanges[T comparable]( ctx context.Context, initialVal T, ctr Watchable[T], updateCb func(msg T) error, errCh <-chan error, ) error
WatchChanges watches a Watchable and calls the callback when the value changes. Note: the value pointer must change to trigger an update.
initial is the initial value to wait for changes on. set initial to nil to wait for value != nil.
T is the type of the message. errCh is an optional error channel to interrupt the operation.
Types ¶
type CContainer ¶
type CContainer[T comparable] struct { // contains filtered or unexported fields }
CContainer is a concurrent container.
func NewCContainer ¶
func NewCContainer[T comparable](val T) *CContainer[T]
NewCContainer builds a CContainer with an initial value.
func (*CContainer[T]) GetValue ¶
func (c *CContainer[T]) GetValue() T
GetValue returns the immediate value of the container.
func (*CContainer[T]) SetValue ¶
func (c *CContainer[T]) SetValue(val T)
SetValue sets the ccontainer value.
func (*CContainer[T]) SwapValue ¶
func (c *CContainer[T]) SwapValue(cb func(val T) T) T
SwapValue locks the container, calls the callback, and stores the return value.
Returns the updated value. If cb is nil returns the current value without changes.
func (*CContainer[T]) WaitValue ¶
func (c *CContainer[T]) WaitValue(ctx context.Context, errCh <-chan error) (T, error)
WaitValue waits for any non-nil value in the container. errCh is an optional channel to read an error from.
func (*CContainer[T]) WaitValueChange ¶
func (c *CContainer[T]) WaitValueChange(ctx context.Context, old T, errCh <-chan error) (T, error)
WaitValueChange waits for a value that is different than the given. errCh is an optional channel to read an error from.
func (*CContainer[T]) WaitValueEmpty ¶
func (c *CContainer[T]) WaitValueEmpty(ctx context.Context, errCh <-chan error) error
WaitValueEmpty waits for an empty value. errCh is an optional channel to read an error from.
func (*CContainer[T]) WaitValueWithValidator ¶
func (c *CContainer[T]) WaitValueWithValidator( ctx context.Context, valid func(v T) (bool, error), errCh <-chan error, ) (T, error)
WaitValueWithValidator waits for any value that matches the validator in the container. errCh is an optional channel to read an error from.
type Watchable ¶
type Watchable[T comparable] interface { // GetValue returns the current value. GetValue() T // WaitValueWithValidator waits for any value that matches the validator in the container. // errCh is an optional channel to read an error from. WaitValueWithValidator( ctx context.Context, valid func(v T) (bool, error), errCh <-chan error, ) (T, error) // WaitValue waits for any non-nil value in the container. // errCh is an optional channel to read an error from. WaitValue(ctx context.Context, errCh <-chan error) (T, error) // WaitValueChange waits for a value that is different than the given. // errCh is an optional channel to read an error from. WaitValueChange(ctx context.Context, old T, errCh <-chan error) (T, error) // WaitValueEmpty waits for an empty value. // errCh is an optional channel to read an error from. WaitValueEmpty(ctx context.Context, errCh <-chan error) error }
Watchable is an interface implemented by ccontainer for watching a value.
func ToWatchable ¶
func ToWatchable[T comparable](ctr *CContainer[T]) Watchable[T]
ToWatchable converts a ccontainer to a Watchable (somewhat read-only).