From 7aa0f05baee0541ad5acddeeb971ef359cb0eb51 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Fri, 2 Aug 2019 16:32:52 +0200 Subject: [PATCH] fix(core): update --- .vscode/settings.json | 3 +++ test/test.timedaggregator.ts | 25 +++++++++++++++++++++ ts/index.ts | 1 + ts/lik.plugins.ts | 2 +- ts/lik.timedaggregator.ts | 42 ++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 test/test.timedaggregator.ts create mode 100644 ts/lik.timedaggregator.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6649301 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "workbench.colorCustomizations": {} +} \ No newline at end of file diff --git a/test/test.timedaggregator.ts b/test/test.timedaggregator.ts new file mode 100644 index 0000000..74d92ce --- /dev/null +++ b/test/test.timedaggregator.ts @@ -0,0 +1,25 @@ +// import test framework +import { expect, tap } from '@pushrocks/tapbundle'; +import * as events from 'events'; +import * as smartpromise from '@pushrocks/smartpromise'; + +// import the module +import * as lik from '../ts/index'; + +let testTimedAggregator: lik.TimedAggregtor; + +tap.test('should create a timed aggregaotor', async (tools) => { + testTimedAggregator = new lik.TimedAggregtor({ + aggregationIntervalInMillis: 1000, + functionForAggregation: (aggregation) => { + console.log(aggregation); + } + }); + testTimedAggregator.add('This'); + testTimedAggregator.add('is a whole sentence.'); + await tools.delayFor(1001); + testTimedAggregator.add('This one is another.'); + await tools.delayFor(2000); +}); + +tap.start(); diff --git a/ts/index.ts b/ts/index.ts index 98edefe..c1d9766 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -7,4 +7,5 @@ export * from './lik.limitedarray'; export * from './lik.looptracker'; export * from './lik.objectmap'; export * from './lik.stringmap'; +export * from './lik.timedaggregator'; export * from './lik.tree'; diff --git a/ts/lik.plugins.ts b/ts/lik.plugins.ts index 0264c61..2c1ee8c 100644 --- a/ts/lik.plugins.ts +++ b/ts/lik.plugins.ts @@ -19,7 +19,7 @@ export { smartdelay, smartpromise, smartrx, smarttime }; // ============== // third party // ============== -import * as minimatch from 'minimatch'; +import minimatch from 'minimatch'; const symbolTree = require('symbol-tree'); export { minimatch, symbolTree }; diff --git a/ts/lik.timedaggregator.ts b/ts/lik.timedaggregator.ts new file mode 100644 index 0000000..89a383f --- /dev/null +++ b/ts/lik.timedaggregator.ts @@ -0,0 +1,42 @@ +import * as plugins from './lik.plugins'; + +export interface ITimedAggregatorOptions { + aggregationIntervalInMillis: number; + functionForAggregation: (input: T[]) => void; +} + +export class TimedAggregtor { + public options: ITimedAggregatorOptions; + private storageArray: T[] = []; + + constructor(optionsArg: ITimedAggregatorOptions) { + this.options = optionsArg; + + } + + private aggregationTimer: plugins.smarttime.Timer; + private checkAggregationStatus () { + const addAggregationTimer = () => { + this.aggregationTimer = new plugins.smarttime.Timer(this.options.aggregationIntervalInMillis); + this.aggregationTimer.completed.then(() => { + const aggregateForProcessing = this.storageArray; + if (aggregateForProcessing.length === 0) { + this.aggregationTimer = null; + return; + } + this.storageArray = []; + addAggregationTimer(); + this.options.functionForAggregation(aggregateForProcessing); + }); + this.aggregationTimer.start(); + }; + if (!this.aggregationTimer) { + addAggregationTimer(); + } + } + + public add(aggregationArg: T) { + this.storageArray.push(aggregationArg); + this.checkAggregationStatus(); + } +}; \ No newline at end of file