* * @param value Add new value to the average window and updated current average
(value: number)
| 26 | * @param value Add new value to the average window and updated current average |
| 27 | */ |
| 28 | public addValue(value: number): void { |
| 29 | const target = this.getNextIndex(); |
| 30 | const oldValue = this.inputValues[target]; |
| 31 | const newCount = |
| 32 | oldValue === undefined ? this.currentCount + 1 : this.currentCount; |
| 33 | |
| 34 | this.inputValues[target] = value; |
| 35 | |
| 36 | this.currentAverage = Math.max( |
| 37 | 0, |
| 38 | this.currentAverage * (this.currentCount / newCount) + |
| 39 | (value - (oldValue ?? 0)) / newCount |
| 40 | ); |
| 41 | |
| 42 | this.currentCount = newCount; |
| 43 | } |
| 44 | |
| 45 | private getNextIndex(): number { |
| 46 | // starts from 0 once we reach end of the array |