onTick method

SelectBuilder<R> onTick(
  1. Duration d,
  2. FutureOr<R> body(), {
  3. Object? tag,
  4. bool if_()?,
})

Wait for a ticker to fire.

Tickers provide recurring timer events. Use this for periodic operations, heartbeats, or any regular interval-based processing.

Parameters:

  • d: Duration period
  • body: Function to call when the ticker fires
  • tag: Optional tag for debugging
  • if_: Optional guard condition

Example:


while (server.isRunning) {
  final action = await XSelect.run<ServerAction>((s) => s
    ..onRecvValue(requests, (req) => ServerAction.handleRequest(req))
    ..onTick(Duration(seconds: 5), () => ServerAction.heartbeat())
    ..onTick(Duration(minutes: 1), () => ServerAction.healthCheck())
    ..timeout(Duration(minutes: 30), () => ServerAction.idleShutdown())
  );

  await server.processAction(action);
}

See also:

  • onDelay - For fixed delayed branch

Implementation

SelectBuilder<R> onTick(
  Duration d,
  FutureOr<R> Function() body, {
  Object? tag,
  bool Function()? if_,
}) {
  _branches.add((TimerBranch<R>.period(d, body, tag: tag), if_));
  return this;
}