retryOperation method

Future<void> retryOperation(
  1. FutureOr<DataType?> operation(), {
  2. required RetryConfig config,
  3. required void onSuccessful(
    1. DataType?
    ),
  4. required void onFailed(
    1. ErrorType?
    ),
})
inherited

Implementation

Future<void> retryOperation(
  FutureOr<T?> Function() operation, {
  required RetryConfig config,
  required void Function(T?) onSuccessful,
  required void Function(E?) onFailed,
}) async {
  for (int attempts = 0; attempts < config.maxRetries; attempts++) {
    final completer = Completer<T?>();
    await Future.delayed(
      attempts == 0 ? Duration.zero : config.retryDelay,
      operation,
    ).then(completer.complete).catchError(completer.completeError);
    try {
      final result = await completer.future;
      onSuccessful(result);
      break;
    } catch (e, stack) {
      if (e is E?) {
        if (attempts == config.maxRetries - 1) {
          onFailed(e as E?);
        }
      } else {
        FlutterError.reportError(
          FlutterErrorDetails(
            exception: e,
            library: 'fl_query',
            context: ErrorDescription('retryOperation'),
            stack: stack,
          ),
        );
      }
      if (!await QueryClient.connectivity.isConnected) {
        break;
      }
    }
  }
}