Answers for "angular no internet detection"

0

angular no internet detection

//Tested with Angular 8.1 and rxjs 6.5.2
import { Observable, Observer, fromEvent, merge } from 'rxjs';
import { map } from 'rxjs/operators';

ngOnInit(){
	this.createOnline$().subscribe(isOnline => console.log(isOnline));
}

createOnline$() {
return merge<boolean>(
  fromEvent(window, 'offline').pipe(map(() => false)),
  fromEvent(window, 'online').pipe(map(() => true)),
  new Observable((sub: Observer<boolean>) => {
	sub.next(navigator.onLine);
	sub.complete();
  }));
}
Posted by: Guest on July-08-2020

Code answers related to "angular no internet detection"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language