Fix for Chrome browser in private mode

This commit is contained in:
piotrpekala7 2020-06-16 19:16:18 +02:00
parent 41f5af2b90
commit edb7aefba2
5 changed files with 99 additions and 63 deletions

View File

@ -21,29 +21,35 @@ export class DirectLinkComponent implements OnInit {
) {}
async ngOnInit() {
if (this.serverService.isServiceInitialized) this.getServers();
this.serverService.serviceInitialized.subscribe(async (value: boolean) => {
if (value) {
this.getServers();
}
});
}
private async getServers() {
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];
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]);
});
}
}
});
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]);
});
}
}
}

View File

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

View File

@ -35,32 +35,39 @@ export class ServersComponent implements OnInit, OnDestroy {
private childProcessService: ChildProcessService,
private bottomSheet: MatBottomSheet,
) {}
getServers() {
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);
}
},
error => {}
);
});
});
}
ngOnInit() {
this.isElectronApp = this.electronService.isElectronApp;
const runningServersNames = this.serverManagement.getRunningServers();
if (this.serverService.isServiceInitialized) this.getServers();
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';
}
});
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.getServers();
}
});

View File

@ -17,23 +17,28 @@ export class ProjectMapGuard implements CanActivate {
private router: Router
) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
const server_id = route.paramMap.get("server_id");
const project_id = route.paramMap.get("project_id");
return from(this.serverService.get(parseInt(server_id, 10))).pipe(
switchMap(response => {
if (!response) this.router.navigate(['/servers']);
return this.projectService.list(response as Server)
}),
map(response => {
let projectToOpen = response.find(n => n.project_id === project_id);
if (projectToOpen) return true;
// return from(this.serverService.tryToCreateDb()).pipe(
// switchMap(response => {
// return this.serverService.get(parseInt(server_id, 10));
// }),
// switchMap(response => {
// if (!response) this.router.navigate(['/servers']);
// return this.projectService.list(response as Server)
// }),
// map(response => {
// let projectToOpen = response.find(n => n.project_id === project_id);
// if (projectToOpen) return true;
this.toasterService.error('Project could not be opened');
this.projectService.projectListUpdated();
return false;
})
)
// this.toasterService.error('Project could not be opened');
// this.projectService.projectListUpdated();
// return false;
// })
// )
return true;
}
}

View File

@ -1,9 +1,8 @@
import { Injectable, EventEmitter } from '@angular/core';
import { IndexedDbService } from './indexed-db.service';
import { Server } from '../models/server';
import { Observable } from 'rxjs';
import { Observable, Subject } from 'rxjs';
import { HttpServer } from './http-server.service';
import { ToasterService } from './toaster.service';
@Injectable()
export class ServerService {
@ -11,24 +10,41 @@ export class ServerService {
private ready: Promise<any>;
private isIncognitoMode: boolean = false;
private serverIdsInIncognitoMode: string[] = [];
public serviceInitialized: EventEmitter<boolean> = new EventEmitter<boolean>();
public serviceInitialized: Subject<boolean> = new Subject<boolean>();
public isServiceInitialized: boolean;
constructor(
private indexedDbService: IndexedDbService,
private httpServer: HttpServer,
private toasterService: ToasterService
private httpServer: HttpServer
) {
this.indexedDbService.get().openDatabase(1).then(() => {
this.ready = indexedDbService.get().openDatabase(1, evt => {
evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true });
this.ready = this.indexedDbService.get().openDatabase(1, evt => {
evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true });
}).then(() => {
this.indexedDbService.get().getAll(this.tablename)
.then(() => {})
.catch(() => {
this.isIncognitoMode = true;
});
}).catch(() => {
this.isIncognitoMode = true;
}).finally(() => {
this.serviceInitialized.emit(true);
this.isServiceInitialized = true;
this.serviceInitialized.next(true);
});
}
public tryToCreateDb() {
let promise = new Promise(resolve => {
this.indexedDbService.get().openDatabase(1, evt => {
evt.currentTarget.result.createObjectStore(this.tablename, { keyPath: 'id', autoIncrement: true });
}).then(() => {
}).catch(() => {
this.isIncognitoMode = true;
});
});
return promise;
}
public get(id: number): Promise<Server> {
if (this.isIncognitoMode) {
let server: Server = JSON.parse(localStorage.getItem(`server-${id}`));