history

The history namespace exposes the engine's undo/redo stack and the two patterns for grouping multiple operations into a single reversible step.

Every mutating call across the API — api.nodes.add, api.edges.update, api.style.setNodeColor, api.layout.setPositions, and so on — is wrapped in a Command and routed through the shared CommandExecutor. Reads (api.nodes.get, api.selection.getSelectedNodeIds, etc.) and camera ops never touch the stack.

The four core operations

await api.history.undo();   // pop one step off the undo stack, push to redo
await api.history.redo();   // reverse
await api.history.clear();  // discard both stacks (e.g. after loading a new dataset)
await api.history.flush();  // commit any pending coalesced commands immediately

canUndo() and canRedo() are cheap to call on every render — wire them straight to button disabled props. getUndoStack() and getRedoStack() return arrays of human-readable labels, useful for a history dropdown.

Grouping commands into one undo step

A typical "import 200 nodes + 500 edges + apply layout" sequence should undo as one step, not 701. Two mechanisms handle this:

Implicit coalescing

Commands dispatched within the same synchronous tick (or before the next microtask commit) are automatically merged. The simplest case "just works":

// All three become one undo step because they run synchronously.
api.style.setNodeColor("s1", [1, 0, 0]);
api.style.setNodeColor("s2", [0, 1, 0]);
api.style.setNodeColor("s3", [0, 0, 1]);

If you await between mutations, the coalescing window closes and each becomes its own step.

Explicit transactions

For async workflows or when you want a guaranteed group regardless of timing, wrap in transaction(label, fn):

await api.history.transaction("Import dataset", async () => {
  await api.nodes.add(nodes);          // async I/O → would normally split…
  await api.edges.add(edges);          // …but transaction keeps them as one step
  await api.layout.circle();
});
// Undo stack now contains a single "Import dataset" entry.

Transactions nest: only the outermost endTransaction actually commits, so calling transaction(...) from inside another transaction is safe.

The lower-level beginTransaction(label) / endTransaction() pair is useful when the begin and end live in different callbacks (e.g. a drag start / drag end pair). Prefer transaction(label, fn) whenever both ends can be in the same scope — it auto-rolls-back on thrown exceptions.

Manual command execution

execute(command) lets host code dispatch custom Command instances directly. The command must implement the Command interface (a do() / undo() pair plus a label). This is how the API's own methods are implemented and is the escape hatch for genuinely engine-foreign operations that still need to participate in undo.

Events

CommandExecutor emits "history:changed" after every commit. Subscribe via the engine event bus to refresh history UI:

api.events.on("history:changed", ({ canUndo, canRedo }) => {
  undoBtn.disabled = !canUndo;
  redoBtn.disabled = !canRedo;
});

Methods