create sevice to manage deep links

This commit is contained in:
MathieuG-P
2022-12-12 23:35:57 +01:00
parent 0372f7a1d5
commit ca5c86dbf1
4 changed files with 96 additions and 1 deletions
+11
View File
@@ -14,6 +14,7 @@ import log from 'electron-log';
import './ipcs';
import { UtilsService } from './services/utils.service';
import { WindowManagerService } from './services/window-manager.service';
import { DeepLinkService } from './services/deep-link.service';
export const PRELOAD_PATH = app.isPackaged ? path.join(__dirname, 'preload.js') : path.join(__dirname, '../../.erb/dll/preload.js')
@@ -60,6 +61,16 @@ app.on('window-all-closed', () => {
}
});
app.on('open-url', (event, url) => {
DeepLinkService.getInstance().dispatchLinkOpened(url);
});
const gotTheLock = app.requestSingleInstanceLock();
if(!gotTheLock){
app.quit();
}
app.whenReady().then(() => {
createWindow();
}).catch(log.error);
+80
View File
@@ -0,0 +1,80 @@
import { app } from "electron";
import path from "path";
import { URL } from "url";
export class DeepLinkService {
// See docs : https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app#main-process-mainjs
private static instance: DeepLinkService;
public static getInstance(): DeepLinkService{
if(!DeepLinkService.instance){ DeepLinkService.instance = new DeepLinkService(); }
return DeepLinkService.instance;
}
private readonly listeners = new Map<string, Listerner[]>()
public registerDeepLink(protocol: string): boolean{
if(process.defaultApp && process.argv.length >= 2){
return app.setAsDefaultProtocolClient(protocol, process.execPath, [path.resolve(process.argv[1])]);
}
return app.setAsDefaultProtocolClient(protocol);
}
public unRegisterDeepLink(protocol: string): boolean{
if(process.defaultApp && process.argv.length >= 2) {
return app.removeAsDefaultProtocolClient(protocol, process.execPath, [path.resolve(process.argv[1])]);
}
app.removeAsDefaultProtocolClient(protocol);
}
public isDeepLinkRegistred(protocol: string): boolean{
if(process.defaultApp && process.argv.length >= 2) {
return app.isDefaultProtocolClient(protocol, process.execPath, [path.resolve(process.argv[1])]);
}
return app.isDefaultProtocolClient(protocol);
}
public addLinkOpenedListener(protocol: string, fn: Listerner){
if(!this.listeners.has(protocol)){
this.listeners.set(protocol, [] as Listerner[]);
}
this.listeners.get(protocol).push(fn);
}
public removeLinkOpenedListener(protocol: string, fn: Listerner){
if(!this.listeners.get(protocol)?.length){ return; }
const listeners = this.listeners.get(protocol);
const fnIndex = listeners.findIndex(listener => listener === fn);
if(fnIndex < 0){ return; }
listeners.splice(fnIndex, 1);
}
public dispatchLinkOpened(link: string){
const url = new URL(link);
const protocolListeners = this.listeners.get(url.protocol.replace(":", "")) ?? [];
protocolListeners.forEach(listerner => {
listerner(link);
});
}
}
type Listerner = (link: string) => void;
@@ -233,6 +233,10 @@ export class LocalMapsManagerService {
}
public async enableDeepLink(){
// TODO
}
}
@@ -160,7 +160,7 @@ export class MapsManagerService {
}
public async enableDeepLink(): Promise<boolean>{
return true;
return true; // TODO
}
public get versionLinked$(): Observable<BSVersion>{