load method

Future<V> load(
  1. K key
)

Loads a key, returning a Future for the value represented by that key.

Implementation

Future<V> load(K key) {
  final batch = _getCurrentBatch(this);
  final cacheMap = this._cacheMap;
  final cacheKey = this._cacheKeyFn(key);

  // If caching and there is a cache-hit, return cached Future.
  if (cacheMap != null) {
    final cachedFuture = cacheMap.get(cacheKey);
    if (cachedFuture != null) {
      final cacheHits =
          batch.cacheHits != null ? batch.cacheHits! : (batch.cacheHits = []);
      final comp = Completer<V>();
      cacheHits.add(() {
        comp.complete(cachedFuture);
      });
      return comp.future;
    }
  }

  // Otherwise, produce a new Future for this key, and enqueue it to be
  // dispatched along with the current batch.
  batch.keys.add(key);
  final comp = Completer<V>();
  batch.callbacks.add(comp);

  // If caching, cache this promise.
  if (cacheMap != null) {
    cacheMap.set(cacheKey, comp.future);
  }

  return comp.future;
}