execute<T> method

Future<T> execute<T>({
  1. required String url,
  2. required Future<T> fn(),
})

Executes a function with rate limiting

url is the URL to rate limit fn is the function to execute

Implementation

Future<T> execute<T>({
  required String url,
  required Future<T> Function() fn,
}) async {
  final domain = _extractDomain(url);
  // We'll use the domain to look up the delay when processing the queue

  // Create a completer for this request
  final completer = Completer<T>();

  // Create a queued request
  final queuedRequest = _QueuedRequest<T>(fn: fn, completer: completer);

  // Add the request to the queue
  _requestQueues.putIfAbsent(domain, () => Queue<_QueuedRequest>());
  _requestQueues[domain]!.add(queuedRequest);

  // Process the queue if this is the only request
  if (_requestQueues[domain]!.length == 1) {
    _processQueue(domain);
  }

  // Return the future from the completer
  return completer.future;
}