state

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

func Unregister

func Unregister(name string)

Unregister removes a named State from the registry.

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).

func Entries

func Entries() []Entry

Entries returns every named State, sorted by name.

func Lookup

func Lookup(name string) (Entry, bool)

Lookup returns the named State registered with Named.

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

func Named[T any](name string, initial T) *State[T]

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 New

func New[T any](initial T) *State[T]

New returns a State holding initial.

func (*State[T]) Get

func (s *State[T]) Get() T

Get returns the current value.

func (*State[T]) Name

func (s *State[T]) Name() string

Name returns the name the State was registered under with Named, or "".

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

func (s *State[T]) SetIfChanged(v T) bool

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.

func (*State[T]) Version

func (s *State[T]) Version() uint64

Version returns a counter incremented by every change.

func (*State[T]) Watch

func (s *State[T]) Watch(fn func(T)) (unsubscribe func())

Watch registers fn to be called every time Set/Update changes the value. It returns an unsubscribe function.

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

func (t *Tracker) Changed() bool

Changed reports whether any State read while t was active has changed since. A nil Tracker never changes.

func (*Tracker) Len

func (t *Tracker) Len() int

Len returns the number of distinct States recorded.

func (*Tracker) Stop

func (t *Tracker) Stop()

Stop ends recording. Trackers must be stopped in reverse order of creation.

Jump to

Keyboard shortcuts

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