Use a circular array to store time measurements.
[e-mobility-charging-stations-simulator.git] / src / utils / CircularArray.ts
1 import Constants from './Constants';
2
3 export default class CircularArray<T> extends Array<T> {
4 public size: number;
5
6 constructor(size: number = Constants.MAXIMUM_MEASUREMENTS_NUMBER) {
7 super();
8 this.size = size;
9 }
10
11 push(...items: T[]): number {
12 while (this.length > this.size) {
13 this.shift();
14 }
15 return super.push(...items);
16 }
17
18 unshift(...items: T[]): number {
19 while (this.length > this.size) {
20 this.pop();
21 }
22 return super.unshift(...items);
23 }
24 }