Documentation
¶
Overview ¶
Package rx is reactive state in the style of Svelte 5 runes. State is a writable signal, Derived a lazy memoized computation over other signals, Effect a side effect that runs again when something it read has changed. Dependencies are whatever a Derived or an Effect reads with Get during its last run; nothing is declared.
Everything except State.Post belongs to one goroutine, the UI thread. gowt.Run attaches rx to its event loop: effects made stale by a Set run once per batch, after the current event handler, and Post hands values over from other goroutines with one hop per batch.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Attach ¶
func Attach(prepare func(tick func()) (wake func())) (detach func())
Attach binds rx to an event loop and makes the calling goroutine its UI thread. prepare gets the function that must run on that thread and returns wake, which queues one call of it and is safe from any goroutine. gowt.Run calls Attach; detach unbinds before the loop stops.
func Batch ¶
func Batch(fn func())
Batch runs fn and then flushes, for code that needs the widgets updated when it returns; writes from event handlers are batched per event-loop turn anyway.
func Effect ¶
func Effect(fn func()) (dispose func())
Effect runs fn now, so a bound widget has its value before the first layout, then once per Flush after a source it read changed. What fn creates, and the OnCleanup funcs it registers, end before the next run and on dispose. Inside an Effect or a Scope the new effect is owned by it. A panic in the first run disposes the effect and reaches the caller.
func Flush ¶
func Flush()
Flush runs every stale effect until none is left, parents before their children. A panic in one effect does not stop the others: Flush finishes, then panics with the first value (later ones go to stderr). Effects that keep each other stale for 100 rounds are a cycle and panic too. Inside an Effect, Flush and Batch only schedule: the running effect would be entered again.
func OnCleanup ¶
func OnCleanup(f func())
OnCleanup registers f with the running Effect or Derived, to run before its next run and on dispose, or with the Scope being built, to run on dispose. Elsewhere f never runs.
Types ¶
type Derived ¶
type Derived[T any] struct { // contains filtered or unexported fields }
Derived is a lazy memoized computation over other signals. It recomputes when read after a source changed; an equal result stops the change from reaching its observers.
func Compute ¶
func Compute[T comparable](fn func() T) *Derived[T]
Compute makes a Derived compared with ==.
func ComputeFunc ¶
ComputeFunc makes a Derived with a custom equality; eq nil means every recompute propagates. Inside an Effect or a Scope the Derived is owned by it and stops updating when disposed.
type Reader ¶
type Reader[T any] interface { Get() T Peek() T }
Reader is what bindings take: a State or a Derived.
type State ¶
type State[T any] struct { // contains filtered or unexported fields }
State is a writable signal. Its zero value is not usable; make one with New or NewFunc.
func New ¶
func New[T comparable](v T) *State[T]
New makes a State compared with ==: a Set of an equal value does nothing.
func NewFunc ¶
NewFunc makes a State with a custom equality; eq nil means every Set notifies, which suits slices and other values that are replaced, not mutated.
func (*State[T]) Get ¶
func (s *State[T]) Get() T
Get returns the value and subscribes the Derived or Effect that is running.
func (*State[T]) Post ¶
func (s *State[T]) Post(v T)
Post stores v for the UI thread; it is the only call that is safe from any goroutine. Posts before the next tick keep only the last value per State and are applied in one batch. Before a loop is attached, and after it is detached, values wait for the next Attach.