Being a newcomer to rxjs, I grasp most operators except for the specific use case involving BehaviorSubject
, filter
, and take
.
I am working on renewing an oauth access and refresh token pair within an Angular interceptor. While reviewing various codes from different sources, it appears that there is a common approach to tackling this task. However, there is one aspect that perplexes me.
For instance, in this example, requests are delayed until fresh access and refresh tokens are obtained.
private refreshSubject = new BehaviorSubject<any>(null);
// ...etc.
if (!this.refreshInProgress) {
// request new access and refresh tokens from server
// populate refreshSubject once tokens received
}
else {
return this.refreshSubject.pipe(
filter(result => result !== null), // <---- ???
take(1), // <---- ???
switchMap(() => next.handle(this.addToken(request)))
);
}
In all such instances, it is explained that requests are held up until refreshSubject
receives the updated tokens, with emphasis on the role of filter
.
This is the aspect that baffles me. The official rxjs documentation does not explicitly mention how or where the waiting and queuing process takes place when using filter
.