Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Last active April 18, 2023 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NetanelBasal/ffc05098febbdcc5d95481afe66f898d to your computer and use it in GitHub Desktop.
Save NetanelBasal/ffc05098febbdcc5d95481afe66f898d to your computer and use it in GitHub Desktop.
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: OfflineInterceptor, multi: true }
]
@Injectable()
export class OfflineInterceptor {
private onlineChanges$ = fromEvent(window, 'online').pipe(mapTo(true));
get isOnline() {
return navigator.onLine;
}
intercept(req, next) {
return next.handle(req).pipe(
retryWhen(errors => {
if (this.isOnline) {
return errors.pipe(switchMap(err => throwError(err)));
}
return this.onlineChanges$;
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment