Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IfUnequal ¶
func IfUnequal[T comparable](old *T, new T) bool
IfUnequal reports a value as having changed only if new != *old.
Types ¶
type UpdateFunc ¶
UpdateFunc is the type of a function used to update a value. It should update old to be the same as new and report whether old has changed.
type Value ¶
type Value[T any] struct { // contains filtered or unexported fields }
Value represents a shared value that can be watched for changes. Methods on a Value may be called concurrently.
The zero Value is ok to use; watchers on the zero value will block until Set is called.
By default, watchers will be notified whenever Set is called, but WithUpdater can be used to trigger notifications less often.
func WithUpdater ¶
func WithUpdater[T any](updater UpdateFunc[T]) *Value[T]
WithUpdater returns a value that uses the given function to update the Value and report whether it's changed.
The default updater is Always.
func (*Value[T]) Close ¶
Close closes the Value, unblocking any outstanding watchers. Close always returns nil.
func (*Value[T]) Get ¶
func (v *Value[T]) Get() T
Get returns the current value. If the watcher has been closed, it returns the zero value.
type Watcher ¶
type Watcher[T any] struct { // contains filtered or unexported fields }
Watcher represents a single watcher of a shared value.
func (*Watcher[T]) Close ¶
func (w *Watcher[T]) Close()
Close closes the Watcher without closing the underlying value. It may be called concurrently with Next.
func (*Watcher[T]) Next ¶
Next blocks until there is a new value to be retrieved from the value that is being watched. It also unblocks when the value or the Watcher itself is closed. Next returns false if the value or the Watcher itself have been closed.
Example ¶
var v Value[string] // The channel is not necessary for normal use of the watcher. // It just makes the test output predictable. ch := make(chan bool) go func() { for x := 0; x < 3; x++ { v.Set(fmt.Sprintf("value%d", x)) ch <- true } v.Close() }() w := v.Watch() for w.Next() { fmt.Println(w.Value()) <-ch }
Output: value0 value1 value2