batching_future 0.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • new63

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.
class _DirectionsRequestComputer implements BatchComputer<DirectionsRequest, Duration> {
    @override
  Future<List<Duration>> compute(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(
    _DirectionsRequestComputer(),
    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.

0.0.3 #

  • Adds caching option
  • Better readme.

0.0.2 #

  • Adds example.

0.0.1 #

  • Initial version

example/lib/main.dart

import 'dart:async';

import 'package:batching_future/batching_future.dart';

class Doubler implements BatchComputer<int, int> {
  @override
  Future<List<int>> compute(List<int> batchedInputs) {
    return Future.value(batchedInputs.map((i) => i * 2).toList());
  }
}

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

Use this package as a library

1. Depend on it

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


dependencies:
  batching_future: ^0.0.3

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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
30
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:batching_future/batching_future.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
quiver ^2.0.5 2.0.5
synchronized ^2.1.0+1 2.1.0+1
Transitive dependencies
matcher 0.12.5
meta 1.1.7
path 1.6.4
stack_trace 1.9.3
Dev dependencies
test any