Skip to content
yldm-techPublic

About

Concurrency-safe reactive state store for Go desktop apps: a versioned Store with selectors, undo/redo, composable persistence and async actions.

Topics

Resources

Contributing

Security policy

Stars

9 stars

Watchers

0 watching

Forks

Latest commit

 

History

147 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nodra

Website Go Reference Latest version Go version GitHub stars License X GitHub Sponsors

Nodra is a concurrency-safe reactive state store for Go desktop applications. It provides Zustand-like ergonomics for MyGo apps while keeping state independent from the renderer and persistence backend.

简体中文 · 日本語 · 한국어 · Español

Install

go get github.com/yldm-tech/nodra@latest

The core package is github.com/yldm-tech/nodra. The optional inspector is github.com/yldm-tech/nodra/devtools, the optional bridge to web pages is github.com/yldm-tech/nodra/bridge, and the Edit menu integration for native UI is github.com/yldm-tech/nodra/nativeui.

Quick start

package main

import (
	"fmt"

	"github.com/yldm-tech/nodra"
)

type State struct{ Count int }

func main() {
	store := nodra.New(State{})
	defer store.Close()

	stop := store.Subscribe(func(s nodra.Snapshot[State]) {
		fmt.Println(s.Value.Count)
	})
	defer stop()

	_ = store.Update(func(s *State) { s.Count++ })
}

Core capabilities

  • Versioned Store[T] with synchronous subscriptions, bounded watches and read-only views.
  • Derived state, selectors, projections, field bindings and atomic transactions.
  • Undo/redo history with grouping, filtering and payload budgets.
  • JSON, versioned migration, encrypted, backup, journaled and conflict-checked persistence.
  • Debounced, cancelable and queued async actions with progress, retries and metrics.
  • Scope for ownership and shutdown ordering.
  • Optional MyGo invalidation helpers and a local authenticated DevTools inspector.
  • A bridge that lets the web pages of a MyGo app follow a Store and change it through registered actions.
  • Undo and Redo of the window's keys and Edit menu acting on a History, for MyGo native UI.

MyGo

mygo.App.WhenReady(func() {
	window := mygo.NewWindow(mygo.WindowOptions{Title: "Counter", Content: ui.View(view)})
	// Redraws stop when the window closes. Keep the returned stop only to end them sooner: a defer here would end them as soon as WhenReady returns.
	nodra.InvalidateSelector(store, window, func(s State) int {
		return s.Count
	})
})

The integration targets MyGo v0.9.0. Keep temporary widget state in the view owner and commit only the edited field to the shared Store. See examples/mygo for a minimal native window.

history := nodra.Track(store, 100)
nativeui.InvalidateHistory(history, window) // redraw when Undo or Redo becomes possible

func (app *App) view(c *ui.Context) {
	nativeui.UndoShortcuts(c, app.history) // Cmd+Z, Shift+Cmd+Z and the Edit menu
	// ...
}

The optional nativeui subpackage makes the window's Undo and Redo act on a History: the keys, and with MyGo 0.9.0 on macOS the Edit menu items, which are enabled only while history can act. A focused text input keeps its own undo. It is separate from the core so apps without native UI do not link MyGo's ui package.

Services and CLIs that share Stores but never open a window can build with -tags nodra_nomygo. It leaves out the *mygo.Window helpers (InvalidateTarget stays) so MyGo is not linked, and a CGO_ENABLED=0 Linux build is then a static executable. The tag applies to the whole build: nativeui, or any library that calls those helpers, does not compile with it, and bridge links MyGo again. See docs/support.md.

Web pages

b := bridge.New(bridge.Options{})
stop, err := bridge.ExposeProjection(b, "counter", store, func(s State) PageState { return PageState{Count: s.Count} })
stopAdd, err := bridge.Handle(b, "counter/add", func(ctx context.Context, by int) error {
	return store.Update(func(s *State) { s.Count += by })
})
b.Bind() // before App.Run
import { dispatch, remoteState } from "./nodra.js";

const counter = remoteState("counter"); // subscribe and getSnapshot fit useSyncExternalStore
await dispatch("counter/add", 1);

The optional bridge subpackage makes a Store the one source of truth for native windows and web pages alike. Pages see only what the projection returns, receive a frame when it changes (coalesced into the latest only once about 1 MiB of frames waits for a slow page), and change state only through the actions Go registers. The JavaScript client in bridge/client has no dependencies. See docs/bridge.md and examples/web.

Persistence

persistence := nodra.NewPersistence(store, nodra.FileStorage{Path: "state.json", Backup: true}, nodra.JSONCodec[State]{})
// A missing file is a first start. Stop on any other load error: autosave would overwrite the file.
if err := persistence.Load(ctx); err != nil && !errors.Is(err, fs.ErrNotExist) {
	return err
}
writer, err := persistence.AutoSaveAsync(ctx, nodra.AsyncOptions{Debounce: 100 * time.Millisecond})
if err != nil {
	return err
}
defer func() {
	if err := writer.Close(shutdownCtx); err != nil {
		// The store still owns the data: retry with persistence.Save using a fresh context, or report the error.
		log.Printf("save state: %v", err)
	}
}()

Use VersionedJSONCodec, AESGCMCodec, Journal, Durable and WithConflictDetection when the application needs migrations, encryption or stronger file recovery guarantees. Details are in docs/architecture.md.

Complete example

examples/workbench is the full Nodra + MyGo desktop project. It combines cloned state, local drafts, validated backup persistence, debounced and cancelable async search, async saves, scoped shutdown, native invalidation, English/Simplified Chinese localization and optional DevTools.

go run ./examples/workbench
go run ./examples/workbench -devtools

The smaller examples cover one integration at a time:

DevTools

inspector := devtools.New(devtools.Options{})
defer inspector.Close()
stop, err := devtools.Attach(inspector, "state", store.ReadOnly())
if err != nil {
	panic(err)
}
defer stop()
url, _ := inspector.Listen()
fmt.Println(url)

DevTools is read-only by default. AllowRestore explicitly enables typed JSON editing and snapshot restoration. See docs/devtools.md for redaction, limits, remote read-only mode and the JSON compatibility contract.

Documentation

Verification

go test ./...
go test -race ./...
go vet ./...
golangci-lint run ./...
go run golang.org/x/vuln/cmd/govulncheck@latest ./...

Nodra is one Go module: github.com/yldm-tech/nodra. The project requires Go 1.27.3 or newer and uses MyGo v0.9.0 for native integration.

About

Concurrency-safe reactive state store for Go desktop apps: a versioned Store with selectors, undo/redo, composable persistence and async actions.

Topics

Resources

Contributing

Security policy

Stars

9 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages