mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-20 13:36:02 +00:00
api running in electron main process
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import resolveApi, { resolveApiHeaders } from './resolveApi';
|
||||
import { writable } from 'svelte/store';
|
||||
import { cacheClean } from './cache';
|
||||
import getElectron from './getElectron';
|
||||
// import socket from './socket';
|
||||
|
||||
let eventSource;
|
||||
@@ -13,37 +14,53 @@ function wantEventSource() {
|
||||
}
|
||||
|
||||
export async function apiCall(route: string, args: {} = undefined) {
|
||||
const resp = await fetch(`${resolveApi()}/${route}`, {
|
||||
method: 'POST',
|
||||
cache: 'no-cache',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...resolveApiHeaders(),
|
||||
},
|
||||
body: JSON.stringify(args),
|
||||
});
|
||||
return resp.json();
|
||||
const electron = getElectron();
|
||||
if (electron) {
|
||||
const resp = await electron.invoke(route.replace('/', '-'), args);
|
||||
return resp;
|
||||
} else {
|
||||
const resp = await fetch(`${resolveApi()}/${route}`, {
|
||||
method: 'POST',
|
||||
cache: 'no-cache',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...resolveApiHeaders(),
|
||||
},
|
||||
body: JSON.stringify(args),
|
||||
});
|
||||
return resp.json();
|
||||
}
|
||||
}
|
||||
|
||||
const apiHandlers = new WeakMap();
|
||||
|
||||
export function apiOn(event: string, handler: Function) {
|
||||
wantEventSource();
|
||||
if (!apiHandlers.has(handler)) {
|
||||
const handlerProxy = e => {
|
||||
// console.log('RECEIVED', e.type, JSON.parse(e.data));
|
||||
handler(JSON.parse(e.data));
|
||||
};
|
||||
apiHandlers.set(handler, handlerProxy);
|
||||
}
|
||||
const electron = getElectron();
|
||||
if (electron) {
|
||||
electron.addEventListener(event, handler);
|
||||
} else {
|
||||
wantEventSource();
|
||||
if (!apiHandlers.has(handler)) {
|
||||
const handlerProxy = e => {
|
||||
// console.log('RECEIVED', e.type, JSON.parse(e.data));
|
||||
handler(JSON.parse(e.data));
|
||||
};
|
||||
apiHandlers.set(handler, handlerProxy);
|
||||
}
|
||||
|
||||
eventSource.addEventListener(event, apiHandlers.get(handler));
|
||||
eventSource.addEventListener(event, apiHandlers.get(handler));
|
||||
}
|
||||
}
|
||||
|
||||
export function apiOff(event: string, handler: Function) {
|
||||
wantEventSource();
|
||||
if (apiHandlers.has(handler)) {
|
||||
eventSource.removeEventListener(event, apiHandlers.get(handler));
|
||||
const electron = getElectron();
|
||||
if (electron) {
|
||||
electron.removeEventListener(event, handler);
|
||||
} else {
|
||||
wantEventSource();
|
||||
if (apiHandlers.has(handler)) {
|
||||
eventSource.removeEventListener(event, apiHandlers.get(handler));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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