deduplicateFetch<T> method

Future<T> deduplicateFetch<T>(
  1. String queryKey,
  2. Future<T> fetcher()
)

Deduplicate concurrent fetches for the same query

Implementation

Future<T> deduplicateFetch<T>(
  String queryKey,
  Future<T> Function() fetcher,
) async {
  // Check if fetch is already in progress
  final pending = _pendingFetches[queryKey];
  if (pending != null) {
    return pending as Future<T>;
  }

  // Start new fetch
  final future = fetcher();
  _pendingFetches[queryKey] = future;

  try {
    final result = await future;
    return result;
  } finally {
    _pendingFetches.remove(queryKey);
  }
}