@Injectable()classHttpService{privateretryLogicSubject:Subject<any>;constructor(privatehttp:HttpClient,privatestore:Store<any>){}ngOnInit(){this.store.map((state)=>state.tokens).subscribe((tokens)=>{if(this.retryLogicSubject){this.retryLogicSubject.next({})}});}publicgetWithRetryLogic(url){returnObservable.defer(()=>this.http.get(url,this.options)).catch((err)=>{if(error.status===401){returnObservable.throw(error);}returnObservable.of(error);}).retryWhen((error)=>{this.retryLogicSubject=newSubject();this.startRefreshTokensProcess();returnthis.retryLogicSubject.asObservable()}).map(()=>{if(data.status<400&&data.status>=200){//check for errorsthis.retryLogicSubject=undefined;returndata.json();}throwdata;// back to error stream}).timeout(5000)}privatestartRefreshTokensProcess(){letdata={refreshToken:this.refreshToken}this.http.post('refreshTokenUrl',data).subscribe((data:Response)=>this.saveTokensToStore(data.json()),(error)=>this.getTokenFromParentAndSaveToStore());}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My the-most-proud-code using RxJS is described in my article: medium.com/@alexanderposhtaruk/rx-...
In short: