Skip to content

Instantly share code, notes, and snippets.

@LironHazan
Created October 12, 2020 18:59
Show Gist options
  • Save LironHazan/47cc7ce993f7e17992b5c925a0259c10 to your computer and use it in GitHub Desktop.
Save LironHazan/47cc7ce993f7e17992b5c925a0259c10 to your computer and use it in GitHub Desktop.
Yoni Argivi's snippet for rxjs post
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$));
}
@mhamri
Copy link

mhamri commented Jun 17, 2021

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

a closure it's a variable outside of a function that is used inside a function

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 simple distinctUntilChange 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.

@LironHazan
Copy link
Author

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.

@mhamri
Copy link

mhamri commented Jun 17, 2021

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