Last active
April 18, 2023 07:47
-
-
Save NetanelBasal/ffc05098febbdcc5d95481afe66f898d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
providers: [ | |
{ provide: HTTP_INTERCEPTORS, useClass: OfflineInterceptor, multi: true } | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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