crossmintPoll<T extends Object> function

Future<T> crossmintPoll<T extends Object>({
  1. required Future<T?> action(
    1. int attempt
    ),
  2. required Object onTimeout(),
  3. CrossmintPollingConfig config = const CrossmintPollingConfig(),
})

Polls action up to config.maxAttempts times with exponential backoff.

action receives the current zero-based attempt index and should return null to indicate "keep polling" or a non-null value to stop.

Throws the result of onTimeout if all attempts are exhausted.

Implementation

Future<T> crossmintPoll<T extends Object>({
  required Future<T?> Function(int attempt) action,
  required Object Function() onTimeout,
  CrossmintPollingConfig config = const CrossmintPollingConfig(),
}) async {
  final Random rng = Random();
  for (int attempt = 0; attempt < config.maxAttempts; attempt += 1) {
    final T? result = await action(attempt);
    if (result != null) {
      return result;
    }
    await Future<void>.delayed(config.delayForAttempt(attempt, rng));
  }
  throw onTimeout();
}