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.
session-desktop/ts/node/sql_channel.ts

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';
let initialized = false;
const SQL_CHANNEL_KEY = 'sql-channel';
const ERASE_SQL_KEY = 'erase-sql-key';
3 years ago
// tslint:disable: no-console
3 years ago
export function initialize() {
if (initialized) {
throw new Error('sqlChannels: already initialized!');
}
initialized = true;
ipcMain.on(SQL_CHANNEL_KEY, (event, jobId, callName, ...args) => {
try {
const fn = sql[callName];
if (!fn) {
throw new Error(`sql channel: ${callName} is not an available function`);
}
const result = fn(...args);
event.sender.send(`${SQL_CHANNEL_KEY}-done`, jobId, null, result);
} catch (error) {
const errorForDisplay = error && error.stack ? error.stack : error;
console.log(`sql channel error with call ${callName}: ${errorForDisplay}`);
}
});
ipcMain.on(ERASE_SQL_KEY, event => {
try {
3 years ago
userConfig.remove();
ephemeralConfig.remove();
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);
}
});
}