run method

Future<void> run()

Executes the request.

If the request is already running, it will not start a new one, but will return the future of the currently running request.

Implementation

Future<void> run() async {
  if (_running != null && !_running!.isCompleted) {
    // 既に実行中 (already running)
    return _running!.future;
  }
  _running = Completer<void>();
  _canceled = false;

  loading.value = true;
  error.value = null;

  int attempt = 0;
  while (true) {
    try {
      final result = await service();

      if (_canceled) break;

      data.value = result;
      options.onSuccess?.call(result);
      break;
    } catch (e) {
      if (_canceled) break;

      error.value = e;
      attempt++;
      if (attempt > options.retryCount) {
        options.onError?.call(e);
        break;
      }
      await Future.delayed(const Duration(milliseconds: 500));
    }
  }

  if (!_canceled) {
    loading.value = false;
    options.onFinally?.call();
  }
  _running?.complete();
}