run method

Future<T> run()

Runs request and returns result.

Will be finished when:

Throws StateError if already running.

Implementation

Future<T> run() async {
  if (_running) throw StateError('Already running');
  _running = true;
  _canceled = false;

  var attempts = 0;
  T res;

  // ignore: literal_only_boolean_expressions
  while (true) {
    res = await request();
    if (!_canceled && attempts < maxAttempts && await retryIf(res)) {
      attempts++;

      final delay = _delay = CancelableOperation<void>.fromFuture(
          Future<void>.delayed(_getDelay(attempts)));
      await delay.valueOrCancellation();
      _delay = null;
      if (delay.isCanceled) break;
    } else {
      break;
    }
  }

  _running = false;
  return res;
}