mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
events work in electron IPC
This commit is contained in:
parent
6dcbb5e308
commit
185cfab5d8
@ -90,7 +90,7 @@
|
||||
},
|
||||
"homepage": "./",
|
||||
"scripts": {
|
||||
"start": "cross-env ELECTRON_START_URL=http://localhost:5000 ELECTRON_DEBUG=1 DEVMODE=1 electron .",
|
||||
"start": "cross-env ELECTRON_START_URL=http://localhost:5000 DEVMODE=1 electron .",
|
||||
"start:local": "cross-env electron .",
|
||||
"dist": "electron-builder",
|
||||
"build": "cd ../packages/api && yarn build && cd ../web && yarn build && cd ../../app && yarn dist",
|
||||
|
@ -221,11 +221,14 @@ function createWindow() {
|
||||
}
|
||||
}
|
||||
|
||||
global.API_PACKAGE = process.env.DEVMODE ? '../packages/api/src/index' : '../packages/api/dist/bundle.js';
|
||||
const api = require(path.join(
|
||||
__dirname,
|
||||
process.env.ELECTRON_DEBUG ? '../../packages/api' : '../packages/api/dist/bundle.js'
|
||||
process.env.DEVMODE ? '../../packages/api/src/index' : '../packages/api/dist/bundle.js'
|
||||
));
|
||||
api.getMainModule().useAllControllers(null, electron);
|
||||
const main = api.getMainModule();
|
||||
main.initializeElectronSender(mainWindow.webContents);
|
||||
main.useAllControllers(null, electron);
|
||||
|
||||
loadMainWindow();
|
||||
|
||||
@ -267,7 +270,9 @@ function createWindow() {
|
||||
}
|
||||
|
||||
function onAppReady() {
|
||||
if (!process.env.DEVMODE) {
|
||||
autoUpdater.checkForUpdatesAndNotify();
|
||||
}
|
||||
createWindow();
|
||||
}
|
||||
|
||||
|
@ -136,7 +136,11 @@ module.exports = {
|
||||
raw: true,
|
||||
},
|
||||
test(req, res) {
|
||||
const subprocess = fork(process.argv[1], ['--start-process', 'connectProcess', ...process.argv.slice(3)]);
|
||||
const subprocess = fork(global['API_PACKAGE'] || process.argv[1], [
|
||||
'--start-process',
|
||||
'connectProcess',
|
||||
...process.argv.slice(3),
|
||||
]);
|
||||
subprocess.on('message', resp => {
|
||||
if (handleProcessCommunication(resp, subprocess)) return;
|
||||
// @ts-ignore
|
||||
|
@ -74,7 +74,7 @@ module.exports = {
|
||||
const existing = this.opened.find(x => x.conid == conid && x.database == database);
|
||||
if (existing) return existing;
|
||||
const connection = await connections.get({ conid });
|
||||
const subprocess = fork(process.argv[1], [
|
||||
const subprocess = fork(global['API_PACKAGE'] || process.argv[1], [
|
||||
'--start-process',
|
||||
'databaseConnectionProcess',
|
||||
...process.argv.slice(3),
|
||||
|
@ -103,7 +103,7 @@ module.exports = {
|
||||
stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
|
||||
env: {
|
||||
...process.env,
|
||||
DBGATE_API: global['dbgateApiModulePath'] || process.argv[1],
|
||||
DBGATE_API: global['API_PACKAGE'] || global['dbgateApiModulePath'] || process.argv[1],
|
||||
..._.fromPairs(pluginNames.map(name => [`PLUGIN_${_.camelCase(name)}`, getPluginBackendPath(name)])),
|
||||
},
|
||||
});
|
||||
|
@ -37,7 +37,7 @@ module.exports = {
|
||||
const existing = this.opened.find(x => x.conid == conid);
|
||||
if (existing) return existing;
|
||||
const connection = await connections.get({ conid });
|
||||
const subprocess = fork(process.argv[1], [
|
||||
const subprocess = fork(global['API_PACKAGE'] || process.argv[1], [
|
||||
'--start-process',
|
||||
'serverConnectionProcess',
|
||||
...process.argv.slice(3),
|
||||
|
@ -65,7 +65,11 @@ module.exports = {
|
||||
async create({ conid, database }) {
|
||||
const sesid = uuidv1();
|
||||
const connection = await connections.get({ conid });
|
||||
const subprocess = fork(process.argv[1], ['--start-process', 'sessionProcess', ...process.argv.slice(3)]);
|
||||
const subprocess = fork(global['API_PACKAGE'] || process.argv[1], [
|
||||
'--start-process',
|
||||
'sessionProcess',
|
||||
...process.argv.slice(3),
|
||||
]);
|
||||
const newOpened = {
|
||||
conid,
|
||||
database,
|
||||
|
@ -90,7 +90,7 @@ function start() {
|
||||
|
||||
// Tell the client to retry every 10 seconds if connectivity is lost
|
||||
res.write('retry: 10000\n\n');
|
||||
socket.set(res);
|
||||
socket.setSseResponse(res);
|
||||
});
|
||||
|
||||
app.use(bodyParser.json({ limit: '50mb' }));
|
||||
@ -162,4 +162,8 @@ function useAllControllers(app, electron) {
|
||||
useController(app, electron, '/query-history', queryHistory);
|
||||
}
|
||||
|
||||
module.exports = { start, useAllControllers };
|
||||
function initializeElectronSender(electronSender) {
|
||||
socket.setElectronSender(electronSender);
|
||||
}
|
||||
|
||||
module.exports = { start, useAllControllers, initializeElectronSender };
|
||||
|
@ -29,7 +29,11 @@ class DatastoreProxy {
|
||||
|
||||
async ensureSubprocess() {
|
||||
if (!this.subprocess) {
|
||||
this.subprocess = fork(process.argv[1], ['--start-process', 'jslDatastoreProcess', ...process.argv.slice(3)]);
|
||||
this.subprocess = fork(global['API_PACKAGE'] || process.argv[1], [
|
||||
'--start-process',
|
||||
'jslDatastoreProcess',
|
||||
...process.argv.slice(3),
|
||||
]);
|
||||
|
||||
this.subprocess.on('message', message => {
|
||||
// @ts-ignore
|
||||
|
@ -1,35 +1,29 @@
|
||||
let res = null;
|
||||
let sseResponse = null;
|
||||
let electronSender = null;
|
||||
let init = '';
|
||||
|
||||
module.exports = {
|
||||
set(value) {
|
||||
res = value;
|
||||
setSseResponse(value) {
|
||||
sseResponse = value;
|
||||
},
|
||||
setElectronSender(value) {
|
||||
electronSender = value;
|
||||
},
|
||||
// get() {
|
||||
// return socket;
|
||||
// },
|
||||
emit(message, data) {
|
||||
if (res) {
|
||||
if (electronSender) {
|
||||
electronSender.send(message, data == null ? null : data);
|
||||
} else if (sseResponse) {
|
||||
if (init) {
|
||||
res.write(init);
|
||||
sseResponse.write(init);
|
||||
init = '';
|
||||
}
|
||||
res.write(`event: ${message}\ndata: ${JSON.stringify(data == null ? null : data)}\n\n`);
|
||||
sseResponse.write(`event: ${message}\ndata: ${JSON.stringify(data == null ? null : data)}\n\n`);
|
||||
} else {
|
||||
init += res;
|
||||
init += sseResponse;
|
||||
}
|
||||
|
||||
// console.log('EMIT:', message, data);
|
||||
// socket.emit(message, data);
|
||||
},
|
||||
emitChanged(key) {
|
||||
this.emit('clean-cache', key);
|
||||
this.emit(key);
|
||||
// console.log('EMIT_CHANGED:', key);
|
||||
// socket.emit('clean-cache', key);
|
||||
// socket.emit(key);
|
||||
|
||||
// socket.send(key, 'clean-cache');
|
||||
// socket.send(null, key);
|
||||
},
|
||||
};
|
||||
|
@ -28,7 +28,7 @@ module.exports = function useController(app, electron, route, controller) {
|
||||
if (electron) {
|
||||
if (meta === true) {
|
||||
const handler = `${route.substring(1)}-${_.kebabCase(key)}`;
|
||||
console.log('REGISTERING HANDLER', handler);
|
||||
// console.log('REGISTERING HANDLER', handler);
|
||||
electron.ipcMain.handle(handler, async (event, args) => {
|
||||
const data = await controller[key](args);
|
||||
return data;
|
||||
|
@ -5,11 +5,12 @@ import getElectron from './getElectron';
|
||||
// import socket from './socket';
|
||||
|
||||
let eventSource;
|
||||
let cacheCleanerRegistered;
|
||||
|
||||
function wantEventSource() {
|
||||
if (!eventSource) {
|
||||
eventSource = new EventSource(`${resolveApi()}/stream`);
|
||||
eventSource.addEventListener('clean-cache', e => cacheClean(JSON.parse(e.data)));
|
||||
// eventSource.addEventListener('clean-cache', e => cacheClean(JSON.parse(e.data)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,7 +38,14 @@ const apiHandlers = new WeakMap();
|
||||
export function apiOn(event: string, handler: Function) {
|
||||
const electron = getElectron();
|
||||
if (electron) {
|
||||
electron.addEventListener(event, handler);
|
||||
if (!apiHandlers.has(handler)) {
|
||||
const handlerProxy = (e, data) => {
|
||||
handler(data);
|
||||
};
|
||||
apiHandlers.set(handler, handlerProxy);
|
||||
}
|
||||
|
||||
electron.addEventListener(event, apiHandlers.get(handler));
|
||||
} else {
|
||||
wantEventSource();
|
||||
if (!apiHandlers.has(handler)) {
|
||||
@ -50,15 +58,20 @@ export function apiOn(event: string, handler: Function) {
|
||||
|
||||
eventSource.addEventListener(event, apiHandlers.get(handler));
|
||||
}
|
||||
|
||||
if (!cacheCleanerRegistered) {
|
||||
cacheCleanerRegistered = true;
|
||||
apiOn('clean-cache', reloadTrigger => cacheClean(reloadTrigger));
|
||||
}
|
||||
}
|
||||
|
||||
export function apiOff(event: string, handler: Function) {
|
||||
const electron = getElectron();
|
||||
if (apiHandlers.has(handler)) {
|
||||
if (electron) {
|
||||
electron.removeEventListener(event, handler);
|
||||
electron.removeEventListener(event, apiHandlers.get(handler));
|
||||
} else {
|
||||
wantEventSource();
|
||||
if (apiHandlers.has(handler)) {
|
||||
eventSource.removeEventListener(event, apiHandlers.get(handler));
|
||||
}
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ class ElectronApi {
|
||||
}
|
||||
|
||||
removeEventListener(channel: string, listener: Function) {
|
||||
this.ipcRenderer.removeEventListener(channel, listener);
|
||||
this.ipcRenderer.removeListener(channel, listener);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user