derived<T> static method

DerivedBeacon<T> derived<T>(
  1. T compute(), {
  2. bool manualStart = false,
})

Creates a DerivedBeacon whose value is derived from a computation function. This beacon will recompute its value everytime one of it's dependencies change.

If manualStart is true, the future will not execute until start() is called.

Example:

final age = Beacon.writable<int>(18);
final canDrink = Beacon.derived(() => age.value >= 21);

print(canDrink.value); // Outputs: false

age.value = 22;

print(canDrink.value); // Outputs: true

Implementation

static DerivedBeacon<T> derived<T>(
  T Function() compute, {
  bool manualStart = false,
}) {
  final beacon = DerivedBeacon<T>(manualStart: manualStart);

  final unsub = effect(() {
    // beacon is manually triggered if in idle state
    if (beacon.status.value == DerivedStatus.idle) return;

    beacon.forceSetValue(compute());
  });

  beacon.$setInternalEffectUnsubscriber(unsub);

  return beacon;
}