WritableComputedImpl<T>.withPrevious constructor

WritableComputedImpl<T>.withPrevious(
  1. T getter(
    1. T?
    ),
  2. void setter(
    1. T
    ), {
  3. JoltDebugFn? onDebug,
})

Creates a writable 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
  • setter: Function called when the computed value is set
  • onDebug: Optional debug callback for reactive system debugging

Example:

final signal = Signal([5]);
final computed = WritableComputed<int>.withPrevious(
  (prev) {
    final newValue = signal.value[0] * 2;
    if (prev != null && prev == newValue) {
      return prev;
    }
    return newValue;
  },
  (value) => signal.value = [value ~/ 2],
);

Implementation

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

  computed = WritableComputedImpl(fn, setter, onDebug: onDebug);
  return computed;
}