From 84212c0c33fde77cbc4a6997c1d4f42dcfc9e608 Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Fri, 12 Jun 2020 12:05:27 +0200 Subject: [PATCH 1/4] Update server.service.ts --- src/app/services/server.service.ts | 55 +++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/app/services/server.service.ts b/src/app/services/server.service.ts index 1f5e03a1..8bdae05d 100644 --- a/src/app/services/server.service.ts +++ b/src/app/services/server.service.ts @@ -3,26 +3,51 @@ import { IndexedDbService } from './indexed-db.service'; import { Server } from '../models/server'; import { Observable } from 'rxjs'; import { HttpServer } from './http-server.service'; +import { ToasterService } from './toaster.service'; @Injectable() export class ServerService { private tablename = 'servers'; private ready: Promise; + private isIncognitoMode: boolean = false; + private serverIdsInIncognitoMode: string[] = []; constructor( private indexedDbService: IndexedDbService, - private httpServer: HttpServer + private httpServer: HttpServer, + private toasterService: ToasterService ) { this.ready = indexedDbService.get().openDatabase(1, evt => { evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true }); }); + + this.ready.then(() => {}).catch(() => { + this.isIncognitoMode = true; + }) } public get(id: number): Promise { + if (this.isIncognitoMode) { + let server: Server = JSON.parse(localStorage.getItem(`server-${id}`)); + let promise = new Promise(resolve => { + return server; + }); + return promise; + } + return this.onReady(() => this.indexedDbService.get().getByKey(this.tablename, id)) as Promise; } public create(server: Server) { + if (this.isIncognitoMode) { + server.id = this.serverIdsInIncognitoMode.length + 1; + localStorage.setItem(`server-${server.id}`, JSON.stringify(server)); + let promise = new Promise(resolve => { + return server; + }); + return promise; + } + return this.onReady(() => { const promise = new Promise((resolve, reject) => { this.indexedDbService @@ -38,6 +63,13 @@ export class ServerService { } public update(server: Server) { + if (this.isIncognitoMode) { + let promise = new Promise(resolve => { + return []; + }); + return promise; + } + return this.onReady(() => { const promise = new Promise((resolve, reject) => { this.indexedDbService @@ -52,10 +84,24 @@ export class ServerService { } public findAll() { + if (this.isIncognitoMode) { + let promise = new Promise(resolve => { + return []; + }); + return promise; + } + return this.onReady(() => this.indexedDbService.get().getAll(this.tablename)) as Promise; } public delete(server: Server) { + if (this.isIncognitoMode) { + let promise = new Promise(resolve => { + return []; + }); + return promise; + } + return this.onReady(() => this.indexedDbService.get().delete(this.tablename, server.id)); } @@ -68,6 +114,13 @@ export class ServerService { } public getLocalServer(host: string, port: number) { + if (this.isIncognitoMode) { + let promise = new Promise(resolve => { + return []; + }); + return promise; + } + const promise = new Promise((resolve, reject) => { this.findAll().then((servers: Server[]) => { const local = servers.find(server => server.location === 'bundled'); From 5f52f25da4044be0e62381e881492536b9d8394a Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Fri, 12 Jun 2020 14:28:53 +0200 Subject: [PATCH 2/4] Update server.service.ts --- src/app/services/server.service.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/app/services/server.service.ts b/src/app/services/server.service.ts index 8bdae05d..55e7de22 100644 --- a/src/app/services/server.service.ts +++ b/src/app/services/server.service.ts @@ -42,6 +42,8 @@ export class ServerService { if (this.isIncognitoMode) { server.id = this.serverIdsInIncognitoMode.length + 1; localStorage.setItem(`server-${server.id}`, JSON.stringify(server)); + this.serverIdsInIncognitoMode.push(`server-${server.id}`); + let promise = new Promise(resolve => { return server; }); @@ -64,6 +66,9 @@ export class ServerService { public update(server: Server) { if (this.isIncognitoMode) { + localStorage.removeItem(`server-${server.id}`); + localStorage.setItem(`server-${server.id}`, JSON.stringify(server)); + let promise = new Promise(resolve => { return []; }); @@ -86,7 +91,13 @@ export class ServerService { public findAll() { if (this.isIncognitoMode) { let promise = new Promise(resolve => { - return []; + let servers: Server[] = []; + this.serverIdsInIncognitoMode.forEach(n => { + let server: Server = JSON.parse(localStorage.getItem(n)); + servers.push(server); + }); + + return servers; }); return promise; } @@ -96,8 +107,11 @@ export class ServerService { public delete(server: Server) { if (this.isIncognitoMode) { + localStorage.removeItem(`server-${server.id}`); + this.serverIdsInIncognitoMode = this.serverIdsInIncognitoMode.filter(n => n !== `server-${server.id}`); + let promise = new Promise(resolve => { - return []; + return server.id; }); return promise; } @@ -114,13 +128,6 @@ export class ServerService { } public getLocalServer(host: string, port: number) { - if (this.isIncognitoMode) { - let promise = new Promise(resolve => { - return []; - }); - return promise; - } - const promise = new Promise((resolve, reject) => { this.findAll().then((servers: Server[]) => { const local = servers.find(server => server.location === 'bundled'); From 53b4e064ee9c4a414e48c84c2e0af5609a76659a Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Fri, 12 Jun 2020 15:44:04 +0200 Subject: [PATCH 3/4] Updating server service --- .../direct-link/direct-link.component.ts | 36 ++++++++++-------- .../add-server-dialog.component.ts | 8 ++++ .../server-discovery.component.ts | 6 ++- .../components/servers/servers.component.ts | 38 ++++++++++--------- src/app/services/server.service.ts | 37 +++++++++++------- 5 files changed, 78 insertions(+), 47 deletions(-) diff --git a/src/app/components/direct-link/direct-link.component.ts b/src/app/components/direct-link/direct-link.component.ts index 66277db4..d76496bb 100644 --- a/src/app/components/direct-link/direct-link.component.ts +++ b/src/app/components/direct-link/direct-link.component.ts @@ -24,22 +24,26 @@ export class DirectLinkComponent implements OnInit { const serverIp = this.route.snapshot.paramMap.get('server_ip'); const serverPort = +this.route.snapshot.paramMap.get('server_port'); const projectId = this.route.snapshot.paramMap.get('project_id'); + + this.serverService.serviceInitialized.subscribe(async (value: boolean) => { + if (value) { + const servers = await this.serverService.findAll(); + const server = servers.filter(server => server.host === serverIp && server.port === serverPort)[0]; - const servers = await this.serverService.findAll(); - const server = servers.filter(server => server.host === serverIp && server.port === serverPort)[0]; - - if (server) { - this.router.navigate(['/server', server.id, 'project', projectId]); - } else { - let serverToAdd: Server = new Server(); - serverToAdd.host = serverIp; - serverToAdd.port = serverPort; - serverToAdd.location = 'bundled'; - serverToAdd.name = serverIp; - - this.serverService.create(serverToAdd).then((addedServer: Server) => { - this.router.navigate(['/server', addedServer.id, 'project', projectId]); - }); - } + if (server) { + this.router.navigate(['/server', server.id, 'project', projectId]); + } else { + let serverToAdd: Server = new Server(); + serverToAdd.host = serverIp; + serverToAdd.port = serverPort; + serverToAdd.location = 'bundled'; + serverToAdd.name = serverIp; + + this.serverService.create(serverToAdd).then((addedServer: Server) => { + this.router.navigate(['/server', addedServer.id, 'project', projectId]); + }); + } + } + }); } } diff --git a/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts b/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts index 5519c3da..7408a2e5 100644 --- a/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts +++ b/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts @@ -48,6 +48,8 @@ export class AddServerDialogComponent implements OnInit { async getDefaultLocation() { + console.log('get default location'); + const localServers = await this.numberOfLocalServers(); if(this.electronService.isElectronApp && localServers === 0) { return 'local'; @@ -56,7 +58,10 @@ export class AddServerDialogComponent implements OnInit { } async numberOfLocalServers() { + console.log('calling find all'); const servers = await this.serverService.findAll(); + console.log('servers from add server dialog....'); + return servers.filter((server) => server.location === 'local').length; } @@ -83,7 +88,9 @@ export class AddServerDialogComponent implements OnInit { } async ngOnInit() { + console.log('Start.... '); this.locations = await this.getLocations(); + console.log('Locations: ', this.locations); const defaultLocalServerPath = await this.getDefaultLocalServerPath(); const defaultUbridgePath = await this.getDefaultUbridgePath(); @@ -134,6 +141,7 @@ export class AddServerDialogComponent implements OnInit { }) }); + console.log('????'); const defaultLocation = await this.getDefaultLocation(); this.serverForm.get('location').setValue(defaultLocation); this.serverForm.get('host').setValue(this.getDefaultHost()); diff --git a/src/app/components/servers/server-discovery/server-discovery.component.ts b/src/app/components/servers/server-discovery/server-discovery.component.ts index da682fca..3dec2dd6 100644 --- a/src/app/components/servers/server-discovery/server-discovery.component.ts +++ b/src/app/components/servers/server-discovery/server-discovery.component.ts @@ -32,7 +32,11 @@ export class ServerDiscoveryComponent implements OnInit { ) {} ngOnInit() { - this.discoverFirstAvailableServer(); + this.serverService.serviceInitialized.subscribe(async (value: boolean) => { + if (value) { + this.discoverFirstAvailableServer(); + } + }); } discoverFirstAvailableServer() { diff --git a/src/app/components/servers/servers.component.ts b/src/app/components/servers/servers.component.ts index 29372b95..de6fc3db 100644 --- a/src/app/components/servers/servers.component.ts +++ b/src/app/components/servers/servers.component.ts @@ -40,24 +40,28 @@ export class ServersComponent implements OnInit, OnDestroy { this.isElectronApp = this.electronService.isElectronApp; const runningServersNames = this.serverManagement.getRunningServers(); - this.serverService.findAll().then((servers: Server[]) => { - servers.forEach((server) => { - const serverIndex = runningServersNames.findIndex((serverName) => server.name === serverName); - if(serverIndex >= 0) { - server.status = 'running'; - } - }); - - servers.forEach((server) => { - this.serverService.checkServerVersion(server).subscribe( - (serverInfo) => { - if ((serverInfo.version.split('.')[1]>=2) && (serverInfo.version.split('.')[0]>=2)) { - if (!this.serverDatabase.find(server.name)) this.serverDatabase.addServer(server); + this.serverService.serviceInitialized.subscribe(async (value: boolean) => { + if (value) { + this.serverService.findAll().then((servers: Server[]) => { + servers.forEach((server) => { + const serverIndex = runningServersNames.findIndex((serverName) => server.name === serverName); + if(serverIndex >= 0) { + server.status = 'running'; } - }, - error => {} - ); - }); + }); + + servers.forEach((server) => { + this.serverService.checkServerVersion(server).subscribe( + (serverInfo) => { + if ((serverInfo.version.split('.')[1]>=2) && (serverInfo.version.split('.')[0]>=2)) { + if (!this.serverDatabase.find(server.name)) this.serverDatabase.addServer(server); + } + }, + error => {} + ); + }); + }); + } }); this.dataSource = new ServerDataSource(this.serverDatabase); diff --git a/src/app/services/server.service.ts b/src/app/services/server.service.ts index 55e7de22..1838a312 100644 --- a/src/app/services/server.service.ts +++ b/src/app/services/server.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, EventEmitter } from '@angular/core'; import { IndexedDbService } from './indexed-db.service'; import { Server } from '../models/server'; import { Observable } from 'rxjs'; @@ -11,30 +11,35 @@ export class ServerService { private ready: Promise; private isIncognitoMode: boolean = false; private serverIdsInIncognitoMode: string[] = []; + public serviceInitialized: EventEmitter = new EventEmitter(); constructor( private indexedDbService: IndexedDbService, private httpServer: HttpServer, private toasterService: ToasterService ) { - this.ready = indexedDbService.get().openDatabase(1, evt => { - evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true }); - }); - - this.ready.then(() => {}).catch(() => { + this.indexedDbService.get().openDatabase(1).then(() => { + this.ready = indexedDbService.get().openDatabase(1, evt => { + evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true }); + }); + }).catch(() => { + console.log('in incognito mode'); this.isIncognitoMode = true; - }) + }).finally(() => { + this.serviceInitialized.emit(true); + }); } public get(id: number): Promise { if (this.isIncognitoMode) { let server: Server = JSON.parse(localStorage.getItem(`server-${id}`)); let promise = new Promise(resolve => { - return server; + resolve(server); }); return promise; } + console.log('pfffff get'); return this.onReady(() => this.indexedDbService.get().getByKey(this.tablename, id)) as Promise; } @@ -45,11 +50,12 @@ export class ServerService { this.serverIdsInIncognitoMode.push(`server-${server.id}`); let promise = new Promise(resolve => { - return server; + resolve(server); }); return promise; } + console.log('pfffff create'); return this.onReady(() => { const promise = new Promise((resolve, reject) => { this.indexedDbService @@ -70,11 +76,12 @@ export class ServerService { localStorage.setItem(`server-${server.id}`, JSON.stringify(server)); let promise = new Promise(resolve => { - return []; + resolve(server); }); return promise; } + console.log('pfffff update'); return this.onReady(() => { const promise = new Promise((resolve, reject) => { this.indexedDbService @@ -96,12 +103,13 @@ export class ServerService { let server: Server = JSON.parse(localStorage.getItem(n)); servers.push(server); }); - - return servers; + console.log('pfffff find all iin'); + resolve(servers); }); return promise; } + console.log('pfffff find all'); return this.onReady(() => this.indexedDbService.get().getAll(this.tablename)) as Promise; } @@ -111,11 +119,12 @@ export class ServerService { this.serverIdsInIncognitoMode = this.serverIdsInIncognitoMode.filter(n => n !== `server-${server.id}`); let promise = new Promise(resolve => { - return server.id; + resolve(server.id); }); return promise; } + console.log('pfffff delete'); return this.onReady(() => this.indexedDbService.get().delete(this.tablename, server.id)); } @@ -154,6 +163,8 @@ export class ServerService { } protected onReady(query) { + console.log('pfffff'); + const promise = new Promise((resolve, reject) => { this.ready.then( () => { From 7248a0f183783db145463910868bd34694e9230c Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Fri, 12 Jun 2020 16:08:12 +0200 Subject: [PATCH 4/4] Code cleaned up --- .../add-server-dialog/add-server-dialog.component.ts | 7 ------- src/app/services/server.service.ts | 9 --------- 2 files changed, 16 deletions(-) diff --git a/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts b/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts index 7408a2e5..12a354bb 100644 --- a/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts +++ b/src/app/components/servers/add-server-dialog/add-server-dialog.component.ts @@ -48,8 +48,6 @@ export class AddServerDialogComponent implements OnInit { async getDefaultLocation() { - console.log('get default location'); - const localServers = await this.numberOfLocalServers(); if(this.electronService.isElectronApp && localServers === 0) { return 'local'; @@ -58,9 +56,7 @@ export class AddServerDialogComponent implements OnInit { } async numberOfLocalServers() { - console.log('calling find all'); const servers = await this.serverService.findAll(); - console.log('servers from add server dialog....'); return servers.filter((server) => server.location === 'local').length; } @@ -88,9 +84,7 @@ export class AddServerDialogComponent implements OnInit { } async ngOnInit() { - console.log('Start.... '); this.locations = await this.getLocations(); - console.log('Locations: ', this.locations); const defaultLocalServerPath = await this.getDefaultLocalServerPath(); const defaultUbridgePath = await this.getDefaultUbridgePath(); @@ -141,7 +135,6 @@ export class AddServerDialogComponent implements OnInit { }) }); - console.log('????'); const defaultLocation = await this.getDefaultLocation(); this.serverForm.get('location').setValue(defaultLocation); this.serverForm.get('host').setValue(this.getDefaultHost()); diff --git a/src/app/services/server.service.ts b/src/app/services/server.service.ts index 1838a312..5dfedebf 100644 --- a/src/app/services/server.service.ts +++ b/src/app/services/server.service.ts @@ -23,7 +23,6 @@ export class ServerService { evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true }); }); }).catch(() => { - console.log('in incognito mode'); this.isIncognitoMode = true; }).finally(() => { this.serviceInitialized.emit(true); @@ -39,7 +38,6 @@ export class ServerService { return promise; } - console.log('pfffff get'); return this.onReady(() => this.indexedDbService.get().getByKey(this.tablename, id)) as Promise; } @@ -55,7 +53,6 @@ export class ServerService { return promise; } - console.log('pfffff create'); return this.onReady(() => { const promise = new Promise((resolve, reject) => { this.indexedDbService @@ -81,7 +78,6 @@ export class ServerService { return promise; } - console.log('pfffff update'); return this.onReady(() => { const promise = new Promise((resolve, reject) => { this.indexedDbService @@ -103,13 +99,11 @@ export class ServerService { let server: Server = JSON.parse(localStorage.getItem(n)); servers.push(server); }); - console.log('pfffff find all iin'); resolve(servers); }); return promise; } - console.log('pfffff find all'); return this.onReady(() => this.indexedDbService.get().getAll(this.tablename)) as Promise; } @@ -124,7 +118,6 @@ export class ServerService { return promise; } - console.log('pfffff delete'); return this.onReady(() => this.indexedDbService.get().delete(this.tablename, server.id)); } @@ -163,8 +156,6 @@ export class ServerService { } protected onReady(query) { - console.log('pfffff'); - const promise = new Promise((resolve, reject) => { this.ready.then( () => {