Calculates the average number of scroll events
(now = Date.now())
| 118 | |
| 119 | /** Calculates the average number of scroll events */ |
| 120 | tick(now = Date.now()): number { |
| 121 | this.tickHistory.enqueue(now) |
| 122 | |
| 123 | let oldestTick = this.tickHistory.peek() ?? now |
| 124 | while (oldestTick < now - this.rollingWindowMs) { |
| 125 | this.tickHistory.dequeue() |
| 126 | oldestTick = this.tickHistory.peek() ?? now |
| 127 | } |
| 128 | |
| 129 | this.buffer += clamp( |
| 130 | this.tickHistory.length * this.multiplier, |
| 131 | -this.maxRows, |
| 132 | this.maxRows, |
| 133 | ) |
| 134 | const rows = Math.floor(this.buffer) |
| 135 | this.buffer -= rows |
| 136 | return rows |
| 137 | } |
| 138 | |
| 139 | reset(): void { |
| 140 | this.tickHistory.clear() |