ccontainer

package
v1.27.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 3 Imported by: 7

Documentation

Index

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 NewCContainerVT added in v1.11.2

func NewCContainerVT[T proto.EqualVT[T]](val T) *CContainer[T]

NewCContainerVT constructs a CContainer that uses VTEqual to check for equality.

func NewCContainerWithEqual added in v1.10.0

func NewCContainerWithEqual[T comparable](val T, isEqual func(a, b T) bool) *CContainer[T]

NewCContainerWithEqual builds a CContainer with an initial value and a comparator.

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).

Jump to

Keyboard shortcuts

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