Improved network switching.

This commit is contained in:
softsimon
2020-05-10 12:31:57 +07:00
parent 21844701b0
commit e876267d4f
3 changed files with 38 additions and 30 deletions

View File

@@ -38,30 +38,36 @@ export class StateService {
constructor(
private router: Router,
) {
this.setNetworkBasedonUrl(window.location.pathname);
this.router.events.subscribe((event) => {
if (event instanceof NavigationStart) {
switch (event.url.split('/')[1]) {
case 'liquid':
case 'liquid-tv':
if (this.network !== 'liquid') {
this.network = 'liquid';
this.networkChanged$.next('liquid');
}
return;
case 'testnet':
case 'testnet-tv':
if (this.network !== 'testnet') {
this.network = 'testnet';
this.networkChanged$.next('testnet');
}
return;
default:
if (this.network !== '') {
this.network = '';
this.networkChanged$.next('');
}
}
this.setNetworkBasedonUrl(event.url);
}
});
}
setNetworkBasedonUrl(url: string) {
switch (url.split('/')[1]) {
case 'liquid':
case 'liquid-tv':
if (this.network !== 'liquid') {
this.network = 'liquid';
this.networkChanged$.next('liquid');
}
return;
case 'testnet':
case 'testnet-tv':
if (this.network !== 'testnet') {
this.network = 'testnet';
this.networkChanged$.next('testnet');
}
return;
default:
if (this.network !== '') {
this.network = '';
this.networkChanged$.next('');
}
}
}
}

View File

@@ -24,14 +24,20 @@ export class WebsocketService {
private onlineCheckTimeout: number;
private onlineCheckTimeoutTwo: number;
private subscription: Subscription;
private network = '';
constructor(
private stateService: StateService,
) {
this.websocketSubject = webSocket<WebsocketResponse | any>(WEB_SOCKET_URL + '/' + this.stateService.network);
this.network = this.stateService.network;
this.websocketSubject = webSocket<WebsocketResponse | any>(WEB_SOCKET_URL + '/' + this.network);
this.startSubscription();
this.stateService.networkChanged$.subscribe((network) => {
if (network === this.network) {
return;
}
this.network = network;
clearTimeout(this.onlineCheckTimeout);
clearTimeout(this.onlineCheckTimeoutTwo);