Undo / Redo History
Medium · Classes
Implement the generic class History<T> that stores a current value with undo and redo.
- value (a getter) returns the current value.
- set(v): if v is equal (===) to the current value, do nothing. Otherwise save the current value on the undo stack, make v current and clear the redo stack. The undo stack keeps at most limit entries: when it grows larger, drop the OLDEST entry.
- undo(): if possible, move the current value to the redo stack and restore the last saved value. Otherwise do nothing.
- redo(): the reverse of undo(). Do nothing if there is nothing to redo.
The driver runHistory(initial, limit, ops) is already written and returns the current value after every operation.