Created
May 18, 2020 14:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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