fix(classes): cleanup resources, add cancellable timeouts, and fix bugs in several core utility classes
This commit is contained in:
@@ -8,6 +8,7 @@ export interface ITimedAggregatorOptions<T> {
|
||||
export class TimedAggregtor<T> {
|
||||
public options: ITimedAggregatorOptions<T>;
|
||||
private storageArray: T[] = [];
|
||||
private isStopped = false;
|
||||
|
||||
constructor(optionsArg: ITimedAggregatorOptions<T>) {
|
||||
this.options = optionsArg;
|
||||
@@ -15,9 +16,16 @@ export class TimedAggregtor<T> {
|
||||
|
||||
private aggregationTimer: plugins.smarttime.Timer;
|
||||
private checkAggregationStatus() {
|
||||
if (this.isStopped) {
|
||||
return;
|
||||
}
|
||||
const addAggregationTimer = () => {
|
||||
this.aggregationTimer = new plugins.smarttime.Timer(this.options.aggregationIntervalInMillis);
|
||||
this.aggregationTimer.completed.then(() => {
|
||||
if (this.isStopped) {
|
||||
this.aggregationTimer = null;
|
||||
return;
|
||||
}
|
||||
const aggregateForProcessing = this.storageArray;
|
||||
if (aggregateForProcessing.length === 0) {
|
||||
this.aggregationTimer = null;
|
||||
@@ -35,7 +43,29 @@ export class TimedAggregtor<T> {
|
||||
}
|
||||
|
||||
public add(aggregationArg: T) {
|
||||
if (this.isStopped) {
|
||||
return;
|
||||
}
|
||||
this.storageArray.push(aggregationArg);
|
||||
this.checkAggregationStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* stops the aggregation timer chain
|
||||
* @param flushRemaining if true, calls functionForAggregation with any remaining items
|
||||
*/
|
||||
public stop(flushRemaining: boolean = false) {
|
||||
this.isStopped = true;
|
||||
if (this.aggregationTimer) {
|
||||
this.aggregationTimer.reset();
|
||||
this.aggregationTimer = null;
|
||||
}
|
||||
if (flushRemaining && this.storageArray.length > 0) {
|
||||
const remaining = this.storageArray;
|
||||
this.storageArray = [];
|
||||
this.options.functionForAggregation(remaining);
|
||||
} else {
|
||||
this.storageArray = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user