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 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];
@ -42,4 +44,6 @@ export class DirectLinkComponent implements OnInit {
});
}
}
});
}
}

View File

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

View File

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

View File

@ -40,6 +40,8 @@ export class ServersComponent implements OnInit, OnDestroy {
this.isElectronApp = this.electronService.isElectronApp;
const runningServersNames = this.serverManagement.getRunningServers();
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);
@ -59,6 +61,8 @@ export class ServersComponent implements OnInit, OnDestroy {
);
});
});
}
});
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 { 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<any>;
private isIncognitoMode: boolean = false;
private serverIdsInIncognitoMode: string[] = [];
public serviceInitialized: EventEmitter<boolean> = new EventEmitter<boolean>();
constructor(
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 => {
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> {
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>;
}
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(() => {
const promise = new Promise((resolve, reject) => {
this.indexedDbService
@ -38,6 +68,16 @@ 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<Server>(resolve => {
resolve(server);
});
return promise;
}
return this.onReady(() => {
const promise = new Promise((resolve, reject) => {
this.indexedDbService
@ -52,10 +92,32 @@ export class ServerService {
}
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[]>;
}
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));
}