diff options
author | Sam Wilkins <samwilkins333@gmail.com> | 2019-09-18 19:16:00 -0400 |
---|---|---|
committer | Sam Wilkins <samwilkins333@gmail.com> | 2019-09-18 19:16:00 -0400 |
commit | 5c49467e4513e260647a74f84042662d78ccdbc7 (patch) | |
tree | ad9d9e68b5a0dd5e81eb4c78c450acd79bc30f39 /src/client/util/UtilExtensions.ts | |
parent | 30ec493a6dae644b0907f6ee6e86696c06aa11b4 (diff) |
tweaks
Diffstat (limited to 'src/client/util/UtilExtensions.ts')
-rw-r--r-- | src/client/util/UtilExtensions.ts | 22 |
1 files changed, 16 insertions, 6 deletions
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 <T, A>(batcher: BatcherAsync<T, A>): module.exports.PredicateBatch = function <T, A>(batcher: PredicateBatcher<T, A>): 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 <T, A>(batcher: PredicateBatcher<T, A>) module.exports.PredicateBatchAsync = async function <T, A>(batcher: PredicateBatcherAsync<T, A>): Promise<T[][]> { 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); |