Merge pull request #889 from GNS3/Strange-behavior-with-Firefox

Strange behavior with firefox
This commit is contained in:
piotrpekala7 2020-06-15 12:18:42 +02:00 committed by GitHub
commit 41f5af2b90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 113 additions and 38 deletions

View File

@ -25,6 +25,8 @@ export class DirectLinkComponent implements OnInit {
const serverPort = +this.route.snapshot.paramMap.get('server_port'); const serverPort = +this.route.snapshot.paramMap.get('server_port');
const projectId = this.route.snapshot.paramMap.get('project_id'); 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 servers = await this.serverService.findAll();
const server = servers.filter(server => server.host === serverIp && server.port === serverPort)[0]; const server = servers.filter(server => server.host === serverIp && server.port === serverPort)[0];
@ -42,4 +44,6 @@ export class DirectLinkComponent implements OnInit {
}); });
} }
} }
});
}
} }

View File

@ -57,6 +57,7 @@ export class AddServerDialogComponent implements OnInit {
async numberOfLocalServers() { async numberOfLocalServers() {
const servers = await this.serverService.findAll(); const servers = await this.serverService.findAll();
return servers.filter((server) => server.location === 'local').length; return servers.filter((server) => server.location === 'local').length;
} }

View File

@ -32,8 +32,12 @@ export class ServerDiscoveryComponent implements OnInit {
) {} ) {}
ngOnInit() { ngOnInit() {
this.serverService.serviceInitialized.subscribe(async (value: boolean) => {
if (value) {
this.discoverFirstAvailableServer(); this.discoverFirstAvailableServer();
} }
});
}
discoverFirstAvailableServer() { discoverFirstAvailableServer() {
forkJoin( forkJoin(

View File

@ -40,6 +40,8 @@ export class ServersComponent implements OnInit, OnDestroy {
this.isElectronApp = this.electronService.isElectronApp; this.isElectronApp = this.electronService.isElectronApp;
const runningServersNames = this.serverManagement.getRunningServers(); const runningServersNames = this.serverManagement.getRunningServers();
this.serverService.serviceInitialized.subscribe(async (value: boolean) => {
if (value) {
this.serverService.findAll().then((servers: Server[]) => { this.serverService.findAll().then((servers: Server[]) => {
servers.forEach((server) => { servers.forEach((server) => {
const serverIndex = runningServersNames.findIndex((serverName) => server.name === serverName); const serverIndex = runningServersNames.findIndex((serverName) => server.name === serverName);
@ -59,6 +61,8 @@ export class ServersComponent implements OnInit, OnDestroy {
); );
}); });
}); });
}
});
this.dataSource = new ServerDataSource(this.serverDatabase); this.dataSource = new ServerDataSource(this.serverDatabase);

View File

@ -1,28 +1,58 @@
import { Injectable } from '@angular/core'; import { Injectable, EventEmitter } from '@angular/core';
import { IndexedDbService } from './indexed-db.service'; import { IndexedDbService } from './indexed-db.service';
import { Server } from '../models/server'; import { Server } from '../models/server';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { HttpServer } from './http-server.service'; import { HttpServer } from './http-server.service';
import { ToasterService } from './toaster.service';
@Injectable() @Injectable()
export class ServerService { export class ServerService {
private tablename = 'servers'; private tablename = 'servers';
private ready: Promise<any>; private ready: Promise<any>;
private isIncognitoMode: boolean = false;
private serverIdsInIncognitoMode: string[] = [];
public serviceInitialized: EventEmitter<boolean> = new EventEmitter<boolean>();
constructor( constructor(
private indexedDbService: IndexedDbService, private indexedDbService: IndexedDbService,
private httpServer: HttpServer private httpServer: HttpServer,
private toasterService: ToasterService
) { ) {
this.indexedDbService.get().openDatabase(1).then(() => {
this.ready = indexedDbService.get().openDatabase(1, evt => { this.ready = indexedDbService.get().openDatabase(1, evt => {
evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true }); evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true });
}); });
}).catch(() => {
this.isIncognitoMode = true;
}).finally(() => {
this.serviceInitialized.emit(true);
});
} }
public get(id: number): Promise<Server> { public get(id: number): Promise<Server> {
if (this.isIncognitoMode) {
let server: Server = JSON.parse(localStorage.getItem(`server-${id}`));
let promise = new Promise<Server>(resolve => {
resolve(server);
});
return promise;
}
return this.onReady(() => this.indexedDbService.get().getByKey(this.tablename, id)) as Promise<Server>; return this.onReady(() => this.indexedDbService.get().getByKey(this.tablename, id)) as Promise<Server>;
} }
public create(server: Server) { public create(server: Server) {
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<Server>(resolve => {
resolve(server);
});
return promise;
}
return this.onReady(() => { return this.onReady(() => {
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
this.indexedDbService this.indexedDbService
@ -38,6 +68,16 @@ export class ServerService {
} }
public update(server: Server) { public update(server: Server) {
if (this.isIncognitoMode) {
localStorage.removeItem(`server-${server.id}`);
localStorage.setItem(`server-${server.id}`, JSON.stringify(server));
let promise = new Promise<Server>(resolve => {
resolve(server);
});
return promise;
}
return this.onReady(() => { return this.onReady(() => {
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
this.indexedDbService this.indexedDbService
@ -52,10 +92,32 @@ export class ServerService {
} }
public findAll() { public findAll() {
if (this.isIncognitoMode) {
let promise = new Promise<Server[]>(resolve => {
let servers: Server[] = [];
this.serverIdsInIncognitoMode.forEach(n => {
let server: Server = JSON.parse(localStorage.getItem(n));
servers.push(server);
});
resolve(servers);
});
return promise;
}
return this.onReady(() => this.indexedDbService.get().getAll(this.tablename)) as Promise<Server[]>; return this.onReady(() => this.indexedDbService.get().getAll(this.tablename)) as Promise<Server[]>;
} }
public delete(server: Server) { 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 => {
resolve(server.id);
});
return promise;
}
return this.onReady(() => this.indexedDbService.get().delete(this.tablename, server.id)); return this.onReady(() => this.indexedDbService.get().delete(this.tablename, server.id));
} }