api running in electron main process

This commit is contained in:
Jan Prochazka
2021-12-25 09:23:03 +01:00
parent 2ff9e8c452
commit 24071ebde7
11 changed files with 173 additions and 104 deletions

View File

@@ -1,11 +1,11 @@
class ElectronApi {
public port?: number;
public authorization?: string;
// public port?: number;
// public authorization?: string;
private ipcRenderer = getIpcRenderer();
constructor(args) {
this.port = args.port;
this.authorization = args.authorization;
constructor() {
// this.port = args.port;
// this.authorization = args.authorization;
}
send(msg, args = null) {
@@ -30,18 +30,29 @@ class ElectronApi {
async openExternal(url) {
await this.ipcRenderer.invoke('openExternal', url);
}
}
let apiInstance = null;
async invoke(route, args) {
const res = await this.ipcRenderer.invoke(route, args);
return res;
}
function initializeElectron(args) {
apiInstance = new ElectronApi(args);
if (window['dbgate_recreateSocket']) {
window['dbgate_recreateSocket']();
addEventListener(channel: string, listener: Function) {
this.ipcRenderer.on(channel, listener);
}
removeEventListener(channel: string, listener: Function) {
this.ipcRenderer.removeEventListener(channel, listener);
}
}
window['dbgate_initializeElectron'] = initializeElectron;
// function initializeElectron(args) {
// apiInstance = new ElectronApi(args);
// if (window['dbgate_recreateSocket']) {
// window['dbgate_recreateSocket']();
// }
// }
// window['dbgate_initializeElectron'] = initializeElectron;
function getIpcRenderer() {
if (window['require']) {
@@ -51,14 +62,16 @@ function getIpcRenderer() {
return null;
}
export function shouldWaitForElectronInitialize() {
return !!getIpcRenderer() && !apiInstance;
}
// export function shouldWaitForElectronInitialize() {
// return !!getIpcRenderer() && !apiInstance;
// }
export function isElectronAvailable() {
return !!getIpcRenderer();
}
const apiInstance = isElectronAvailable() ? new ElectronApi() : null;
export default function getElectron(): ElectronApi {
return apiInstance;
// try {