Undo/redo history with draft changes and explicit checkpoints
In a design tool, dragging a shape fires dozens of position updates per second. If every update became its own undo step, pressing undo would nudge the shape back one pixel at a time. Editors avoid this by treating rapid changes as a draft, and only recording an undo step (a checkpoint) when the gesture ends.
Implement a class DraftHistory that tracks one value with draft changes, checkpoints, undo, and redo.
const history = new DraftHistory(initialValue, { limit }?);
history.getValue() // the current value (including any draft change)
history.set(value) // change the current value without creating a checkpoint
history.isDirty() // true if the current value differs from the latest checkpoint
history.commit() // => boolean: save the current value as a checkpoint
history.discard() // => boolean: throw away the draft, back to the latest checkpoint
history.undo() // => boolean: go back one checkpoint
history.redo() // => boolean: go forward one checkpoint
history.canUndo() // => boolean
history.canRedo() // => boolean
history.subscribe(fn) // => unsubscribe function; fn(value) runs when the current value changes
Object.is and stored by reference (never cloned).set(value): if value is the same as the current value, do nothing. Otherwise make it the current value and clear the redo history.commit(): if dirty, save the current value as a new checkpoint and return true. The previous checkpoint becomes one undo step. If not dirty, return false.discard(): if dirty, reset the current value to the latest checkpoint and return true. Otherwise return false. Undo steps are unchanged.undo(): if dirty, first commit the draft (so redo() can bring it back). Then, if there is an earlier checkpoint, move to it and return true. Otherwise return false.redo(): if a later checkpoint exists (after an undo, with no changing set since), move to it and return true. Otherwise return false.canUndo() is true when dirty or when an earlier checkpoint exists. canRedo() is true when a later checkpoint exists.limit (default Infinity, otherwise a positive integer) is the maximum number of undo steps kept. When a new step would exceed it, drop the oldest one.subscribe(fn): call fn(newValue) every time getValue() changes because of set, discard, undo, or redo. Do not call it when nothing changes (commit, a no-op set, a failed undo). Listeners run in subscription order. The returned function unsubscribes and is safe to call more than once.const position = new DraftHistory({ x: 0, y: 0 });
// Drag: many updates, one checkpoint at the end.
position.set({ x: 4, y: 1 });
position.set({ x: 9, y: 3 });
position.set({ x: 12, y: 5 });
position.commit(); // => true
// Start a second drag but don't release yet.
position.set({ x: 20, y: 5 });
position.isDirty(); // => true
position.undo(); // => true (commits { x: 20 } first, then steps back)
position.getValue(); // => { x: 12, y: 5 }
position.undo(); // => true
position.getValue(); // => { x: 0, y: 0 }
position.undo(); // => false (nothing earlier)
position.redo(); // => true, back to { x: 12, y: 5 }
position.set({ x: 1, y: 1 }); // a new change clears redo
position.canRedo(); // => false
position.discard(); // => true, back to { x: 12, y: 5 }
const title = new DraftHistory('Untitled', { limit: 2 });
['A', 'B', 'C'].forEach((name) => {
title.set(name);
title.commit();
});
title.undo(); // => true, 'B'
title.undo(); // => true, 'A'
title.undo(); // => false ('Untitled' was dropped by the limit)