LogoSignals.dart
Copy Markdown
rodydavis/signals.dart 999999

Type: ChangeStackSignalMixin

API reference and details for ChangeStackSignalMixin from signals.dart.

ChangeStackSignalMixin#

Kind: 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.

If you only need access to the initial and immediate previous values of a signal (without a full history stack or undo/redo mechanisms), use the lightweight TrackedSignalMixin instead.

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"
}
This mixin only works with values that are immutable or are copied/cloned when changed. If you mutate an object in-place directly without replacing the value using 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> history List of changes in the history
redos method dart Iterable<SignalChange> redos 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: