Effect.via constructor

Effect.via(
  1. FutureOr<void> action(
    1. EffectVia via
    ), {
  2. bool guard(
    1. EffectVia via
    )?,
  3. void onUncaughtError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
})

Creates an explicit dynamic discovery Effect using EffectVia.

Unlike manifest effects which skip initial evaluation, Effect.via schedules an initial evaluation on the microtask scheduler upon activation to dynamically discover dependencies accessed through via(node).

  • Re-runs action whenever any dynamically observed node mutates.

  • Automatically prunes stale dependencies and registers new dependencies during re-evaluation (1-pass epoch diffing).

  • Supports both synchronous and asynchronous (async) action closures: when action returns a Future, dependencies accessed via via(node) after await points are tracked for the latest active turn while superseded turns are isolated automatically.

  • If guard is provided, it is evaluated with EffectVia before each run; returning false skips execution.

  • If onUncaughtError is provided, it is invoked when an uncaught synchronous or asynchronous exception occurs.

  • action: The dynamic tracking callback receiving EffectVia.

  • guard: Optional boolean predicate evaluated with EffectVia prior to each execution.

  • onUncaughtError: Optional error recovery handler.

Ensures:

Examples

final a = blooming(1);
final b = blooming(2);

final e = Effect.via((via) {
  print('Dynamic sum: ${via(a.bloom) + via(b.bloom)}');
});

Implementation

@pragma('vm:prefer-inline')
factory Effect.via(
  FutureOr<void> Function(EffectVia via) action, {
  bool Function(EffectVia via)? guard,
  void Function(Object error, StackTrace stackTrace)? onUncaughtError,
}) =>
    _ViaEffect(
      action,
      guard: guard,
      onUncaughtError: onUncaughtError,
    );