state

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 2 Imported by: 3

README

state

Go Reference Go Report Card

state provides a very simple mechanism for creating, manipulating, and listening to observable state in Go. All state is based around interface

type State[T any] interface {
	Listen(func(T)) CancelFunc
}

This registers a listener to be called when new values are available, and returns a function that deregisters the listener. The function is also called immediately with the current value of the state, and the Listen() method does not return until the passed function does.

Mutable state adds a Set(T) method that sets the current state and informs all of the listeners.

On top of these, a large number of utility functions are provided, most producing derived forms of state that modify the state in some way before passing it to listeners.

Documentation

Overview

Package state provides mechanisms for creating and manipulating observable state.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T any](s State[T]) (v T)

Get returns the current value of a given state. If the state passed is nil, it returns the zero-value of T.

If the state passed has a Get method that returns a single value of type T, the return of that method is returned.

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

type MutableState[T any] interface {
	State[T]
	Setter[T]
}

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](from MutableState[F], 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

func Derived[T, F any](from State[F], m func(F) T) State[T]

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

func Static[T any](v T) State[T]

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](from State[T]) 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.

func UniqFunc

func UniqFunc[T any](from State[T], equal func(T, T) bool) State[T]

UniqFunc is like Uniq, but uses a custom comparison function to determine equality.

Jump to

Keyboard shortcuts

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