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$)); | |
} | |
Let's agree to disagree.. I'm tired of this thread as it's pointless, that's ok to disagree, you think that my understanding is wrong I think that your understanding is wrong that's fine as long as we're chatting politely, IMO the software community should feel like a "safe" place..
Have a nice weekend and may the code be with you.
disagree on something that is testable, and easy to confirm is ignorance, anyway...
You said the best; you have a good weekend too. ✌️
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just stated the truth, the code has obvious bugs and it's incorrect, and it is a random copy-paste from different places. if you believe that "overall snippet is correct" please copy it inside a project and make it works.
I understand you felt attack from my tone because you are the reviewer and you did couple of obvious mistakes, but I don't care who did the mistake, so don't get it personally.
your definition of the closure is correct but your understanding & usage is not. and yet my defintion is correct
that's how shared scope is.
Scope of a function is limited to that function. When two functions are side by side of each other, their shared scope is the file/class. your usage of
let cachedResponseStr = null;
is incorrect.moreover, as I stated before because it's not a homogenous snippet, the whole
getHttpDistinctResponse()
is redundant, a simpledistinctUntilChange
function at the end of the retrun would do the same since the cache object isn't a real cache and still it's calling the backend on every interval.