Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const PING_INTERVAL = 1000;
- const PING_URL = 'http://localhost:4200';
- const online$ = new BehaviorSubject<boolean>(navigator.onLine);
- const ping$ = online$.pipe(
- mergeMap((online) => {
- if (online) {
- return interval(PING_INTERVAL).pipe(
- takeWhile(() => !!online$.value),
- mergeMap(() => {
- return fromPromise(fetch(PING_URL, {
- method: 'head',
- mode: 'cors',
- }).then());
- }),
- pluck('status'),
- map(status => status === 200),
- );
- }
- return of(online);
- }),
- );
- window.addEventListener('online', () => {
- online$.next(navigator.onLine);
- });
- window.addEventListener('offline', () => {
- online$.next(navigator.onLine);
- });
- online$.subscribe((yeah) => console.log('online is', yeah));
- ping$.subscribe((status) => console.log('ping', status));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement