UNPKG

ng-connection-monitor

Version:

A lightweight Angular service to detect real internet connection using a backend ping, not navigator.onLine.

88 lines (80 loc) 4.22 kB
import * as i0 from '@angular/core'; import { Injectable, Component, NgModule } from '@angular/core'; import { BehaviorSubject, timer, switchMap, distinctUntilChanged, of, catchError } from 'rxjs'; import * as i1 from '@angular/common/http'; import { CommonModule } from '@angular/common'; const LIB_ENVIRONMENT = { pingUrl: 'https://my-ping-server.vercel.app/api/ping' }; class NgConnectionMonitorService { constructor(http, ngZone) { this.http = http; this.ngZone = ngZone; this.onlineStatus$ = new BehaviorSubject(true); this.pingInterval = 3000; this.pingUrl = LIB_ENVIRONMENT.pingUrl; this.monitorNetworkStatus(); } get isOnline$() { return this.onlineStatus$.asObservable(); } monitorNetworkStatus() { this.ngZone.runOutsideAngular(() => { timer(0, this.pingInterval) .pipe(switchMap(() => this.pingBackend()), distinctUntilChanged()) .subscribe((isOnline) => { this.ngZone.run(() => this.onlineStatus$.next(isOnline)); }); }); } pingBackend() { return this.http.get(this.pingUrl, { observe: 'response' }).pipe(switchMap((response) => of(response.status === 200)), catchError(() => of(false))); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgConnectionMonitorService, deps: [{ token: i1.HttpClient }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgConnectionMonitorService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgConnectionMonitorService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [{ type: i1.HttpClient }, { type: i0.NgZone }] }); class NgConnectionMonitorComponent { static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgConnectionMonitorComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); } static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: NgConnectionMonitorComponent, isStandalone: true, selector: "lib-ng-connection-monitor", ngImport: i0, template: ` <p> ng-connection-monitor works! </p> `, isInline: true, styles: [""] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgConnectionMonitorComponent, decorators: [{ type: Component, args: [{ selector: 'lib-ng-connection-monitor', standalone: true, imports: [], template: ` <p> ng-connection-monitor works! </p> ` }] }] }); class NgConnectionMonitorModule { static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgConnectionMonitorModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.12", ngImport: i0, type: NgConnectionMonitorModule, imports: [CommonModule] }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgConnectionMonitorModule, providers: [NgConnectionMonitorService], imports: [CommonModule] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgConnectionMonitorModule, decorators: [{ type: NgModule, args: [{ declarations: [], imports: [ CommonModule ], providers: [NgConnectionMonitorService] }] }] }); /* * Public API Surface of ng-connection-monitor */ /** * Generated bundle index. Do not edit. */ export { NgConnectionMonitorComponent, NgConnectionMonitorModule, NgConnectionMonitorService }; //# sourceMappingURL=ng-connection-monitor.mjs.map