mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 17:46:00 +00:00
api running in electron main process
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user