aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
Diffstat (limited to 'src/client')
-rw-r--r--src/client/northstar/utils/Extensions.ts15
-rw-r--r--src/client/util/UtilExtensions.ts21
2 files changed, 30 insertions, 6 deletions
diff --git a/src/client/northstar/utils/Extensions.ts b/src/client/northstar/utils/Extensions.ts
index 3722bfd52..ad3e06806 100644
--- a/src/client/northstar/utils/Extensions.ts
+++ b/src/client/northstar/utils/Extensions.ts
@@ -42,6 +42,17 @@ interface PredicateBatcherAsync<I, A> {
type Batcher<I, A> = FixedBatcher | PredicateBatcher<I, A>;
type BatcherAsync<I, A> = Batcher<I, A> | PredicateBatcherAsync<I, A>;
+enum TimeUnit {
+ Milliseconds,
+ Seconds,
+ Minutes
+}
+
+interface Interval {
+ magnitude: number;
+ unit: TimeUnit;
+}
+
interface Array<T> {
fixedBatch<T>(batcher: FixedBatcher): T[][];
predicateBatch<T, A = undefined>(batcher: PredicateBatcher<T, A>): T[][];
@@ -55,8 +66,8 @@ interface Array<T> {
batchedForEachAsync<A = undefined>(batcher: Batcher<T, A>, handler: BatchHandler<T>): Promise<void>;
batchedMapAsync<O, A = undefined>(batcher: Batcher<T, A>, handler: BatchConverter<T, O>): Promise<O[]>;
- batchedForEachInterval<A = undefined>(batcher: Batcher<T, A>, handler: BatchHandler<T>, interval: number): Promise<void>;
- batchedMapInterval<O, A = undefined>(batcher: Batcher<T, A>, handler: BatchConverter<T, O>, interval: number): Promise<O[]>;
+ batchedForEachInterval<A = undefined>(batcher: Batcher<T, A>, handler: BatchHandler<T>, interval: Interval): Promise<void>;
+ batchedMapInterval<O, A = undefined>(batcher: Batcher<T, A>, handler: BatchConverter<T, O>, interval: Interval): Promise<O[]>;
lastElement(): T;
}
diff --git a/src/client/util/UtilExtensions.ts b/src/client/util/UtilExtensions.ts
index 8bd8fd581..da47fc1bc 100644
--- a/src/client/util/UtilExtensions.ts
+++ b/src/client/util/UtilExtensions.ts
@@ -174,7 +174,20 @@ module.exports.ConvertInBatchesAsync = async function <I, O, A>(batcher: Batcher
return collector;
};
-module.exports.ExecuteInBatchesAtInterval = async function <I, A>(batcher: BatcherAsync<I, A>, handler: BatchHandler<I>, interval: number): Promise<void> {
+const convert = (interval: Interval) => {
+ const { magnitude, unit } = interval;
+ switch (unit) {
+ default:
+ case TimeUnit.Milliseconds:
+ return magnitude;
+ case TimeUnit.Seconds:
+ return magnitude * 1000;
+ case TimeUnit.Minutes:
+ return magnitude * 1000 * 60;
+ }
+};
+
+module.exports.ExecuteInBatchesAtInterval = async function <I, A>(batcher: BatcherAsync<I, A>, handler: BatchHandler<I>, interval: Interval): Promise<void> {
if (!this.length) {
return;
}
@@ -194,7 +207,7 @@ module.exports.ExecuteInBatchesAtInterval = async function <I, A>(batcher: Batch
};
await handler(batch, context);
resolve();
- }, interval * 1000);
+ }, convert(interval));
});
if (++completed === quota) {
break;
@@ -204,7 +217,7 @@ module.exports.ExecuteInBatchesAtInterval = async function <I, A>(batcher: Batch
});
};
-module.exports.ConvertInBatchesAtInterval = async function <I, O, A>(batcher: BatcherAsync<I, A>, handler: BatchConverter<I, O>, interval: number): Promise<O[]> {
+module.exports.ConvertInBatchesAtInterval = async function <I, O, A>(batcher: BatcherAsync<I, A>, handler: BatchConverter<I, O>, interval: Interval): Promise<O[]> {
if (!this.length) {
return [];
}
@@ -225,7 +238,7 @@ module.exports.ConvertInBatchesAtInterval = async function <I, O, A>(batcher: Ba
};
collector.push(...(await handler(batch, context)));
resolve();
- }, interval * 1000);
+ }, convert(interval));
});
if (++completed === quota) {
resolve(collector);