|
|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { InternalStore } from '../utils/internal-store-utils';
|
|
|
|
|
import { fromEvent, merge, of, Subscription } from 'rxjs';
|
|
|
|
|
import { fromEvent, merge, of } from 'rxjs';
|
|
|
|
|
import { map } from 'rxjs/operators';
|
|
|
|
|
|
|
|
|
|
export interface InternetConnectionState{
|
|
|
|
|
@ -11,43 +10,9 @@ export interface InternetConnectionState{
|
|
|
|
|
providedIn: 'root',
|
|
|
|
|
})
|
|
|
|
|
export class InternetConnectionService{
|
|
|
|
|
private store = new InternalStore<InternetConnectionState>({ status: true });
|
|
|
|
|
networkStatus$: Subscription = Subscription.EMPTY;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
this.init()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private init(): void {
|
|
|
|
|
this.checkNetworkStatus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private checkNetworkStatus() {
|
|
|
|
|
this.networkStatus$ = merge(
|
|
|
|
|
of(null),
|
|
|
|
|
fromEvent(window, 'offline'),
|
|
|
|
|
fromEvent(window, 'online')
|
|
|
|
|
)
|
|
|
|
|
.pipe(map(() => navigator.onLine))
|
|
|
|
|
.subscribe(status => {
|
|
|
|
|
this.setStatus(status)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getStatus(){
|
|
|
|
|
return this.store.state.status
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getStatus$(){
|
|
|
|
|
return this.store.sliceState(({ status }) => status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateStatus$() {
|
|
|
|
|
return this.store.sliceUpdate(({ status }) => status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setStatus(status: boolean){
|
|
|
|
|
this.store.patch({ status })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
networkStatus$ = merge(
|
|
|
|
|
of(null),
|
|
|
|
|
fromEvent(window, 'offline'),
|
|
|
|
|
fromEvent(window, 'online')
|
|
|
|
|
).pipe(map(() => navigator.onLine))
|
|
|
|
|
}
|
|
|
|
|
|