Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions packages/replay_bloc/lib/src/change_stack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,55 @@ class _ChangeStack<T> {
_redos.clear();
}

void redo() {
if (canRedo) {
final change = _redos.removeFirst();
_history.addLast(change);
return _shouldReplay(change._newValue) ? change.execute() : redo();
void redo(int steps) {
if (steps <= 0) return;

var effectiveSteps = steps;
while (effectiveSteps > 0 && canRedo) {
_Change<T>? changeToExecute;
while (_redos.isNotEmpty) {
final change = _redos.first;
if (_shouldReplay(change._newValue)) {
changeToExecute = _redos.removeFirst();
break;
} else {
_history.addLast(_redos.removeFirst());
}
}

if (changeToExecute != null) {
_history.addLast(changeToExecute);
changeToExecute.execute();
effectiveSteps--;
} else {
break;
}
}
}

void undo() {
if (canUndo) {
final change = _history.removeLast();
_redos.addFirst(change);
return _shouldReplay(change._oldValue) ? change.undo() : undo();
void undo(int steps) {
if (steps <= 0) return;

var effectiveSteps = steps;
while (effectiveSteps > 0 && canUndo) {
_Change<T>? changeToUndo;
while (_history.isNotEmpty) {
final change = _history.last;
if (_shouldReplay(change._oldValue)) {
changeToUndo = _history.removeLast();
break;
} else {
_redos.addFirst(_history.removeLast());
}
}

if (changeToUndo != null) {
_redos.addFirst(changeToUndo);
changeToUndo.undo();
effectiveSteps--;
} else {
break;
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/replay_bloc/lib/src/replay_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ mixin ReplayBlocMixin<Event extends ReplayEvent, State> on Bloc<Event, State> {
}

/// Undo the last change.
void undo() => _changeStack.undo();
void undo([int steps = 1]) => _changeStack.undo(steps);

/// Redo the previous change.
void redo() => _changeStack.redo();
void redo([int steps = 1]) => _changeStack.redo(steps);

/// Checks whether the undo/redo stack is empty.
bool get canUndo => _changeStack.canUndo;
Expand Down
4 changes: 2 additions & 2 deletions packages/replay_bloc/lib/src/replay_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ mixin ReplayCubitMixin<State> on Cubit<State> {
}

/// Undo the last change.
void undo() => _changeStack.undo();
void undo([int steps = 1]) => _changeStack.undo(steps);

/// Redo the previous change.
void redo() => _changeStack.redo();
void redo([int steps = 1]) => _changeStack.redo(steps);

/// Checks whether the undo/redo stack is empty.
bool get canUndo => _changeStack.canUndo;
Expand Down
Loading