| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | /** |
| b69ab31 | | | 11 | * Incrementally increases throttling when an even starts happening too often. |
| b69ab31 | | | 12 | * For example, initially there's no throttle |
| b69ab31 | | | 13 | * After 10 events without a gap of 10s, there's a 10s throttle. |
| b69ab31 | | | 14 | * After 30 events without a gap of 30s, there's a 30s throttle. |
| b69ab31 | | | 15 | * After no events for 10s, the throttle is reset to 0. |
| b69ab31 | | | 16 | * |
| b69ab31 | | | 17 | * These thresholds are configurable. |
| b69ab31 | | | 18 | * "Throttling" means dropping events after the first one (unlike debouncing). |
| b69ab31 | | | 19 | */ |
| b69ab31 | | | 20 | export function stagedThrottler<P extends Array<unknown>>( |
| b69ab31 | | | 21 | stages: Array<{ |
| b69ab31 | | | 22 | throttleMs: number; |
| b69ab31 | | | 23 | /** number of input events needed to advance to the next stage. |
| b69ab31 | | | 24 | * Note: it doesn't matter if it was throttled or not. Every input adds to the advancement. */ |
| b69ab31 | | | 25 | numToNextStage?: number; |
| b69ab31 | | | 26 | resetAfterMs: number; |
| b69ab31 | | | 27 | /** Called when entering a stage. |
| b69ab31 | | | 28 | * Note: 0th stage onEnter is not called "on startup", only if you reset the stage, |
| b69ab31 | | | 29 | * and that this stage resets the next time a value IS emitted, not merely once the time passes. |
| b69ab31 | | | 30 | */ |
| b69ab31 | | | 31 | onEnter?: () => unknown; |
| b69ab31 | | | 32 | }>, |
| b69ab31 | | | 33 | cb: (...args: P) => void, |
| b69ab31 | | | 34 | ) { |
| b69ab31 | | | 35 | // Time of the last non-throttled call |
| b69ab31 | | | 36 | let lastEmitted = -Infinity; |
| b69ab31 | | | 37 | let currentStage = 0; |
| b69ab31 | | | 38 | let numSeen = 0; |
| b69ab31 | | | 39 | |
| b69ab31 | | | 40 | return (...args: P) => { |
| b69ab31 | | | 41 | const stage = nullthrows(stages[currentStage]); |
| b69ab31 | | | 42 | const currentThrottle = stage.throttleMs; |
| b69ab31 | | | 43 | const elapsed = Date.now() - lastEmitted; |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | // Input always counts towards going to the next stage |
| b69ab31 | | | 46 | numSeen++; |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | // Maybe go to the next stage |
| b69ab31 | | | 49 | if (numSeen > 1 && elapsed > stage.resetAfterMs) { |
| b69ab31 | | | 50 | // Reset the throttle |
| b69ab31 | | | 51 | numSeen = 0; |
| b69ab31 | | | 52 | currentStage = 0; |
| b69ab31 | | | 53 | stages[currentStage].onEnter?.(); |
| b69ab31 | | | 54 | } else if (stage.numToNextStage && numSeen >= stage.numToNextStage) { |
| b69ab31 | | | 55 | const nextStage = currentStage + 1; |
| b69ab31 | | | 56 | if (nextStage < stages.length) { |
| b69ab31 | | | 57 | numSeen = 0; |
| b69ab31 | | | 58 | currentStage++; |
| b69ab31 | | | 59 | stages[currentStage].onEnter?.(); |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | } |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | if (elapsed < currentThrottle) { |
| b69ab31 | | | 64 | // Needs to be throttled |
| b69ab31 | | | 65 | return; |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | // No need to throttle |
| b69ab31 | | | 69 | lastEmitted = Date.now(); |
| b69ab31 | | | 70 | return cb(...args); |
| b69ab31 | | | 71 | }; |
| b69ab31 | | | 72 | } |