Consider the case where requests against a particular API may be issued from many parts of your app asynchronously. Additionally, all these API requests could be more efficiently fetched if done in batch (as well as the API supporting batch requests).
Wouldn't it be nice if you could automatically bundle all these requests into batches with a minimal API? Welcome to batching_future
!.
import 'package:batching_future/batching_future.dart';
/// You define exactly how your batch computation is done here.
/// In this case, we send a single API to the Maps API to get the travel time to
/// many locations.
Future<List<Duration>> batchTravelTimes(List<DirectionsRequest> allRequests) async {
return MapsApi.travelTimesBatch(allRequests);
}
import 'package:batching_future/batching_future.dart';
/// Create the batcher!
final directionsBatcher = createBatcher(
batchTravelTimes,
cacheSize: 200,
maxBatchSize: 20,
maxWaitDuration: Duration(milliseconds: 200),
);
And get the result as a simple Future
!
final DirectionsRequest request = createYourRequest();
final Duration duration = await directionsBatcher.submit(request);
The duration
future will complete either when:
- There's a cache hit, at which point, it will return immediately.
- When the
maxWaitDuration
is reached, and your batch computation then finishes. - When the batch "queue" reaches length
maxBatchSize
, and your batch computation then finishes.