I'm trying to build an http-interceptor that waits with sending network requests until the onlinePipe
returns true
. The issue that I'm facing at the moment is that I can't seem to be able to come up with a solution that waits with returning a network request until the user is back online.
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpEvent, HttpHandler, HttpRequest, } from '@angular/common/http';
import { Observable, BehaviorSubject } from 'rxjs';
import { NetworkStatusService } from '../services/networkStatus.service';
@Injectable()
export class OfflineRequestInterceptor implements HttpInterceptor {
constructor(
private networkStatusService: NetworkStatusService,
) {
}
onlinePipe = this.networkStatusService.Online;
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const returnedRequest = new BehaviorSubject<Observable<HttpEvent<any>>>(null);
this.onlinePipe.subscribe((online) => {
if (online) {
returnedRequest.next(next.handle(request));
}
});
return returnedRequest.value;
}
}
As referred to here I could try to implement some kind of mergeMap. Unfortunately I haven't been able to come up with a similar solution used in that situation.