@johngw/stream
Version:
Reactive programming tools using the WHATWG Streams API.
42 lines • 1.07 kB
JavaScript
/**
* A behavior that will increase the debounce time whenever events
* are received within the current time. It's best to be used in
* conjunction with the {@link DebounceTrailingBehavior:class}
* or the {@link DebounceLeadingBehavior:class}.
*
* @group Transformers
* @see {@link debounce:function}
* @example
* --a--b---c----d----|
*
* debounce(5, [
* new DebounceBackOffBehavior({ inc: (ms) => ms * 2 }),
* new DebounceTrailingBehavior(),
* ])
*
* --T5-T10-T20--T40-d-
*/
export class DebounceBackOffBehavior {
#inc;
#max;
#startingMS = 0;
constructor({ inc, max = Number.MAX_SAFE_INTEGER, }) {
this.#inc = inc;
this.#max = max;
}
init(state) {
this.#startingMS = state.ms;
}
preTimer(state) {
return state.timer
? {
...state,
ms: Math.min(this.#inc(state.ms), this.#max),
}
: {
...state,
ms: this.#startingMS,
};
}
}
//# sourceMappingURL=BackOffBehavior.js.map