fix(core): update
This commit is contained in:
@ -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';
|
||||
|
@ -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 };
|
||||
|
42
ts/lik.timedaggregator.ts
Normal file
42
ts/lik.timedaggregator.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import * as plugins from './lik.plugins';
|
||||
|
||||
export interface ITimedAggregatorOptions<T> {
|
||||
aggregationIntervalInMillis: number;
|
||||
functionForAggregation: (input: T[]) => void;
|
||||
}
|
||||
|
||||
export class TimedAggregtor<T> {
|
||||
public options: ITimedAggregatorOptions<T>;
|
||||
private storageArray: T[] = [];
|
||||
|
||||
constructor(optionsArg: ITimedAggregatorOptions<T>) {
|
||||
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();
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user