Documentation
¶
Index ¶
- Variables
- func ArrayMap[T, U any](a *Array[T], fn func(T) U) []U
- func ArrayReduce[T, U any](a *Array[T], fn func(U, T) U, init U) U
- func ArraySort[T cmp.Ordered](a *Array[T])
- func ArraySortDesc[T cmp.Ordered](a *Array[T])
- func ArraySortFold[T any](a *Array[T], key func(T) string)
- func BindFormatter[T comparable](source *Ref[T]) (*Ref[string], WatchHandle)
- func BindFormatterf[T comparable](source *Ref[T], format string) (*Ref[string], WatchHandle)
- func Includes[T comparable](a *Array[T], item T) bool
- func Increment[T Numeric](r *Ref[T], delta T) func()
- func IndexOf[T comparable](a *Array[T], item T) int
- func Set[T comparable](r *Ref[T], val T) func()
- func Toggle(r *Ref[bool]) func()
- type Array
- func (a *Array[T]) At(i int) T
- func (a *Array[T]) Batch(fn func())
- func (a *Array[T]) Clear()
- func (a *Array[T]) Clone() *Array[T]
- func (a *Array[T]) Concat(others ...*Array[T]) *Array[T]
- func (a *Array[T]) Every(fn func(T) bool) bool
- func (a *Array[T]) Fill(v T, start, end int)
- func (a *Array[T]) Filter(fn func(T) bool) *Array[T]
- func (a *Array[T]) Find(fn func(T) bool) (T, bool)
- func (a *Array[T]) FindIndex(fn func(T) bool) int
- func (a *Array[T]) ForEach(fn func(i int, v T))
- func (a *Array[T]) Insert(i int, item T)
- func (a *Array[T]) Len() int
- func (a *Array[T]) LenRef() *Ref[int]
- func (a *Array[T]) Move(from, to int)
- func (a *Array[T]) OnAdded(fn func(index int, item T)) WatchHandle
- func (a *Array[T]) OnChange(fn func()) WatchHandle
- func (a *Array[T]) OnItemChanged(fn func(index int, item T)) WatchHandle
- func (a *Array[T]) OnMoved(fn func(from, to int)) WatchHandle
- func (a *Array[T]) OnRemoved(fn func(index int, item T)) WatchHandle
- func (a *Array[T]) OnReplaced(fn func()) WatchHandle
- func (a *Array[T]) Pop() T
- func (a *Array[T]) Push(items ...T)
- func (a *Array[T]) RandomItem() (T, bool)
- func (a *Array[T]) Remove(i int)
- func (a *Array[T]) RemoveAt(i int)
- func (a *Array[T]) Reserve(n int)
- func (a *Array[T]) Reverse()
- func (a *Array[T]) Set(items []T)
- func (a *Array[T]) SetAt(index int, item T)
- func (a *Array[T]) Shift() T
- func (a *Array[T]) Shrink()
- func (a *Array[T]) Shuffle()
- func (a *Array[T]) Slice(start, end int) []T
- func (a *Array[T]) Some(fn func(T) bool) bool
- func (a *Array[T]) Sort(less func(T, T) int)
- func (a *Array[T]) Splice(start, del int, items ...T)
- func (a *Array[T]) Swap(i, j int)
- func (a *Array[T]) Truncate(n int)
- func (a *Array[T]) Unshift(items ...T)
- func (a *Array[T]) Version() *Ref[uint64]
- type Computed
- type Numeric
- type Record
- func (r *Record) Delete(key string)
- func (r *Record) Get(key string) any
- func (r *Record) Has(key string) bool
- func (r *Record) Keys() []string
- func (r *Record) OnChange(fn func(key string, value any)) WatchHandle
- func (r *Record) OnFieldChange(key string, fn func(value any)) WatchHandle
- func (r *Record) Ref(key string) *Ref[any]
- func (r *Record) Set(key string, value any)
- func (r *Record) SetMany(fields map[string]any)
- func (r *Record) ToMap() map[string]any
- type Ref
- type Scheduler
- type WatchHandle
Constants ¶
This section is empty.
Variables ¶
var TrackingStack []*trackingFrame
TrackingStack is the package-level dependency tracking stack.
Functions ¶
func ArrayMap ¶
ArrayMap transforms each element of a using fn and returns a plain []U. The result is not reactive.
func ArrayReduce ¶
ArrayReduce folds a into a single value of type U using fn, starting from init.
func ArraySortDesc ¶
ArraySortDesc sorts a in descending order. T must satisfy cmp.Ordered.
func ArraySortFold ¶
ArraySortFold sorts a case-insensitively using key to extract a string from each element. Comparison uses Unicode simple case-folding so "Abc" == "abc" == "ABC".
func BindFormatter ¶
func BindFormatter[T comparable](source *Ref[T]) (*Ref[string], WatchHandle)
BindFormatter returns a *Ref[string] that stays in sync with source, converting values via fmt.Sprint.
The returned WatchHandle must be stopped when the binding is no longer needed (e.g. via screen.TrackRef or an explicit h.Stop() call). Dropping the handle leaks a watcher on source for the lifetime of source.
func BindFormatterf ¶
func BindFormatterf[T comparable](source *Ref[T], format string) (*Ref[string], WatchHandle)
BindFormatterf returns a *Ref[string] that stays in sync with source, converting values via fmt.Sprintf with the given format string.
The returned WatchHandle must be stopped when the binding is no longer needed (e.g. via screen.TrackRef or an explicit h.Stop() call). Dropping the handle leaks a watcher on source for the lifetime of source.
func Includes ¶
func Includes[T comparable](a *Array[T], item T) bool
Includes reports whether item is present in a. T must be comparable.
func IndexOf ¶
func IndexOf[T comparable](a *Array[T], item T) int
IndexOf returns the index of item in a, or -1 if absent. T must be comparable.
func Set ¶
func Set[T comparable](r *Ref[T], val T) func()
Set returns a func() that sets the ref to the given value.
Types ¶
type Array ¶
type Array[T any] struct { // contains filtered or unexported fields }
Array[T] is a reactive ordered collection. T is unconstrained (any). Every mutation bumps an internal version counter, notifying all WatchEffect and Computed subscribers through the normal reactive graph. Granular callbacks (OnAdded, OnRemoved, OnMoved, OnReplaced) carry operation detail for incremental or animated widget updates.
Array implements reactiveSource directly — no intermediate *Ref allocation. bumpVersion is a no-op when there are no reactive subscribers (common case).
func NewArrayFrom ¶
NewArrayFrom creates a reactive array from an existing slice. The input slice is copied — the caller retains ownership of the original.
func NewArrayFromAny ¶
NewArrayFromAny creates a reactive Array[any] from a typed slice, boxing each element. This avoids the need for callers to manually convert []T to []any.
func NewArrayWithCap ¶
NewArrayWithCap creates an empty reactive array with the given initial capacity.
func (*Array[T]) At ¶
At returns the element at index i. Supports negative indexing (At(-1) = last). Panics if out of bounds.
func (*Array[T]) Batch ¶
func (a *Array[T]) Batch(fn func())
Batch groups multiple mutations into a single notification. The outermost Batch controls the single notification; inner Batch calls while batching is already active run fn immediately without re-batching.
func (*Array[T]) Concat ¶
Concat returns a new Array with all elements from a followed by all elements from others.
func (*Array[T]) Find ¶
Find returns the first element satisfying fn, and true. Returns zero, false if not found.
func (*Array[T]) LenRef ¶
LenRef returns a *Ref[int] that reactively tracks the array's length. The Ref is allocated lazily on first call and kept in sync by bumpVersion. Because Ref.Set is a no-op when the value is unchanged, length-preserving mutations (SetAt, Swap, Sort, Reverse, …) never propagate through it.
func (*Array[T]) Move ¶
Move moves the element at from to index to. Elements between from and to shift one position to fill the gap.
[A, B, C, D] Move(0, 2) → [B, C, A, D] [A, B, C, D] Move(2, 0) → [C, A, B, D]
func (*Array[T]) OnAdded ¶
func (a *Array[T]) OnAdded(fn func(index int, item T)) WatchHandle
OnAdded registers a callback that fires when elements are added.
func (*Array[T]) OnChange ¶
func (a *Array[T]) OnChange(fn func()) WatchHandle
OnChange registers a callback that fires after every mutation. Returns a WatchHandle — call Stop() to unsubscribe.
func (*Array[T]) OnItemChanged ¶
func (a *Array[T]) OnItemChanged(fn func(index int, item T)) WatchHandle
OnItemChanged registers a callback that fires when a single item is updated in-place via SetAt.
func (*Array[T]) OnMoved ¶
func (a *Array[T]) OnMoved(fn func(from, to int)) WatchHandle
OnMoved registers a callback that fires when an element is moved.
func (*Array[T]) OnRemoved ¶
func (a *Array[T]) OnRemoved(fn func(index int, item T)) WatchHandle
OnRemoved registers a callback that fires when elements are removed.
func (*Array[T]) OnReplaced ¶
func (a *Array[T]) OnReplaced(fn func()) WatchHandle
OnReplaced registers a callback that fires when the array is restructured.
func (*Array[T]) Pop ¶
func (a *Array[T]) Pop() T
Pop removes and returns the last element. Panics if empty.
func (*Array[T]) Push ¶
func (a *Array[T]) Push(items ...T)
Push appends one or more items to the end.
func (*Array[T]) RandomItem ¶
RandomItem returns a random element, or zero/false if empty.
func (*Array[T]) Set ¶
func (a *Array[T]) Set(items []T)
Set replaces all contents with the provided slice (copied).
func (*Array[T]) SetAt ¶
SetAt replaces the item at index in-place and fires OnItemChanged. Panics if index is out of range.
func (*Array[T]) Shift ¶
func (a *Array[T]) Shift() T
Shift removes and returns the first element. Panics if empty.
func (*Array[T]) Shrink ¶
func (a *Array[T]) Shrink()
Shrink reallocates the underlying slice to exactly fit its contents.
func (*Array[T]) Shuffle ¶
func (a *Array[T]) Shuffle()
Shuffle randomizes the order of elements using Fisher-Yates.
func (*Array[T]) Slice ¶
Slice returns a copy of elements [start, end) and registers a reactive dependency.
func (*Array[T]) Splice ¶
Splice removes del elements starting at start, then inserts items. Fires OnRemoved (remove-only), OnAdded (insert-only), or OnReplaced (both).
func (*Array[T]) Swap ¶
Swap swaps the elements at indices i and j. Fires OnMoved once with (i, j) to signal the swap.
type Computed ¶
type Computed[T comparable] struct { // contains filtered or unexported fields }
Computed derives a value from reactive dependencies. It re-evaluates lazily: the function is only called on the next Get() after the node has been marked dirty.
func NewComputed ¶
func NewComputed[T comparable](fn func() T) *Computed[T]
NewComputed creates a new Computed that derives its value by calling fn. The function is evaluated eagerly once to establish initial dependencies.
type Numeric ¶
type Numeric interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
~float32 | ~float64
}
Numeric is a constraint for types that support arithmetic operations.
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record is a reactive key-value object. Each field is stored as a *Ref[any] so that WatchEffect and Computed callers tracking a specific field are not disturbed by unrelated field changes.
Record is not goroutine-safe — all mutations must occur on the game-loop goroutine.
func NewRecordFrom ¶
NewRecordFrom creates a Record pre-populated with fields. The map is copied — the caller retains ownership of the original.
func (*Record) Get ¶
Get returns the current value for key, or nil if the key is not set. Registers a reactive dependency when called inside a WatchEffect or Computed.
func (*Record) OnChange ¶
func (r *Record) OnChange(fn func(key string, value any)) WatchHandle
OnChange registers a callback that fires when any field changes, passing the key and new value. Returns a WatchHandle — call Stop() to unsubscribe.
func (*Record) OnFieldChange ¶
func (r *Record) OnFieldChange(key string, fn func(value any)) WatchHandle
OnFieldChange registers a callback that fires when the named field changes. The callback does NOT fire on registration — only on subsequent changes. Returns a WatchHandle — call Stop() to unsubscribe.
func (*Record) Ref ¶
Ref returns the internal *Ref[any] for key, creating the field with a nil value if it does not yet exist. Use inside WatchEffect or Computed to track a specific field reactively.
func (*Record) Set ¶
Set assigns value to key, creating the field if it doesn't exist. Fires onChange callbacks only when the value actually changes.
type Ref ¶
type Ref[T comparable] struct { // contains filtered or unexported fields }
Ref holds a single reactive value of type T. When the value changes, all subscribers (Computed nodes and Watches) are marked dirty and enqueued for the next scheduler flush.
func NewRef ¶
func NewRef[T comparable](initial T) *Ref[T]
NewRef creates a new Ref with the given initial value.
func (*Ref[T]) Get ¶
func (r *Ref[T]) Get() T
Get returns the current value and registers a dependency if called inside a Computed or Watch evaluation.
func (*Ref[T]) Peek ¶
func (r *Ref[T]) Peek() T
Peek returns the current value without registering a dependency.
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler batches reactive updates and flushes them once per frame.
var DefaultScheduler Scheduler
DefaultScheduler is the package-level scheduler instance used by all reactive primitives. Call DefaultScheduler.Flush() once per frame before the UI update pass.
type WatchHandle ¶
type WatchHandle struct {
// contains filtered or unexported fields
}
WatchHandle allows stopping a watch subscription.
func WatchEffect ¶
func WatchEffect(fn func()) WatchHandle
WatchEffect creates a watch that re-runs fn whenever any reactive value accessed inside fn changes. The function is run once immediately to establish initial dependencies.
func WatchValue ¶
func WatchValue[T comparable](ref *Ref[T], fn func(old, new T)) WatchHandle
WatchValue watches a specific Ref and calls fn with the old and new values. The callback fires immediately with (current, current) to prime the initial state, then with (old, new) on each subsequent change.
func (WatchHandle) Stop ¶
func (h WatchHandle) Stop()
Stop removes all subscriptions and prevents the watch from firing again.