onRequest method

  1. @override
void onRequest(
  1. RequestOptions options,
  2. RequestInterceptorHandler handler
)
override

Intercepts outgoing requests and deduplicates them if applicable.

For the first request with a given signature, creates a Completer and proceeds. For subsequent identical requests, waits for the first request's Completer.

Implementation

@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
  if (!enabled || !_shouldDeduplicate(options)) {
    return handler.next(options);
  }

  final key = _generateRequestKey(options);

  if (_pendingRequests.containsKey(key)) {
    _pendingRequests[key]!.future.then(
      (response) => handler.resolve(_cloneResponse(response, options)),
      onError: (error) => handler.reject(error as DioException),
    );
    return;
  }

  _pendingRequests[key] = Completer<Response>();
  options.extra['_deduplicationKey'] = key;
  handler.next(options);
}