Undo Stack Restores Snapshot History
Push a fresh snapshot before every mutation: undo pops to the previous state, redo replays the future, and any new edit clears the redo stack.
Code
interface DocState {
title: string;
lines: string[];
}
// Undo/redo: push a snapshot before each mutation; undo pops back,
// redo keeps the future until a new edit discards it
const doc: DocState = { title: 'notes', lines: ['first', 'second'] };
const undoStack: DocState[] = [];
const redoStack: DocState[] = [];
function snapshot(state: DocState): DocState {
// copy the container; string elements need nothing deeper
return { title: state.title, lines: [...state.lines] };
}
function edit(mutate: (draft: DocState) => void): void {
undoStack.push(snapshot(doc));
redoStack.length = 0; // a new edit invalidates the redo future
mutate(doc);
}
// restore the popped snapshot into the live document
function undo(): boolean {
const previous = undoStack.pop();
if (!previous) return false;
redoStack.push(snapshot(doc));
Object.assign(doc, previous);
return true;
}
function redo(): boolean {
const next = redoStack.pop();
if (!next) return false;
undoStack.push(snapshot(doc));
Object.assign(doc, next);
return true;
}
edit((draft) => draft.lines.push('third'));
edit((draft) => (draft.title = 'notes v2'));
console.log('after edits:', doc.title, '-', doc.lines.join(', '));
console.log('undo:', undo(), '->', doc.title, '-', doc.lines.join(', '));
console.log('undo:', undo(), '->', doc.title, '-', doc.lines.join(', '));
console.log('undo exhausted:', undo());
console.log('redo:', redo(), '->', doc.title, '-', doc.lines.join(', '));Output
Captured from a sandboxed run of this snippet in the build pipeline.
after edits: notes v2 - first, second, third
undo: true -> notes - first, second, third
undo: true -> notes - first, second
undo exhausted: false
redo: true -> notes - first, second, thirdHow it works
The state machine lives in snapshot, edit, undo and redo — everything else is wiring.
The result comes back as an object carrying title.
Caveats
- The guard
!previousshort-circuits the flow with an earlyreturnwhen its condition holds.
Applying it
The entry-point signature is function snapshot(state: DocState)
Output lines are prefixed (after edits:, undo:, undo exhausted:), making each step’s effect easy to spot.
The call returns title.
Statements that matter
function edit(mutate: (draft: DocState) => void): void {
undoStack.push(snapshot(doc));
redoStack.length = 0; // a new edit invalidates the redo future
mutate(doc);
}
return { title: state.title, lines: [...state.lines] };
edit((draft) => (draft.title = 'notes v2'));
Interactive Tools for This Topic
FAQ
- Does `snapshot` modify its input?
- The input survives unchanged; what you get back is a newly constructed value.
- Does the code pull in any external packages?
- No external packages are involved; every call resolves to a built-in API. The file defines `snapshot`, `edit`, `undo` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.