updateWithUndo method
Applies a reducer and records the previous state only for a real change. Equal states, throwing reducers, cancellation and disposal leave history untouched. When recording is disabled this behaves like update.
Implementation
void updateWithUndo(T Function(T state) reducer, {JIntent? origin}) {
if (!_canChangeHistory) return;
if (!_undoRedoEnabled || _isUndoRedoOperation) {
update(reducer, origin: origin);
return;
}
try {
update((previous) {
final next = reducer(previous);
if (mounted && previous != next) {
_undoStack.add(previous);
if (_undoStack.length > _maxHistorySize) _undoStack.removeAt(0);
_redoStack.clear();
}
return next;
}, origin: origin);
} finally {
_publishHistory();
}
}