1//Tested with Angular 8.1 and rxjs 6.5.2
2import { Observable, Observer, fromEvent, merge } from 'rxjs';
3import { map } from 'rxjs/operators';
4
5ngOnInit(){
6 this.createOnline$().subscribe(isOnline => console.log(isOnline));
7}
8
9createOnline$() {
10return merge<boolean>(
11 fromEvent(window, 'offline').pipe(map(() => false)),
12 fromEvent(window, 'online').pipe(map(() => true)),
13 new Observable((sub: Observer<boolean>) => {
14 sub.next(navigator.onLine);
15 sub.complete();
16 }));
17}