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