run method

Future<(Object?, bool)> run(
  1. Future<void> f()
)

Runs the provided function with exponential backoff.

Returns a tuple containing the error (if any) and a boolean indicating whether the function was actually run.

Implementation

Future<(Object?, bool)> run(Future<void> Function() f) async {
  _init();

  if (_failures != 0) {
    if (_lastRun != null) {
      final sinceLastRun = DateTime.now().difference(_lastRun!);
      if (sinceLastRun < _calcDelay()) {
        return (null, false);
      }
    }
  }

  _lastRun = DateTime.now();
  try {
    await f();
    _failures = 0;
    return (null, true);
  } catch (e) {
    _failures++;
    return (e, true);
  }
}