batching_future 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 72

batching_future #

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!.

Example #

Define your batch computation (unique to your problem) #

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);

Create the batcher based on your computer #

import 'package:batching_future/batching_future.dart';

/// Create the batcher!
final directionsBatcher = createBatcher(
    cacheSize: 200,
    maxBatchSize: 20,
    maxWaitDuration: Duration(milliseconds: 200),

Issue requests against the batcher #

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:

  1. There's a cache hit, at which point, it will return immediately.
  2. When the maxWaitDuration is reached, and your batch computation then finishes.
  3. When the batch "queue" reaches length maxBatchSize, and your batch computation then finishes.

1.0.0 #

  • Switch single-method interface to a function typedef

0.0.3 #

  • Adds caching option
  • Better readme.

0.0.2 #

  • Adds example.

0.0.1 #

  • Initial version


import 'dart:async';

import 'package:batching_future/batching_future.dart';

main(List<String> args) async {
  final batcher = createBatcher<int, int>((i) => => i * 2).toList(),
      maxBatchSize: 3, maxWaitDuration: Duration(milliseconds: 500));
  final inputs = [4, 6, 8];
  final futures = => batcher.submit(i));
  final results = await Future.wait(futures);

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  batching_future: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:batching_future/batching_future.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
quiver ^2.0.5 2.1.3
synchronized ^2.1.0+1 2.2.0+1
Transitive dependencies
matcher 0.12.8
meta 1.2.1
path 1.7.0
stack_trace 1.9.5
Dev dependencies
test any