throttle method

Effect throttle(
  1. Duration duration,
  2. void action(), {
  3. bool guard()?,
  4. bool leading = true,
  5. bool trailing = true,
  6. int? take,
  7. void onUncaughtError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
})

Defines an explicit rate-limited throttle Effect attached to the scoped targets.

Guarantees that action is executed at most once per duration window across all targets.

  • duration: The minimum time interval between consecutive executions.
  • action: The untracked callback to execute when triggered.
  • guard: Optional boolean predicate gating execution.
  • leading: Whether to execute on the leading edge of the time window. Defaults to true.
  • trailing: Whether to execute on the trailing edge if mutations arrived during window. Defaults to true.
  • take: Optional maximum number of times this effect may execute.
  • onUncaughtError: Optional error recovery handler.

Requires:

  • At least one of leading or trailing must be true.
  • If take is provided, it must be greater than or equal to 0.

Ensures:

  • Returns a throttle Effect attached to the scoped targets.

Examples

final effect = (scrollX.bloom, scrollY.bloom).effect.throttle(
  const Duration(milliseconds: 100),
  () => syncViewport(untracked(scrollX.bloom), untracked(scrollY.bloom)),
);

Implementation

@pragma('vm:prefer-inline')
Effect throttle(
  Duration duration,
  void Function() action, {
  bool Function()? guard,
  bool leading = true,
  bool trailing = true,
  int? take,
  void Function(Object error, StackTrace stackTrace)? onUncaughtError,
}) =>
    Effect.throttle(
      _targets,
      duration,
      action,
      guard: guard,
      leading: leading,
      trailing: trailing,
      take: take,
      onUncaughtError: onUncaughtError,
    );