addRequest method

Future<CFResult<String>> addRequest({
  1. required String url,
  2. required Map<String, dynamic> data,
  3. Map<String, String>? headers,
  4. int priority = 5,
})

Add request to pipeline

Implementation

Future<CFResult<String>> addRequest({
  required String url,
  required Map<String, dynamic> data,
  Map<String, String>? headers,
  int priority = 5,
}) async {
  final completer = Completer<CFResult<String>>();
  final request = PipelinedRequest(
    url: url,
    data: data,
    headers: headers ?? {},
    priority: priority,
    completer: completer,
    timestamp: DateTime.now(),
  );

  final host = Uri.parse(url).host;
  _pendingRequests.putIfAbsent(host, () => []);
  _pendingRequests[host]!.add(request);

  // Sort by priority (higher priority first)
  _pendingRequests[host]!.sort((a, b) => b.priority.compareTo(a.priority));

  _scheduleBatch(host);

  Logger.d(
      '$_source: Queued request for $host (${_pendingRequests[host]!.length} pending)');
  return completer.future;
}