Skip to content

Commit

Permalink
Adding maxBatchSize to options
Browse files Browse the repository at this point in the history
  • Loading branch information
shimeez committed Sep 7, 2016
1 parent 68a2a2e commit 2c3165e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ Create a new `DataLoader` given a batch loading function and options.
- *batch*: Default `true`. Set to `false` to disable batching, instead
immediately invoking `batchLoadFn` with a single load key.

- *maxBatchSize*: Default infinite. Limits the number of items that get
passed in to the `batchLoadFn`.

- *cache*: Default `true`. Set to `false` to disable caching, instead
creating a new Promise and new key in the `batchLoadFn` for every load.

Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/dataloader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ describe('Primary API', () => {
expect(loadCalls).to.deep.equal([ [ 1, 2 ] ]);
});

it('batches multiple requests with max batch sizes', async () => {
var [ identityLoader, loadCalls ] = idLoader({ maxBatchSize: 2 });

var promise1 = identityLoader.load(1);
var promise2 = identityLoader.load(2);
var promise3 = identityLoader.load(3);

var [ value1, value2, value3 ] =
await Promise.all([ promise1, promise2, promise3 ]);
expect(value1).to.equal(1);
expect(value2).to.equal(2);
expect(value3).to.equal(3);

expect(loadCalls).to.deep.equal([ [ 1, 2 ], [ 3 ] ]);
});

it('coalesces identical requests', async () => {
var [ identityLoader, loadCalls ] = idLoader();

Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Options<K, V> = {
cache?: boolean;
cacheKeyFn?: (key: any) => any;
cacheMap?: CacheMap<K, Promise<V>>;
maxBatchSize?: number;
};

// If a custom cache is provided, it must be of this type (a subset of ES6 Map).
Expand Down Expand Up @@ -220,6 +221,14 @@ function dispatchQueue<K, V>(loader: DataLoader<K, V>) {
var queue = loader._queue;
loader._queue = [];

var maxBatchSize = loader._options && loader._options.maxBatchSize ||
queue.length;
for (var i = 0; i < queue.length / maxBatchSize; i++) {
loadKeys(loader, queue.slice(i * maxBatchSize, (i + 1) * maxBatchSize));
}
}

function loadKeys<K, V>(loader: DataLoader<K, V>, queue: LoaderQueue<K, V>) {
// Collect all keys to be loaded in this dispatch
var keys = queue.map(({ key }) => key);

Expand Down

0 comments on commit 2c3165e

Please sign in to comment.