ComputedImpl<T>.withPrevious constructor
ComputedImpl<T>.withPrevious (
- T getter(
- T?
- 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 (ornullon first computation) as a parameteronDebug: 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;
}