computedFrom<T, A> function
Async Computed is syntax sugar around FutureSignal.
Inspired by computedFrom from Angular NgExtension.
computedFrom takes a list of signals
and a callback
function to
compute the value of the signal every time one of the signals
changes.
final movieId = signal('id');
late final movie = computedFrom(args, ([movieId]) => fetchMovie(args.first));
Since all dependencies are passed in as arguments there is no need to worry about calling the signals before any async gaps with await.
Implementation
FutureSignal<T> computedFrom<T, A>(
List<ReadonlySignal<A>> signals,
Future<T> Function(List<A> args) fn, {
T? initialValue,
String? debugLabel,
bool autoDispose = false,
bool lazy = true,
}) {
return FutureSignal<T>(
() => fn(signals.map((e) => e()).toList()),
dependencies: signals,
initialValue: initialValue,
debugLabel: debugLabel,
autoDispose: autoDispose,
lazy: lazy,
);
}