Issue
I have an observable, which combines an interval and a date selection action. I use a combineLatest
and switchMap
to make a request with these two observables. The idea is the user is looking at a table for a specific date (which can be changed at any time), and that table should be updated every 20 seconds (this time is configurable by the user).
data$ = combineLatest(
this.refreshInterval$,
this.dateSelected$
).pipe(
switchMap(([_, date]) => this.requestDataForDate(date))
);
This works, except in the case where the request takes longer than 20 seconds. If the request is taking a long time, then every time the interval is triggered, it will cancel the ongoing request and make a new one. This makes sense because I am using a switchMap
, if I change to a concatMap
then this won't happen.
However, if the user changes the date, another request should be made and the previous one should be cancelled, which won't happen when using a concatMap
.
How can I achieve both of these? The request should resubmit if user changes the date, no matter what, but it should not resubmit if the interval is triggered while a request is ongoing
Solution
Using dateSelected$
as base you could:
first
switch
to therefreshInterval$
and then
exhaust|concat
the interval to the requestdata$ = this.dateSelected$.pipe( switchMap((date) => this.refreshInterval$.pipe(exhaustMap(() => this.requestDataForDate(date))) ) );
Answered By - akotech
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.