Created
October 12, 2020 18:59
-
-
Save LironHazan/47cc7ce993f7e17992b5c925a0259c10 to your computer and use it in GitHub Desktop.
Yoni Argivi's snippet for rxjs post
This file contains hidden or 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
public getHttpDistinctResponse(httpRequest$: Observable<any>): Observable<any> { | |
let cachedResponseStr = null; | |
return httpRequest$.pipe( | |
filter((response) => { | |
const currentResponseStr = JSON.stringify(response); | |
const areEquals = cachedResponseStr === currentResponseStr; | |
if (!areEquals) { | |
cachedResponseStr = currentResponseStr; | |
} | |
return !areEquals; | |
}) | |
); | |
} | |
export function pollingOnResolved(httpRequest$: Observable<any>, delayMs = 0): Observable<any> { | |
const polling$ = new BehaviorSubject({}); | |
const rePolling$ = | |
of('').pipe( | |
delay(delayMs), | |
tap(() => polling$.next({})), | |
skip(1) | |
); | |
// BOOM | |
const request = ignoreEqualResponses ? this.getHttpDistinctResponse(httpRequest$) : httpRequest$; | |
const httpPolling$ = concat(request, rePolling$); | |
return polling$.pipe(switchMap(() => httpPolling$)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
disagree on something that is testable, and easy to confirm is ignorance, anyway...
You said the best; you have a good weekend too. ✌️