You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
3 years ago
|
import { ipcMain } from 'electron';
|
||
|
import sql from './sql';
|
||
|
import { userConfig } from './config/user_config';
|
||
|
import { ephemeralConfig } from './config/ephemeral_config';
|
||
7 years ago
|
|
||
|
let initialized = false;
|
||
|
|
||
|
const SQL_CHANNEL_KEY = 'sql-channel';
|
||
|
const ERASE_SQL_KEY = 'erase-sql-key';
|
||
3 years ago
|
// tslint:disable: no-console
|
||
7 years ago
|
|
||
3 years ago
|
export function initialize() {
|
||
7 years ago
|
if (initialized) {
|
||
|
throw new Error('sqlChannels: already initialized!');
|
||
|
}
|
||
|
initialized = true;
|
||
|
|
||
4 years ago
|
ipcMain.on(SQL_CHANNEL_KEY, (event, jobId, callName, ...args) => {
|
||
7 years ago
|
try {
|
||
|
const fn = sql[callName];
|
||
|
if (!fn) {
|
||
4 years ago
|
throw new Error(`sql channel: ${callName} is not an available function`);
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
const result = fn(...args);
|
||
4 years ago
|
|
||
7 years ago
|
event.sender.send(`${SQL_CHANNEL_KEY}-done`, jobId, null, result);
|
||
|
} catch (error) {
|
||
|
const errorForDisplay = error && error.stack ? error.stack : error;
|
||
4 years ago
|
console.log(`sql channel error with call ${callName}: ${errorForDisplay}`);
|
||
7 years ago
|
}
|
||
|
});
|
||
|
|
||
4 years ago
|
ipcMain.on(ERASE_SQL_KEY, event => {
|
||
7 years ago
|
try {
|
||
3 years ago
|
userConfig.remove();
|
||
|
ephemeralConfig.remove();
|
||
7 years ago
|
event.sender.send(`${ERASE_SQL_KEY}-done`);
|
||
|
} catch (error) {
|
||
|
const errorForDisplay = error && error.stack ? error.stack : error;
|
||
|
console.log(`sql-erase error: ${errorForDisplay}`);
|
||
|
event.sender.send(`${ERASE_SQL_KEY}-done`, error);
|
||
|
}
|
||
|
});
|
||
|
}
|