computedFrom<T, A> function

FutureSignal<T> computedFrom<T, A>(
  1. List<ReadonlySignal<A>> signals,
  2. Future<T> fn(
    1. List<A> args
    ), {
  3. T? initialValue,
  4. String? debugLabel,
  5. bool autoDispose = false,
  6. bool lazy = true,
})

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,
  );
}