Documentation
¶
Overview ¶
Package nodra is a state library for MyGo apps: concurrency-safe reactive stores whose snapshots drive native MyGo views. Invalidate redraws a window when its store publishes; the Store itself does not depend on a running MyGo app, so services and tests can use it as well.
Index ¶
- Variables
- func Invalidate[T any](store *Store[T], window *mygo.Window) (stop func())
- func SubscribeSelector[T any, S comparable](store *Store[T], selector func(T) S, fn func(S)) func()
- func SubscribeSelectorWith[T any, V comparable](store *Store[T], selector func(T) V, fn func(current, previous V), ...) func()
- type ActionEvent
- type ActionHooks
- type Codec
- type Definition
- type Derived
- type FileStorage
- type History
- type JSONCodec
- type Mutation
- type MutationType
- type Persistence
- type SelectorOptions
- type Snapshot
- type Storage
- type Store
- func (s *Store[T]) Action(name string, fn func(*T) error) func() error
- func (s *Store[T]) Batch(fn func()) error
- func (s *Store[T]) Close() error
- func (s *Store[T]) Do(name string, fn func(*T) error) error
- func (s *Store[T]) Get() T
- func (s *Store[T]) ID() string
- func (s *Store[T]) Patch(fn func(*T)) error
- func (s *Store[T]) Reset() error
- func (s *Store[T]) Set(value T) error
- func (s *Store[T]) Snapshot() Snapshot[T]
- func (s *Store[T]) State() T
- func (s *Store[T]) Subscribe(fn func(Snapshot[T])) func()
- func (s *Store[T]) SubscribeActionHooks(hooks ActionHooks) func()
- func (s *Store[T]) SubscribeActions(fn func(ActionEvent)) func()
- func (s *Store[T]) SubscribeMutations(fn func(Mutation[T])) func()
- func (s *Store[T]) SubscribeWith(options SubscribeOptions[T], fn func(Snapshot[T])) func()
- func (s *Store[T]) Transaction(fn func(*T) error) error
- func (s *Store[T]) Update(fn func(*T)) error
- func (s *Store[T]) UpdateErr(fn func(*T) error) error
- func (s *Store[T]) Version() uint64
- func (s *Store[T]) Watch(ctx context.Context, buffer int) <-chan Snapshot[T]
- type SubscribeOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoUndo = errors.New("nodra: nothing to undo") ErrNoRedo = errors.New("nodra: nothing to redo") )
ErrNoUndo and ErrNoRedo report an empty history stack.
var ErrClosed = errors.New("nodra: store is closed")
var ErrInvalidPersistence = errors.New("nodra: invalid persistence configuration")
Functions ¶
func Invalidate ¶ added in v0.2.0
Invalidate requests a new frame of window's native Content whenever store publishes a snapshot, so a MyGo view that reads the store during its render shows the new state. It returns the function that stops the subscription; with a nil store or window it subscribes to nothing and returns a no-op.
func SubscribeSelector ¶
func SubscribeSelector[T any, S comparable](store *Store[T], selector func(T) S, fn func(S)) func()
func SubscribeSelectorWith ¶
func SubscribeSelectorWith[T any, V comparable](store *Store[T], selector func(T) V, fn func(current, previous V), options SelectorOptions[V]) func()
SubscribeSelectorWith calls fn with current and previous selected values.
Types ¶
type ActionEvent ¶
type ActionEvent struct {
StoreID string
Name string
Started time.Time
Finished time.Time
Duration time.Duration
Err error
}
ActionEvent describes one completed action.
type ActionHooks ¶
type ActionHooks struct {
Before func(ActionEvent)
After func(ActionEvent)
Error func(ActionEvent)
}
ActionHooks observes action lifecycle events. Before runs before the mutation; After runs only on success; Error runs only on failure.
type Definition ¶
type Definition[T any] struct { // contains filtered or unexported fields }
Definition is a reusable named store factory, analogous to Pinia's defineStore and Zustand's reusable vanilla store creator.
func Define ¶
func Define[T any](id string, initial func() T) *Definition[T]
Define creates a store definition. Each New call gets independent state.
func (*Definition[T]) ID ¶
func (d *Definition[T]) ID() string
func (*Definition[T]) New ¶
func (d *Definition[T]) New() *Store[T]
type Derived ¶
Derived is a memoized getter derived from a store. It recomputes when the source publishes and only notifies listeners when the selected value changes.
type FileStorage ¶
FileStorage stores encoded state at Path. Save writes a temporary file in the same directory and renames it into place, so readers see a complete file.
type History ¶
type History[T any] struct { // contains filtered or unexported fields }
History records successful store snapshots and provides bounded undo/redo.
func Track ¶
Track starts recording changes to store. limit is the maximum number of undo entries; values below one disable history recording.
type JSONCodec ¶
type JSONCodec[T any] struct{}
JSONCodec encodes a store value using encoding/json.
type Mutation ¶
type Mutation[T any] struct { Type MutationType Name string Previous T Value T Version uint64 At time.Time }
Mutation contains Pinia-like subscription metadata.
type MutationType ¶
type MutationType string
MutationType describes why a store published a snapshot.
const ( MutationDirect MutationType = "direct" MutationPatch MutationType = "patch" MutationAction MutationType = "action" MutationReset MutationType = "reset" )
type Persistence ¶
type Persistence[T any] struct { // contains filtered or unexported fields }
Persistence connects a store to a Storage and Codec. It is explicit by default: call Load at startup and Save after successful application actions.
func NewPersistence ¶
func NewPersistence[T any](store *Store[T], storage Storage, codec Codec[T]) *Persistence[T]
func (*Persistence[T]) AutoSave ¶
func (p *Persistence[T]) AutoSave(ctx context.Context) func()
AutoSave subscribes to updates and saves synchronously after each update. The returned function stops autosaving. Use Batch to coalesce writes.
func (*Persistence[T]) AutoSaveWithErrors ¶
func (p *Persistence[T]) AutoSaveWithErrors(ctx context.Context) (<-chan error, func())
AutoSaveWithErrors subscribes to updates and reports persistence failures on a buffered channel. The channel closes when ctx is canceled or stop is called.
type SelectorOptions ¶
type SelectorOptions[V comparable] struct { Immediate bool Equals func(previous, next V) bool }
SelectorOptions configures a selector subscription.
type Storage ¶
type Storage interface {
Load(context.Context) ([]byte, error)
Save(context.Context, []byte) error
}
Storage is the minimal persistence backend used by Persistence. Files, key-value databases and encrypted stores can implement it.
type Store ¶
type Store[T any] struct { // contains filtered or unexported fields }
func (*Store[T]) Action ¶
Action returns a reusable named action function, matching Pinia action definitions while keeping Go's explicit function types.
func (*Store[T]) SubscribeActionHooks ¶
func (s *Store[T]) SubscribeActionHooks(hooks ActionHooks) func()
SubscribeActionHooks registers lifecycle hooks and returns an unsubscribe function.
func (*Store[T]) SubscribeActions ¶
func (s *Store[T]) SubscribeActions(fn func(ActionEvent)) func()
func (*Store[T]) SubscribeMutations ¶
func (*Store[T]) SubscribeWith ¶
func (s *Store[T]) SubscribeWith(options SubscribeOptions[T], fn func(Snapshot[T])) func()
func (*Store[T]) Transaction ¶
func (*Store[T]) Update ¶
Example ¶
package main
import (
"fmt"
"github.com/yldm-tech/nodra"
)
func main() {
store := nodra.New(0)
store.Subscribe(func(snapshot nodra.Snapshot[int]) {
fmt.Println(snapshot.Value, snapshot.Version)
})
if err := store.Update(func(value *int) { *value = 42 }); err != nil {
fmt.Println(err)
}
}
Output: 42 1