store

package
v0.46.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: MIT Imports: 3 Imported by: 0

README

Store Package

Reactive state management for Galaxy Go/WASM applications, inspired by Astro's nano stores.

Features

  • Type-safe reactive stores using Go generics
  • Three store types: Atom, Map, Computed
  • HMR integration - state persists across hot reloads
  • Subscription-based reactivity
  • Thread-safe with mutex protection
  • Zero dependencies - uses only stdlib and syscall/js

Quick Start

import "github.com/withgalaxy/galaxy/pkg/store"

count := store.NewAtom(0)
doubled := store.NewComputed(count, func(v int) int { return v * 2 })

count.Subscribe(func(val int) {
    fmt.Println("Count:", val)
})

count.Set(5) // Prints: Count: 5

Store Types

Atom[T]

Single value container with type safety.

stringStore := store.NewAtom("hello")
intStore := store.NewAtom(42)
structStore := store.NewAtom(User{Name: "Alice"})
MapStore

Key-value container for structured data.

user := store.NewMap(map[string]any{
    "name": "Alice",
    "age": 30,
})
user.SetKey("age", 31)
Computed[T]

Derived values from other stores.

doubled := store.NewComputed(count, func(v int) int {
    return v * 2
})

HMR Support

Enable HMR to preserve state across hot reloads:

count := store.NewAtomWithHMR(0, "my-counter")
user := store.NewMapWithHMR(initialData, "user-state")

API

Atom[T]
  • NewAtom[T](initial T) *Atom[T]
  • NewAtomWithHMR[T](initial T, key string) *Atom[T]
  • Get() T
  • Set(value T)
  • Update(fn func(T) T)
  • Subscribe(callback func(T)) Unsubscriber
  • Value() T - alias for Get()
MapStore
  • NewMap(initial map[string]any) *MapStore
  • NewMapWithHMR(initial map[string]any, key string) *MapStore
  • Get() map[string]any
  • GetKey(key string) any
  • Set(value map[string]any)
  • SetKey(key string, value any)
  • DeleteKey(key string)
  • Subscribe(callback func(map[string]any)) Unsubscriber
Computed[T]
  • NewComputed[S, T](source ReadableStore[S], fn func(S) T) *Computed[T]
  • Get() T
  • Subscribe(callback func(T)) Unsubscriber
  • Destroy() - cleanup source subscription

Examples

See galaxy/examples/basic/src/pages/:

  • store-counter.gxc - Basic usage
  • store-todos.gxc - Todo app with complex state

Documentation

Full documentation: galaxy-docs/src/content/docs/stores-guide.md

Testing

Tests are in store_test.go with WASM build tags. Cannot run directly without browser environment.

Future Extensions

Designed for multi-language support via ReadableStore interface abstraction.

Documentation

Rendered for js/wasm

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Atom

type Atom[T any] struct {
	// contains filtered or unexported fields
}

func NewAtom

func NewAtom[T any](initial T) *Atom[T]

func NewAtomWithHMR

func NewAtomWithHMR[T any](initial T, hmrKey string) *Atom[T]

func (*Atom[T]) Get

func (a *Atom[T]) Get() T

func (*Atom[T]) Set

func (a *Atom[T]) Set(value T)

func (*Atom[T]) Subscribe

func (a *Atom[T]) Subscribe(callback func(T)) Unsubscriber

func (*Atom[T]) Update

func (a *Atom[T]) Update(fn func(T) T)

func (*Atom[T]) Value

func (a *Atom[T]) Value() T

type Computed

type Computed[T any] struct {
	// contains filtered or unexported fields
}

func NewComputed

func NewComputed[S any, T any](source ReadableStore[S], transform func(S) T) *Computed[T]

func (*Computed[T]) Destroy

func (c *Computed[T]) Destroy()

func (*Computed[T]) Get

func (c *Computed[T]) Get() T

func (*Computed[T]) Subscribe

func (c *Computed[T]) Subscribe(callback func(T)) Unsubscriber

func (*Computed[T]) Value

func (c *Computed[T]) Value() T

type MapStore

type MapStore struct {
	// contains filtered or unexported fields
}

func NewMap

func NewMap(initial map[string]any) *MapStore

func NewMapWithHMR

func NewMapWithHMR(initial map[string]any, hmrKey string) *MapStore

func (*MapStore) DeleteKey

func (m *MapStore) DeleteKey(key string)

func (*MapStore) Get

func (m *MapStore) Get() map[string]any

func (*MapStore) GetKey

func (m *MapStore) GetKey(key string) any

func (*MapStore) Set

func (m *MapStore) Set(value map[string]any)

func (*MapStore) SetKey

func (m *MapStore) SetKey(key string, value any)

func (*MapStore) Subscribe

func (m *MapStore) Subscribe(callback func(map[string]any)) Unsubscriber

func (*MapStore) Value

func (m *MapStore) Value() map[string]any

type ReadableStore

type ReadableStore[T any] interface {
	Get() T
	Subscribe(callback func(T)) Unsubscriber
}

type ReadonlyStore

type ReadonlyStore[T any] interface {
	Get() T
	Subscribe(callback func(T)) Unsubscriber
	Value() T
}

type Store

type Store[T any] interface {
	Get() T
	Set(value T)
	Subscribe(callback func(T)) Unsubscriber
	Value() T
}

type Unsubscriber

type Unsubscriber func()

Jump to

Keyboard shortcuts

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