start method

Timer start({
  1. Duration interval = const Duration(seconds: 1),
  2. void onSync(
    1. ReplicationSync sync
    )?,
  3. void onError(
    1. Object error,
    2. StackTrace stack
    )?,
})

Syncs every interval until stop. Errors go to onError and the next tick tries again (frames not shipped stay in the log).

Implementation

Timer start({
  Duration interval = const Duration(seconds: 1),
  void Function(ReplicationSync sync)? onSync,
  void Function(Object error, StackTrace stack)? onError,
}) {
  var running = false;
  return Timer.periodic(interval, (_) async {
    if (running) {
      return;
    }
    running = true;
    try {
      final result = await sync();
      onSync?.call(result);
    } on Object catch (error, stack) {
      onError?.call(error, stack);
    } finally {
      running = false;
    }
  });
}