@vielzeug/ledger
v1.1.3
Published
Async undo/redo command history with Ripple signals for reactive canUndo/canRedo state
Readme
@vielzeug/ledger
Async undo/redo command history with Ripple signals for reactive canUndo/canRedo state.
Features
- Async commands —
executeandrollbackcan returnPromise<void>; operations are serialised - Reactive state —
canUndo,canRedo,historySize,isProcessing,historySnapshotare RippleComputedvalues - Batch — group multiple commands into one undo step
- Max history — configurable cap; oldest entries evicted automatically
- Disposable —
dispose()+[Symbol.dispose]forusingdeclarations
Install
pnpm add @vielzeug/ledgerQuick start
import { compose, createLedger } from '@vielzeug/ledger';
import { effect } from '@vielzeug/ripple';
const ledger = createLedger({ maxHistory: 50 });
// Execute a command
await ledger.do({
execute: async () => { item.name = newName; },
rollback: async () => { item.name = oldName; },
label: 'Rename item',
});
// Undo / redo
await ledger.undo();
await ledger.redo();
// Batch — compose multiple commands into one undo step
await ledger.do(compose([cmd1, cmd2, cmd3], 'Multi-edit'));
// Bind to UI
effect(() => {
undoButton.disabled = !ledger.canUndo.value;
redoButton.disabled = !ledger.canRedo.value;
});
ledger.dispose(); // or: using ledger = createLedger()