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/northstar/utils/Extensions.ts | 10 ++++++++-- src/client/util/UtilExtensions.ts | 22 ++++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/client/northstar/utils/Extensions.ts b/src/client/northstar/utils/Extensions.ts index ad3e06806..8254c775c 100644 --- a/src/client/northstar/utils/Extensions.ts +++ b/src/client/northstar/utils/Extensions.ts @@ -31,13 +31,19 @@ type BatchHandlerAsync = (batch: I[], context: BatchContext) => Promise type BatchConverter = BatchConverterSync | BatchConverterAsync; type BatchHandler = BatchHandlerSync | BatchHandlerAsync; type FixedBatcher = { batchSize: number } | { batchCount: number, mode?: Mode }; +interface ExecutorResult { + updated: A; + makeNextBatch: boolean; +} interface PredicateBatcher { - executor: (element: I, accumulator: A | undefined) => A | undefined; + executor: (element: I, accumulator: A) => ExecutorResult; initial: A; + persistAccumulator?: boolean; } interface PredicateBatcherAsync { - executor: (element: I, accumulator: A | undefined) => Promise; + executor: (element: I, accumulator: A) => Promise>; initial: A; + persistAccumulator?: boolean; } type Batcher = FixedBatcher | PredicateBatcher; type BatcherAsync = Batcher | PredicateBatcherAsync; 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