computed<T> function
Data is often derived from other pieces of existing data. The computed function lets you combine the values of multiple signals into a new signal that can be reacted to, or even used by additional computeds. When the signals accessed from within a computed callback change, the computed callback is re-executed and its new return value becomes the computed signal's value.
import 'package:preact_signals/preact_signals.dart';
final name = signal("Jane");
final surname = signal("Doe");
final fullName = computed(() => name.value + " " + surname.value);
// Logs: "Jane Doe"
print(fullName.value);
// Updates flow through computed, but only if someone
// subscribes to it. More on that later.
name.value = "John";
// Logs: "John Doe"
print(fullName.value);
Any signal that is accessed inside the computed's callback function will be automatically subscribed to and tracked as a dependency of the computed signal.
Implementation
ReadonlySignal<T> computed<T>(
ComputedCallback<T> compute, {
String? debugLabel,
}) {
final instance = Computed<T>(compute, debugLabel: debugLabel);
_onComputedCreated(instance);
return instance;
}