computedAsync<T> function

FutureSignal<T> computedAsync<T>(
  1. Future<T> callback(), {
  2. T? initialValue,
  3. String? debugLabel,
  4. bool autoDispose = false,
  5. List<ReadonlySignal> dependencies = const [],
  6. bool lazy = true,
})

Async Computed is syntax sugar around FutureSignal.

Inspired by computedAsync from Angular NgExtension.

computedAsync takes a callback function to compute the value of the signal. This callback is converted into a Computed signal.

final movieId = signal('id');
late final movie = computedAsync(() => fetchMovie(movieId()));

It is important that signals are called before any async gaps with await.

Any signal that is read inside the callback will be tracked as a dependency and the computed signal will be re-evaluated when any of the dependencies change.

Implementation

FutureSignal<T> computedAsync<T>(
  Future<T> Function() callback, {
  T? initialValue,
  String? debugLabel,
  bool autoDispose = false,
  List<ReadonlySignal<dynamic>> dependencies = const [],
  bool lazy = true,
}) {
  return FutureSignal<T>(
    callback,
    dependencies: dependencies,
    initialValue: initialValue,
    debugLabel: debugLabel,
    autoDispose: autoDispose,
    lazy: lazy,
  );
}