dispatchBatch<K extends Object, V> function
void
dispatchBatch<
K extends Object, V>( - DataLoader<K, V, Object?> loader,
- _Batch<K, V> batch
)
Implementation
void dispatchBatch<K extends Object, V>(
DataLoader<K, V, Object?> loader,
_Batch<K, V> batch,
) {
// Mark this batch as having been dispatched.
batch.hasDispatched = true;
// If there's nothing to load, resolve any cache hits and return early.
if (batch.keys.isEmpty) {
_resolveCacheHits(batch);
return;
}
// Call the provided batchLoadFn for this loader with the batch's keys and
// with the loader as the `this` context.
final batchFuture = loader._batchLoadFn(batch.keys);
// Await the resolution of the call to batchLoadFn.
batchFuture.then((values) {
if (values.length != batch.keys.length) {
throw StateError(
'DataLoader must be constructed with a function which accepts '
'List<key> and returns Future<List<value>>, but the function did '
'not return a Future of an List of the same length as the List '
'of keys.'
'\n\nKeys:\n${batch.keys}'
'\n\nValues:\n$values');
}
// Resolve all cache hits in the same micro-task as freshly loaded values.
_resolveCacheHits(batch);
// Step through values, resolving or rejecting each Future in the batch.
for (var i = 0; i < batch.callbacks.length; i++) {
final value = values[i];
batch.callbacks[i].complete(value);
}
}).catchError((Object error, StackTrace stackTrace) {
_failedDispatch(loader, batch, error, stackTrace);
});
}