Documentation
¶
Overview ¶
Package state provides a minimal global-state primitive: an observable value that can be read and written from anywhere - not just from within a Layout call, but a goroutine outside the render loop too (a network response, a timer) - and requests a redraw when it changes.
Reads are tracked: a cached part of the UI (giode.Memo) that read a State while it was built is rebuilt when that State changes, and only then.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RequestRedraw ¶
func RequestRedraw()
RequestRedraw asks the app for a new frame. It is safe to call from any goroutine; components that change on their own outside a frame (a toast shown from a background goroutine) use it.
func SetInvalidator ¶
func SetInvalidator(fn func())
SetInvalidator registers the function State calls after a value changes. The app does this once, in App.New; application code using State doesn't need to call it.
Types ¶
type Entry ¶
type Entry interface {
// Name is the registered name.
Name() string
// JSON returns the current value encoded as JSON.
JSON() ([]byte, error)
// SetJSON decodes data into a value of the State's type and sets
// it.
SetJSON(data []byte) error
}
Entry is a named State seen through a type-erased view, for tooling that inspects or edits application state (the MCP server).
type State ¶
type State[T any] struct { // contains filtered or unexported fields }
State is an observable value of type T, safe for concurrent use: read and write it from anywhere, including a goroutine outside the render loop, not just from within a Layout call. Create it once, typically as a package-level var or a field alongside a stateful component's own state, and share the pointer wherever the value is read or written.
func Named ¶
Named returns a State holding initial, registered under name so tooling - the built-in MCP server's state tools - can read and set it. Names must be unique; registering a name again replaces the earlier State.
func (*State[T]) Set ¶
func (s *State[T]) Set(v T)
Set replaces the value, notifies every watcher (in registration order, on the calling goroutine), and requests a redraw.
func (*State[T]) SetIfChanged ¶
SetIfChanged is Set, skipping the update - no watchers, no redraw, no cache invalidation - when v equals the current value. Values that aren't comparable with == are compared with reflect.DeepEqual.
func (*State[T]) Update ¶
func (s *State[T]) Update(fn func(current T) T)
Update atomically replaces the value with fn(current), notifies every watcher and requests a redraw - safe for a read-modify-write under concurrent access (e.g. incrementing a counter) where a separate Get then Set could race with another goroutine's write in between.
type Tracker ¶
type Tracker struct {
// contains filtered or unexported fields
}
Tracker records the States read while it is active. Trackers nest: a read is recorded by every active tracker, since a nested cache's dependencies are also its parents'.
func Track ¶
func Track() *Tracker
Track starts recording State reads into a new Tracker. Call Stop when done. Reads from any goroutine are recorded while a tracker is active; a spurious dependency only costs an extra rebuild.
func (*Tracker) Changed ¶
Changed reports whether any State read while t was active has changed since. A nil Tracker never changes.