Skip to content

thi.ng/atom

@input-actions/thi-ng backs state with @thi.ng/atom’s Atom<T>. The unique value: when options.history is set, the atom is wrapped in History and the adapter exposes undo / redo / canUndo / canRedo / clearHistory on its public API. Every dispatched action becomes an undoable step — for free.

Terminal window
pnpm add @input-actions/thi-ng @thi.ng/atom
import { createThingStoreSync } from '@input-actions/thi-ng';
export const store = createThingStoreSync(
{ count: 0, _async: {} },
{ history: true }, // enable undo/redo; { history: { maxLen: 100 } } sets the stack depth
);

@thi.ng/atom is a static import, so creating the store is synchronous. It works at module scope and during SSR. createThingStore and ensureThingAtomLoaded still exist for code that already awaits them, and they resolve immediately.

import { StoreProvider } from '@input-actions/core';
import { store } from './store';
<StoreProvider store={store}>
<App />
</StoreProvider>
import { ThingAdapter } from '@input-actions/thi-ng';
import { store } from './store';
// Type the imported store as the concrete adapter to expose undo/redo without a cast.
const adapter = store as ThingAdapter<AppState>;
adapter.undo(); // → previous state, or undefined if at base
adapter.redo(); // → next state, or undefined if at tip
adapter.canUndo(); // boolean
adapter.canRedo(); // boolean
adapter.clearHistory(); // wipe the stack

Because the input-actions vocabulary is data — every state change is a tuple the executor interprets — wrapping the underlying atom in History captures every state change uniformly. Reset, save, toggle, push, merge, batch — all become reversible by virtue of going through one mutation path. No per-action ceremony.

Wire undo / redo to keyboard shortcuts:

import { useEffect } from 'react';
import type { ThingAdapter } from '@input-actions/thi-ng';
function UndoRedoKeys({ store }: { store: ThingAdapter<any> }) {
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
const mod = e.metaKey || e.ctrlKey;
if (mod && e.key === 'z' && !e.shiftKey) {
e.preventDefault();
store.undo();
} else if (mod && (e.key === 'y' || (e.shiftKey && e.key === 'Z'))) {
e.preventDefault();
store.redo();
}
};
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [store]);
return null;
}

The adapter accepts an existing Atom<T> via options.atom. If your app already uses @thi.ng/atom for other state (cursors, views, additional watches), pass the same atom and the action-driven state flows through it. Same store, two surfaces — actions for state changes, vanilla thi.ng primitives for derived reads.

import { Atom } from '@thi.ng/atom';
const sharedAtom = new Atom({ count: 0, _async: {} });
export const store = createThingStoreSync(
{ count: 0, _async: {} },
{ atom: sharedAtom, history: true },
);

The thi.ng example demonstrates undo/redo wired to UI buttons. The architecture point: an undo system that comes from the data shape of state changes, not from per-component history bookkeeping.