From 5c49467e4513e260647a74f84042662d78ccdbc7 Mon Sep 17 00:00:00 2001 From: Sam Wilkins Date: Wed, 18 Sep 2019 19:16:00 -0400 Subject: tweaks --- src/client/util/UtilExtensions.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/client/util') diff --git a/src/client/util/UtilExtensions.ts b/src/client/util/UtilExtensions.ts index da47fc1bc..1447e37cb 100644 --- a/src/client/util/UtilExtensions.ts +++ b/src/client/util/UtilExtensions.ts @@ -17,14 +17,19 @@ module.exports.BatchAsync = async function (batcher: BatcherAsync): module.exports.PredicateBatch = function (batcher: PredicateBatcher): T[][] { const batches: T[][] = []; let batch: T[] = []; - const { executor, initial } = batcher; - let accumulator: A | undefined = initial; + const { executor, initial, persistAccumulator } = batcher; + let accumulator = initial; for (let element of this) { - if ((accumulator = executor(element, accumulator)) !== undefined) { + const { updated, makeNextBatch } = executor(element, accumulator); + accumulator = updated; + if (!makeNextBatch) { batch.push(element); } else { batches.push(batch); batch = [element]; + if (!persistAccumulator) { + accumulator = initial; + } } } batches.push(batch); @@ -34,14 +39,19 @@ module.exports.PredicateBatch = function (batcher: PredicateBatcher) module.exports.PredicateBatchAsync = async function (batcher: PredicateBatcherAsync): Promise { const batches: T[][] = []; let batch: T[] = []; - const { executor, initial } = batcher; - let accumulator: A | undefined = initial; + const { executor, initial, persistAccumulator } = batcher; + let accumulator: A = initial; for (let element of this) { - if ((accumulator = await executor(element, accumulator)) !== undefined) { + const { updated, makeNextBatch } = await executor(element, accumulator); + accumulator = updated; + if (!makeNextBatch) { batch.push(element); } else { batches.push(batch); batch = [element]; + if (!persistAccumulator) { + accumulator = initial; + } } } batches.push(batch); -- cgit v1.2.3-70-g09d2