createComputedAsync<S> method

FutureSignal<S> createComputedAsync<S>(
  1. Future<S> fn(), {
  2. S? initialValue,
  3. String? debugLabel,
  4. List<ReadonlySignal> dependencies = const [],
  5. 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<S> createComputedAsync<S>(
  Future<S> Function() fn, {
  S? initialValue,
  String? debugLabel,
  List<ReadonlySignal<dynamic>> dependencies = const [],
  bool lazy = true,
}) {
  return _bindLocal(
    computedAsync<S>(
      fn,
      dependencies: dependencies,
      initialValue: initialValue,
      debugLabel: debugLabel,
      lazy: lazy,
    ),
  );
}