Documentation ¶
Overview ¶
Package state provides mechanisms for creating and manipulating observable state.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CancelFunc ¶
type CancelFunc func()
type Listenable ¶
type Listenable[T any] struct { // contains filtered or unexported fields }
Listenable implements a registerable list of listening functions.
Listenable's methods are thread-safe.
func (*Listenable[T]) Add ¶
func (lis *Listenable[T]) Add(f func(T)) uint32
Add registers a listener function, returning an ID that can be used to remove it later.
func (*Listenable[T]) Remove ¶
func (lis *Listenable[T]) Remove(id uint32)
Remove deregisters the listener function with the given ID.
func (*Listenable[T]) Send ¶
func (lis *Listenable[T]) Send(v T)
Send calls all of the registered listener functions with the given value. It does not return until all of the registered functions do.
TODO: Don't send to the listener that triggered the send?
type MutableState ¶
MutableState is a State that can be changed. There is no guarantee about the relationship between the Set method returning and the State's listeners being informed of the update.
func Mutable ¶
func Mutable[T any](v T) MutableState[T]
func Mutator ¶
func Mutator[T, F any, FS MutableState[F]](from FS, mapGet func(F) T, mapSet func(T) F) MutableState[T]
Mutator returns a mutable derived state. Like Derived, the returned state runs successive values through mapGet, but it can also be set and, when set, runs the new value through mapSet before setting the underlying state.
type Setter ¶
type Setter[T any] interface { Set(v T) }
Setter represents a value that can be changed. Setting a value is thread-safe with reading it.
type State ¶
type State[T any] interface { // Listen registers a listener function to be called whenever the // State's value changes. // // When Listen is first called, it immediately calls the provided // function with the current value of the state and does not return // until after the provided function returns. Listen(func(T)) CancelFunc }
State represents a value that can change over time.
func Derived ¶
Derived returns a read-only state that derives its values from another state, passing them through the mapping function m. In other words, when from's value changes, the derived state's listeners will be called with that new value passed through m.
func Static ¶
Static returns a state that never changes. If a value is definitely constant, using this type of state is more efficient than a mutable one.
func Uniq ¶
func Uniq[T comparable, S State[T]](from S) State[T]
Uniq returns a State that wraps from. Listeners of the returned state will only see the first of successive values that are equal to each other, similar to the Unix uniq command.