Skip to content

Commit a972eb5

Browse files
committed
Follow-ups to #42
- Add maxBatchSize to type definitions. - Add some additional comments. - Optimize common path. - Fix lint and type checking errors.
1 parent baf07db commit a972eb5

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Create a new `DataLoader` given a batch loading function and options.
160160
- *batch*: Default `true`. Set to `false` to disable batching, instead
161161
immediately invoking `batchLoadFn` with a single load key.
162162

163-
- *maxBatchSize*: Default infinite. Limits the number of items that get
163+
- *maxBatchSize*: Default `Infinity`. Limits the number of items that get
164164
passed in to the `batchLoadFn`.
165165

166166
- *cache*: Default `true`. Set to `false` to disable caching, instead

src/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ type Options<K, V> = {
3030
*/
3131
batch?: boolean,
3232

33+
/**
34+
* Default `Infinity`. Limits the number of items that get
35+
* passed in to the `batchLoadFn`.
36+
*/
37+
maxBatchSize?: number;
38+
3339
/**
3440
* Default `true`. Set to `false` to disable caching,
3541
* instead creating a new Promise and new key in

src/index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ type BatchLoadFn<K, V> = (keys: Array<K>) => Promise<Array<V | Error>>;
1616
// custom cache instance.
1717
type Options<K, V> = {
1818
batch?: boolean;
19+
maxBatchSize?: number;
1920
cache?: boolean;
2021
cacheKeyFn?: (key: any) => any;
2122
cacheMap?: CacheMap<K, Promise<V>>;
22-
maxBatchSize?: number;
2323
};
2424

2525
// If a custom cache is provided, it must be of this type (a subset of ES6 Map).
@@ -222,14 +222,25 @@ function dispatchQueue<K, V>(loader: DataLoader<K, V>) {
222222
var queue = loader._queue;
223223
loader._queue = [];
224224

225-
var maxBatchSize = loader._options && loader._options.maxBatchSize ||
226-
queue.length;
227-
for (var i = 0; i < queue.length / maxBatchSize; i++) {
228-
loadKeys(loader, queue.slice(i * maxBatchSize, (i + 1) * maxBatchSize));
225+
// If a maxBatchSize was provided and the queue is longer, then segment the
226+
// queue into multiple batches, otherwise treat the queue as a single batch.
227+
var maxBatchSize = loader._options && loader._options.maxBatchSize;
228+
if (maxBatchSize && maxBatchSize > 0 && maxBatchSize < queue.length) {
229+
for (var i = 0; i < queue.length / maxBatchSize; i++) {
230+
dispatchQueueBatch(
231+
loader,
232+
queue.slice(i * maxBatchSize, (i + 1) * maxBatchSize)
233+
);
234+
}
235+
} else {
236+
dispatchQueueBatch(loader, queue);
229237
}
230238
}
231239

232-
function loadKeys<K, V>(loader: DataLoader<K, V>, queue: LoaderQueue<K, V>) {
240+
function dispatchQueueBatch<K, V>(
241+
loader: DataLoader<K, V>,
242+
queue: LoaderQueue<K, V>
243+
) {
233244
// Collect all keys to be loaded in this dispatch
234245
var keys = queue.map(({ key }) => key);
235246

0 commit comments

Comments
 (0)