LogoSignals.dart
Copy Markdown
rodydavis/signals.dart 999999

TrackedSignalMixin

A mixin that adds tracking for the initial and previous values to a Signal.

A mixin that adds tracking for the initial and previous values to a Signal.

TrackedSignalMixin stores the initialValue (the value the signal had when it was created or initialized) and the previousValue (the value of the signal right before the most recent update).

If you are looking for full undo/redo capabilities, use [ChangeStackSignalMixin](/packages/signals/mixins/change-stack) instead.

Example Usage#

import 'package:signals/signals.dart';

class MyTrackedSignal extends Signal<int> with TrackedSignalMixin<int> {
  MyTrackedSignal(super.internalValue);
}

void main() {
  final signal = MyTrackedSignal(0);

  print('Initial: ${signal.initialValue}');   // Prints: "Initial: 0"
  print('Previous: ${signal.previousValue}'); // Prints: "Previous: null"

  signal.value = 1;
  print('Initial: ${signal.initialValue}');   // Prints: "Initial: 0"
  print('Previous: ${signal.previousValue}'); // Prints: "Previous: 0"

  signal.value = 2;
  print('Initial: ${signal.initialValue}');   // Prints: "Initial: 0"
  print('Previous: ${signal.previousValue}'); // Prints: "Previous: 1"
}
This mixin only works with values that are immutable or are copied/cloned on mutation. If the value is mutated directly in-place without re-assigning, initialValue and previousValue will end up pointing to the same modified instance as the current value.

Methods#

View Methods
T initialValue

The initial value the signal was created with

T? previousValue

Get the previous value (if exists)

void afterCreate(T val)
void beforeUpdate(val)