Documentation
¶
Overview ¶
Package kites implements a lightweight, thread-safe external state store (similar to Zustand or Redux) for global state management outside of the VDOM tree.
It follows the State-Method + Higher-Order Action pattern on *Store[T]: state structs define pointer-receiver mutation methods (func (s *State) Inc()), which are passed as method expressions to Store.Update, Store.Action, Bind, and Action. Stores support copy-on-write isolation for mutable slices/maps, scoped dependency injection and parallel test isolation through Context[T] and Provider, selector-filtered non-UI subscriptions via SubscribeSelector, deterministic async waiting via WaitFor, and automatic action-name tracking for DevTools (extras/kites/kitesdt). It integrates with the kitex framework through Use, UseWith, UseContext, and Store.Use.
Index ¶
- func Action[T any, A any](s *Store[T], method func(*T, A)) func(A)
- func Bind[T any, A any](method func(*T, A), arg A) func(*T)
- func CleanState(val any) any
- func Select[T any, A any, U any](selector func(T, A) U, arg A) func(T) U
- func SubscribeSelector[T any, U any](s *Store[T], selector func(T) U, listener func(newVal, oldVal U)) func()
- func SubscribeSelectorWith[T any, U any](s *Store[T], selector func(T) U, isEqual func(a, b U) bool, ...) func()
- func Use[T any, U any](s *Store[T], selector func(T) U) U
- func UseContext[T any, U any](ctx *Context[T], selector func(T) U) U
- func UseContextWith[T any, U any](ctx *Context[T], selector func(T) U, isEqual func(a, b U) bool) U
- func UseWith[T any, U any](s *Store[T], selector func(T) U, isEqual func(a, b U) bool) U
- func WaitFor[T any](ctx context.Context, s *Store[T], predicate func(T) bool) (T, error)
- type Context
- type InspectableStore
- type Store
- func (s *Store[T]) Action(mutators ...func(*T)) func()
- func (s *Store[T]) Get() T
- func (s *Store[T]) Initial() T
- func (s *Store[T]) InspectInitial() any
- func (s *Store[T]) InspectState() any
- func (s *Store[T]) InspectTypeName() string
- func (s *Store[T]) Reset()
- func (s *Store[T]) Set(updater func(T) T)
- func (s *Store[T]) SetState(next T)
- func (s *Store[T]) SetStateAny(val any) error
- func (s *Store[T]) Subscribe(listener func(newVal T, oldVal T)) func()
- func (s *Store[T]) SubscribeAction(listener func(action string, newVal T, oldVal T)) func()
- func (s *Store[T]) SubscribeActionAny(listener func(action string, newVal, oldVal any)) func()
- func (s *Store[T]) SubscribeAny(listener func(newVal, oldVal any)) func()
- func (s *Store[T]) SubscriberCount() int
- func (s *Store[T]) Update(mutators ...func(*T))
- func (s *Store[T]) Use() T
- type StoreHistorySnapshot
- type StoreSnapshot
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Action ¶
Action curries a 1-argument state method func(*T, A) against store s, returning a callable func(A) that calls s.Update(Bind(method, arg)) and preserves the method name in DevTools.
func Bind ¶
Bind curries a 1-argument state method func(*T, A) (or helper function) with arg into a func(*T) mutator while preserving the original method name for DevTools tracking.
func CleanState ¶
CleanState recursively converts an arbitrary Go value into a JSON-serializable representation, stripping non-serializable chan/unsafe.Pointer fields and representing non-nil functions as "ƒ () {}".
func Select ¶
Select curries a 1-argument value-receiver selector method func(T, A) U with arg into a standard func(T) U selector for Use, UseContext, and SubscribeSelector.
func SubscribeSelector ¶
func SubscribeSelector[T any, U any](s *Store[T], selector func(T) U, listener func(newVal, oldVal U)) func()
SubscribeSelector subscribes to a specific slice of the store selected by selector. The listener is invoked only when the selected slice changes according to default equality (== for comparable types, reflect.DeepEqual otherwise). Returns an unsubscribe function.
func SubscribeSelectorWith ¶
func SubscribeSelectorWith[T any, U any](s *Store[T], selector func(T) U, isEqual func(a, b U) bool, listener func(newVal, oldVal U)) func()
SubscribeSelectorWith subscribes to a specific slice of the store using a custom equality function isEqual.
func Use ¶
Use integrates the external *kites.Store with the kitex VDOM tree. It retrieves the current slice of the store using the provided selector, and subscribes to store changes using a thread-safe external-store bridge (modeled after React's useSyncExternalStore).
Selectors can be inline closures or value-receiver method expressions on T (e.g. AppState.SelectCounter, or parameterized via kites.Select(AppState.ItemByID, id)). Selector outputs may be comparable primitives, structs, slices, or maps. If the selected slice has not changed between updates, Use bails out without marking the component dirty or scheduling a VDOM re-render.
Note (Go 1.27+ roadmap): Once Kite upgrades to Go 1.27 (when Go 1.28 is released) where generic methods are supported, selector-based Use and UseWith will also be exposed directly as generic methods on *Store[T] and *Context[T].
func UseContext ¶
UseContext selects a slice U from the store in ctx and subscribes the component to changes in that slice.
func UseContextWith ¶
UseContextWith selects a slice U from the store in ctx using a custom equality comparator isEqual.
Types ¶
type Context ¶
type Context[T any] struct { // contains filtered or unexported fields }
Context holds a scoped *Store[T] for dependency injection and parallel test isolation via Provider.
func NewContext ¶
NewContext creates a store Context initialized with a default store holding initialState.
func (*Context[T]) Action ¶
func (c *Context[T]) Action(mutators ...func(*T)) func()
Action returns a zero-argument callback that invokes Update(mutators...) on the active store in ctx.
func (*Context[T]) Provider ¶
Provider mounts an isolated *Store[T] instance for the given VDOM subtree.
type InspectableStore ¶
type InspectableStore interface {
SubscriberCount() int
Reset()
InspectTypeName() string
InspectState() any
InspectInitial() any
SetStateAny(state any) error
SubscribeAny(listener func(newVal, oldVal any)) func()
SubscribeActionAny(listener func(action string, newVal, oldVal any)) func()
}
InspectableStore is implemented by *Store[T]. It exposes type-erased inspection, subscription, reset, and time-travel restore hooks for DevTools integrations such as kitesdt.
type Store ¶
type Store[T any] struct { // contains filtered or unexported fields }
Store holds the state of type T and manages subscribers to changes. It is fully thread-safe and can be used both inside and outside the kitex VDOM tree.
func (*Store[T]) Action ¶
func (s *Store[T]) Action(mutators ...func(*T)) func()
Action returns a zero-argument callback that invokes s.Update(mutators...). This enables passing Go method expressions on *T directly to UI event handlers:
OnPress: store.Action((*AppState).Increment) OnPress: store.Action(kites.Bind((*AppState).AddCount, 10))
func (*Store[T]) Initial ¶
func (s *Store[T]) Initial() T
Initial returns a snapshot of the store's initial state.
func (*Store[T]) InspectInitial ¶
InspectInitial returns a JSON-safe snapshot of the store's initial state.
func (*Store[T]) InspectState ¶
InspectState returns a JSON-safe snapshot of the current store state.
func (*Store[T]) InspectTypeName ¶
InspectTypeName returns the Go type name of the store's state type T.
func (*Store[T]) Reset ¶
func (s *Store[T]) Reset()
Reset restores the store's state to a fresh copy of its initial state and notifies subscribers.
func (*Store[T]) Set ¶
func (s *Store[T]) Set(updater func(T) T)
Set updates the state using the provided updater function. It passes an isolated copy-on-write draft of the current state to updater under a write lock so that in-place mutations to slices, maps, or data pointers inside updater never race with concurrent readers (such as the UI drawing thread) or corrupt previous snapshots. Subscribers are notified after unlocking.
func (*Store[T]) SetState ¶
func (s *Store[T]) SetState(next T)
SetState replaces the store's state with next and notifies subscribers.
func (*Store[T]) SetStateAny ¶
SetStateAny replaces the store's state with a dynamically typed snapshot of type T.
func (*Store[T]) Subscribe ¶
func (s *Store[T]) Subscribe(listener func(newVal T, oldVal T)) func()
Subscribe registers a listener callback to be invoked when the state changes. It returns an unsubscribe function that removes the listener.
func (*Store[T]) SubscribeAction ¶
SubscribeAction registers a listener callback invoked with the resolved action name and state transition snapshots whenever the store mutates.
func (*Store[T]) SubscribeActionAny ¶
SubscribeActionAny registers a type-erased listener notified with the resolved action name and state snapshots on every mutation.
func (*Store[T]) SubscribeAny ¶
SubscribeAny registers a type-erased listener notified on state transitions. When the state type contains mutable reference types, isolated clones are passed to the listener so stored snapshots remain safe for time-travel restoration.
func (*Store[T]) SubscriberCount ¶
SubscriberCount returns the number of active subscribers attached to the store.
func (*Store[T]) Update ¶
func (s *Store[T]) Update(mutators ...func(*T))
Update mutates the state in place using one or more pointer mutators applied sequentially to a single isolated copy-on-write draft under a single lock, commits the result, and notifies subscribers once.
Action names are automatically extracted from the passed method expressions, bound mutators (via Bind), or named helper functions for DevTools tracking.
type StoreHistorySnapshot ¶
type StoreHistorySnapshot struct {
Seq uint64 `json:"seq"`
Timestamp string `json:"timestamp"`
Action string `json:"action"`
State any `json:"state"`
PrevState any `json:"prevState,omitempty"`
}
StoreHistorySnapshot represents a single recorded state transition in a store's timeline.
type StoreSnapshot ¶
type StoreSnapshot struct {
ID string `json:"id"`
Name string `json:"name"`
TypeName string `json:"typeName"`
State any `json:"state"`
Initial any `json:"initial"`
Subscribers int `json:"subscribers"`
UpdatedAt string `json:"updatedAt,omitempty"`
History []StoreHistorySnapshot `json:"history,omitempty"`
}
StoreSnapshot is a JSON-serializable snapshot of a tracked kites store and its bounded mutation history for DevTools inspection.