Key-value store with nested transactions and undo/redo
An admin console edits team members in a local key-value store before syncing. Some actions are a single write, like renaming a user. Others are multi-step forms (“change role, move team, remove the old invite”) that must undo as one step, and dialogs inside those forms can be cancelled on their own. Implement a class TransactionalStore with nested transactions and undo/redo.
const store = new TransactionalStore(initialRecords?);
store.get(key) // the value, or undefined
store.has(key) // => boolean
store.set(key, value) // => boolean: true if the store changed
store.delete(key) // => boolean: true if the key existed
store.snapshot() // => a plain object copy of all records
store.begin() // opens a (possibly nested) transaction
store.commit() // closes the innermost transaction, keeping its changes
store.rollback() // closes the innermost transaction, reverting its changes
store.undo() // => boolean
store.redo() // => boolean
store.canUndo() // => boolean
store.canRedo() // => boolean
'toString'). Values are stored by reference and compared with Object.is.initialRecords (a plain object, optional). This is the starting state: it is not an undo step.set on a key that already holds the same value, and delete on a missing key, change nothing: they return false and are not recorded anywhere. undefined is an ordinary value: set(key, undefined) on a missing key adds it, and has(key) becomes true.undo() puts every key of the latest step back to how it was before the step (including making keys missing again or bringing deleted keys back), moves the step to the redo history, and returns true. With nothing to undo, it returns false.redo() re-applies the latest undone step and returns true, or returns false if there is nothing to redo.get sees them right away.begin() may be called while a transaction is open, creating a nested one.rollback() reverts every change made since the matching begin(), including changes from nested transactions that were committed into it. The enclosing transaction (if any) stays open.commit() on a nested transaction hands its changes to the enclosing transaction; nothing is recorded yet.commit() on the outermost transaction records one undo step containing the net changes: each key whose final state (value, or missing) differs from its state before the outermost begin(). Keys that ended up back where they started are left out. If no key changed in total, nothing is recorded and the redo history is kept.commit() or rollback() with no open transaction throws an Error.undo() or redo() while a transaction is open throws an Error and changes nothing. canUndo() and canRedo() still report the recorded history.const team = new TransactionalStore({ ana: 'viewer' });
team.set('raj', 'editor'); // step 1
team.begin();
team.set('ana', 'admin');
team.delete('raj');
team.begin();
team.set('kim', 'viewer');
team.rollback(); // only kim is reverted
team.commit(); // step 2: ana and raj
team.snapshot(); // => { ana: 'admin' }
team.undo(); // => true
team.snapshot(); // => { ana: 'viewer', raj: 'editor' }
team.undo(); // => true
team.snapshot(); // => { ana: 'viewer' }
team.undo(); // => false
team.redo(); // => true, raj is back
team.begin();
team.set('ana', 'owner');
team.set('ana', 'viewer'); // back to where it started
team.commit(); // no net change: nothing recorded
team.canRedo(); // => true (step 2 can still be redone)