rx

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: EPL-2.0 Imports: 7 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 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 Catch

func Catch(fn func(), handle func(r any)) (dispose func())

Catch runs fn as Scope does and owns what fn creates. A panic in an Effect it owns, at any depth, during a Flush goes to handle instead of to the caller of Flush; the nearest Catch above the effect takes it. handle runs on the UI thread while the stack still shows where the panic was raised, and should dispose the scope or the part that failed. A panic in handle goes to the next Catch above, or to Flush's caller. A panic in the first run of an Effect still reaches the code that made it.

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.

func Pending

func Pending() int

Pending counts the work waiting for a tick: stale effects and State.Post values not applied yet. For a test that waits for the UI to go quiet. UI thread only.

func Scope

func Scope(fn func()) (dispose func())

Scope runs fn untracked and owns what fn creates: dispose disposes it all, for effects that must die together, such as those of one window. A Scope made inside an Effect belongs to that run and is disposed with it.

func Untrack

func Untrack(fn func())

Untrack calls fn without subscribing the current Derived or Effect to what fn reads.

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

func ComputeFunc[T any](fn func() T, eq func(a, b T) bool) *Derived[T]

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.

func (*Derived[T]) Get

func (d *Derived[T]) Get() T

Get returns the value, recomputing it if a source changed, and subscribes the running Derived or Effect.

func (*Derived[T]) Peek

func (d *Derived[T]) Peek() T

Peek is Get without subscribing.

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

func NewFunc[T any](v T, eq func(a, b T) bool) *State[T]

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]) Peek

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

Peek returns the value without subscribing.

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.

func (*State[T]) Set

func (s *State[T]) Set(v T)

Set stores v. Observers are marked now, so a Derived read right after sees v; effects run at the next Flush. Set panics off the UI thread (use Post) and inside a Derived.

func (*State[T]) Update

func (s *State[T]) Update(fn func(T) T)

Update sets fn(current value).

Jump to

Keyboard shortcuts

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