ComputedImpl<T>.withPrevious constructor

ComputedImpl<T>.withPrevious(
  1. T getter(
    1. T?
    ), {
  2. JoltDebugFn? onDebug,
})

Creates a computed value with a getter that receives the previous value.

Parameters:

  • getter: Function that computes the value, receiving the previous value (or null on first computation) as a parameter
  • onDebug: Optional debug callback for reactive system debugging

Example:

final signal = Signal<List<int>>([1, 2, 3]);
final computed = Computed<List<int>>.withPrevious((prev) {
  final newList = List<int>.from(signal.value);
  if (prev != null &&
      prev.length == newList.length &&
      prev.every((item) => newList.contains(item))) {
    return prev; // Return previous to maintain stability
  }
  return newList;
});

Implementation

factory ComputedImpl.withPrevious(
  T Function(T?) getter, {
  JoltDebugFn? onDebug,
}) {
  late final ComputedImpl<T> computed;
  T fn() => getter(computed.pendingValue);

  computed = ComputedImpl(fn, onDebug: onDebug);
  return computed;
}