Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(network): new network service that return the network status #380

Merged
merged 2 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/core/src/lib/network/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './network.service';
export * from './network.module';
10 changes: 10 additions & 0 deletions packages/core/src/lib/network/network.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NetworkService } from './network.service';

@NgModule({
declarations: [],
imports: [CommonModule],
providers: [NetworkService]
})
export class NetworkModule { }
12 changes: 12 additions & 0 deletions packages/core/src/lib/network/network.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';

import { NetworkService } from './network.service';

describe('NetworkService', () => {
beforeEach(() => TestBed.configureTestingModule({}));

it('should be created', () => {
const service: NetworkService = TestBed.get(NetworkService);
expect(service).toBeTruthy();
});
});
75 changes: 75 additions & 0 deletions packages/core/src/lib/network/network.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Injectable, EventEmitter, OnDestroy, AfterViewInit, OnInit, Injector } from '@angular/core';
import { Observable, Subscription, fromEvent } from 'rxjs';
import { debounceTime, startWith } from 'rxjs/operators';

import { MessageService } from '../message';
import { LanguageService } from '../language/shared/language.service';

export interface ConnectionState {
connection: boolean;
}

@Injectable({
providedIn: 'root'
})
export class NetworkService implements OnDestroy {

private stateChangeEventEmitter = new EventEmitter<ConnectionState>();
private onlineSubscription: Subscription;
private offlineSubscription: Subscription;

private state: ConnectionState = {
connection: window.navigator.onLine
};

constructor(
private messageService: MessageService,
private injector: Injector
) {
this.checkNetworkState();
}

private checkNetworkState() {
this.onlineSubscription = fromEvent(window, 'online').subscribe(() => {
const translate = this.injector.get(LanguageService).translate;
const message = translate.instant('igo.core.network.online.message');
const title = translate.instant('igo.core.network.online.title');
this.messageService.info(message, title);
this.state.connection = true;
this.emitEvent();
});

this.offlineSubscription = fromEvent(window, 'offline').subscribe(() => {
const translate = this.injector.get(LanguageService).translate;
const message = translate.instant('igo.core.network.offline.message');
const title = translate.instant('igo.core.network.offline.title');
this.messageService.info(message, title);
this.state.connection = false;
this.emitEvent();
});
}

private emitEvent() {
this.stateChangeEventEmitter.emit(this.state);
}

ngOnDestroy(): void {
try {
this.offlineSubscription.unsubscribe();
this.onlineSubscription.unsubscribe();
} catch (e) {
}
}

currentState(reportState = true): Observable<ConnectionState> {
return reportState ?
this.stateChangeEventEmitter.pipe(
debounceTime(300),
startWith(this.state),
)
:
this.stateChangeEventEmitter.pipe(
debounceTime(300)
);
}
}
10 changes: 10 additions & 0 deletions packages/core/src/locale/en.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
"Sorry, service is currently not available. Please try again later",
"title": "Error server"
}
},
"network": {
"online": {
"message": "Online",
"title": "Status:"
},
"offline": {
"message": "Offline",
"title": "Status:"
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/locale/fr.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
"Désolé! Le service que vous voulez utiliser n’est malheureusement pas disponible pour le moment. Essayez plus tard!",
"title": "Erreur serveur"
}
},
"network": {
"online": {
"message": "En ligne",
"title": "Statut:"
},
"offline": {
"message": "Hors-Ligne",
"title": "Statut:"
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * from './lib/language';
export * from './lib/media';
export * from './lib/message';
export * from './lib/request';
export * from './lib/network';