ChangeStackSignalMixin#
mixin |
Package: package:signals_core
Mixin: ChangeStackSignalMixin#
A mixin that adds undo, redo, and state history replay capabilities to a Signal.
ChangeStackSignalMixin keeps track of past states of a signal's value in a double-ended queue, allowing you to easily go back to previous values using undo and go forward to subsequent values using redo. You can inspect if undo or redo are available via canUndo and canRedo.
You can also set a limit on the maximum size of the history stack, preventing memory leak issues in long-running scenarios.
Example Usage#
import 'package:signals/signals.dart';
class HistorySignal extends Signal<int> with ChangeStackSignalMixin<int> {
HistorySignal(super.internalValue);
}
void main() {
final counter = HistorySignal(0);
counter.limit = 5; // Cap history stack to 5 items
counter.value = 1;
counter.value = 2;
counter.value = 3;
print('Current: ${counter.value}'); // Prints: "Current: 3"
print('Can Undo: ${counter.canUndo}'); // Prints: "Can Undo: true"
// Undo last change
counter.undo();
print('Undone: ${counter.value}'); // Prints: "Undone: 2"
// Undo once more
counter.undo();
print('Undone: ${counter.value}'); // Prints: "Undone: 1"
// Redo the previous undo
counter.redo();
print('Redone: ${counter.value}'); // Prints: "Redone: 2"
}
set or the .value
setter, the history queue will store references to the same mutated object, and undo/redo
will appear to do nothing.
Members of ChangeStackSignalMixin#
| Member | Type | Signature | Description |
|---|---|---|---|
| limit | field |
dart int? limit |
Max values to keep in history |
| history | method |
dart Iterable<SignalChange |
List of changes in the history |
| redos | method |
dart Iterable<SignalChange |
List of changes in the redo stack |
| canRedo | method |
dart bool canRedo |
Can redo the previous change |
| canUndo | method |
dart bool canUndo |
Can undo the previous change |
| set | method |
dart bool set(T val, {bool force = false}) |
|
| redo | method |
dart void redo() |
Redo Previous Undo |
| undo | method |
dart void undo() |
Undo Last Change |
| clear | method |
dart void clear() |
Clear the history for redo and undo |
| clearUndo | method |
dart void clearUndo() |
Clear undo stack |
| clearRedo | method |
dart void clearRedo() |
Clear redo stack |
References#
The ChangeStackSignalMixin type is referenced and used in the following pages:
- TrackedSignalMixin (signals_flutter/mixins)
- ChangeStackSignalMixin (signals_flutter/mixins)
- signals_flutter
- TrackedSignalMixin (signals_core/mixins)
- ChangeStackSignalMixin (signals_core/mixins)
- signals_core
- TrackedSignalMixin (signals/mixins)
- ChangeStackSignalMixin (signals/mixins)
- signals