Skip to content

Instantly share code, notes, and snippets.

@JonCatmull
Created May 18, 2020 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonCatmull/2a562d974914a605047c6b476631507d to your computer and use it in GitHub Desktop.
Save JonCatmull/2a562d974914a605047c6b476631507d to your computer and use it in GitHub Desktop.
RxJS custom operator to debounce a stream for set time after first value is emitted.
/**
* Debounce after first emit.
* This operator will debounce all requests apart from the first for a set duration.
* @param ms time to wait in milliconds.
*/
const debounceAfterFirst = <T,>(ms: number) => (source: Observable<T>) => {
return source.pipe(
map((val: T, index: number) => ({ val, index } as { index: number; val: T })),
debounce(({ index }) => timer(index === 0 ? 0 : ms)),
map(({ val }) => val)
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment