Skip to content

Instantly share code, notes, and snippets.

@JanMalch
Created May 30, 2022 18:54
Show Gist options
  • Save JanMalch/5f3fc24927cf8030ac773c8345f12deb to your computer and use it in GitHub Desktop.
Save JanMalch/5f3fc24927cf8030ac773c8345f12deb to your computer and use it in GitHub Desktop.
Custom bufferDebounce RxJS operator
import { buffer, debounceTime, OperatorFunction, SchedulerLike } from 'rxjs';
/**
* Emits buffered values from the source Observable only after a particular time span
* has passed without another source emission.
*
* @param {number} dueTime The timeout duration in milliseconds (or the time
* unit determined internally by the optional `scheduler`) for the window of
* time required to wait for emission silence before emitting the buffered
* source values.
* @param {SchedulerLike} scheduler The {@link SchedulerLike} to use for
* managing the timers that handle the timeout for each value.
* @author https://stackoverflow.com/a/62598663
*/
export function bufferDebounce<T>(
dueTime: number,
scheduler?: SchedulerLike,
): OperatorFunction<T, T[]> {
return (source) =>
source.pipe(buffer(source.pipe(debounceTime(dueTime, scheduler))));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment