From 185cfab5d8cf67af5e1fa5608971f9e01592b5d0 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sat, 25 Dec 2021 10:52:48 +0100 Subject: [PATCH] events work in electron IPC --- app/package.json | 2 +- app/src/electron.js | 11 +++++-- packages/api/src/controllers/connections.js | 6 +++- .../src/controllers/databaseConnections.js | 2 +- packages/api/src/controllers/runners.js | 2 +- .../api/src/controllers/serverConnections.js | 2 +- packages/api/src/controllers/sessions.js | 6 +++- packages/api/src/main.js | 8 +++-- packages/api/src/utility/DatastoreProxy.js | 6 +++- packages/api/src/utility/socket.js | 32 ++++++++----------- packages/api/src/utility/useController.js | 2 +- packages/web/src/utility/api.ts | 27 ++++++++++++---- packages/web/src/utility/getElectron.ts | 2 +- 13 files changed, 68 insertions(+), 40 deletions(-) diff --git a/app/package.json b/app/package.json index 550b407c..bb56fb55 100644 --- a/app/package.json +++ b/app/package.json @@ -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", diff --git a/app/src/electron.js b/app/src/electron.js index affffaf1..dac1c9a2 100644 --- a/app/src/electron.js +++ b/app/src/electron.js @@ -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() { - autoUpdater.checkForUpdatesAndNotify(); + if (!process.env.DEVMODE) { + autoUpdater.checkForUpdatesAndNotify(); + } createWindow(); } diff --git a/packages/api/src/controllers/connections.js b/packages/api/src/controllers/connections.js index a132032b..3c12816e 100644 --- a/packages/api/src/controllers/connections.js +++ b/packages/api/src/controllers/connections.js @@ -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 diff --git a/packages/api/src/controllers/databaseConnections.js b/packages/api/src/controllers/databaseConnections.js index 06ee9fd2..fc11aa9c 100644 --- a/packages/api/src/controllers/databaseConnections.js +++ b/packages/api/src/controllers/databaseConnections.js @@ -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), diff --git a/packages/api/src/controllers/runners.js b/packages/api/src/controllers/runners.js index b0f123dd..7908f905 100644 --- a/packages/api/src/controllers/runners.js +++ b/packages/api/src/controllers/runners.js @@ -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)])), }, }); diff --git a/packages/api/src/controllers/serverConnections.js b/packages/api/src/controllers/serverConnections.js index 2afa9c41..1dd7527c 100644 --- a/packages/api/src/controllers/serverConnections.js +++ b/packages/api/src/controllers/serverConnections.js @@ -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), diff --git a/packages/api/src/controllers/sessions.js b/packages/api/src/controllers/sessions.js index 9ca54d20..5fe1bf29 100644 --- a/packages/api/src/controllers/sessions.js +++ b/packages/api/src/controllers/sessions.js @@ -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, diff --git a/packages/api/src/main.js b/packages/api/src/main.js index c3534cf5..70002df0 100644 --- a/packages/api/src/main.js +++ b/packages/api/src/main.js @@ -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 }; diff --git a/packages/api/src/utility/DatastoreProxy.js b/packages/api/src/utility/DatastoreProxy.js index e7e23fe7..dcda4540 100644 --- a/packages/api/src/utility/DatastoreProxy.js +++ b/packages/api/src/utility/DatastoreProxy.js @@ -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 diff --git a/packages/api/src/utility/socket.js b/packages/api/src/utility/socket.js index b7659d7e..928e24c5 100644 --- a/packages/api/src/utility/socket.js +++ b/packages/api/src/utility/socket.js @@ -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); }, }; diff --git a/packages/api/src/utility/useController.js b/packages/api/src/utility/useController.js index a1f4c0d7..400db650 100644 --- a/packages/api/src/utility/useController.js +++ b/packages/api/src/utility/useController.js @@ -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; diff --git a/packages/web/src/utility/api.ts b/packages/web/src/utility/api.ts index e9e20e52..8c1d6cb0 100644 --- a/packages/web/src/utility/api.ts +++ b/packages/web/src/utility/api.ts @@ -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 (electron) { - electron.removeEventListener(event, handler); - } else { - wantEventSource(); - if (apiHandlers.has(handler)) { + if (apiHandlers.has(handler)) { + if (electron) { + electron.removeEventListener(event, apiHandlers.get(handler)); + } else { + wantEventSource(); eventSource.removeEventListener(event, apiHandlers.get(handler)); } } diff --git a/packages/web/src/utility/getElectron.ts b/packages/web/src/utility/getElectron.ts index 1f907de2..07cedc3b 100644 --- a/packages/web/src/utility/getElectron.ts +++ b/packages/web/src/utility/getElectron.ts @@ -41,7 +41,7 @@ class ElectronApi { } removeEventListener(channel: string, listener: Function) { - this.ipcRenderer.removeEventListener(channel, listener); + this.ipcRenderer.removeListener(channel, listener); } }