watcher

package
v0.0.0-...-97ebcb8 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: Apache-2.0, BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Always

func Always[T any](old *T, new T) bool

Always is the default updater function. It just assigns new to old and returns true.

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

type UpdateFunc[T any] func(old *T, new T) bool

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 NewValue

func NewValue[T any](initial T) *Value[T]

NewValue creates a new Value holding the given initial value.

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

func (v *Value[T]) Close() error

Close closes the Value, unblocking any outstanding watchers. Close always returns nil.

func (*Value[T]) Closed

func (v *Value[T]) Closed() bool

Closed reports whether the value has been closed.

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.

func (*Value[T]) GetOK

func (v *Value[T]) GetOK() (T, bool)

GetOK returns the most recently set value and reports whether it is valid. After v has been closed, GetOK will always return *new(T), false.

func (*Value[T]) Set

func (v *Value[T]) Set(val T)

Set sets the shared value to val.

func (*Value[T]) Watch

func (v *Value[T]) Watch() *Watcher[T]

Watch returns a Watcher that can be used to watch for changes to the 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

func (w *Watcher[T]) Next() bool

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

func (*Watcher[T]) Value

func (w *Watcher[T]) Value() T

Value returns the last value that was retrieved from the watched Value by Next.

Jump to

Keyboard shortcuts

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