computed<R, D> function

Accessor<R> computed<R, D>(
  1. R computation(), {
  2. bool notEqual = true,
})

Efficiently create a derived value from a computation.

computed provides a solution to efficiently use a value derived from signals. It returns an Accessor to the derived value which allows for reactive read-only access to the derived value.

computed presents similar strategies and API to computedStream with the difference primarily that computed provides the user with an Accessor instead of a ValueStream.

Implementation

Accessor<R> computed<R, D>(R Function() computation, {bool notEqual = true}) {
  final stream = computedStream(computation, notEqual: notEqual);
  final (result, setResult) = signal(stream.value);

  stream.listen((value) => setResult(value));

  return result;
}