2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
import { ipcMain } from 'electron';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
import { remove as removeUserConfig } from './user_config';
|
|
|
|
import { remove as removeEphemeralConfig } from './ephemeral_config';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
type SQLType = {
|
|
|
|
sqlCall(callName: string, args: ReadonlyArray<unknown>): unknown;
|
2018-07-27 01:13:56 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
let sql: SQLType | undefined;
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
let initialized = false;
|
|
|
|
|
|
|
|
const SQL_CHANNEL_KEY = 'sql-channel';
|
|
|
|
const ERASE_SQL_KEY = 'erase-sql-key';
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export function initialize(mainSQL: SQLType): void {
|
2018-07-27 01:13:56 +00:00
|
|
|
if (initialized) {
|
|
|
|
throw new Error('sqlChannels: already initialized!');
|
|
|
|
}
|
|
|
|
initialized = true;
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
sql = mainSQL;
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
ipcMain.on(SQL_CHANNEL_KEY, async (event, jobId, callName, ...args) => {
|
|
|
|
try {
|
2021-06-18 17:04:27 +00:00
|
|
|
if (!sql) {
|
|
|
|
throw new Error(`${SQL_CHANNEL_KEY}: Not yet initialized!`);
|
|
|
|
}
|
2021-04-05 22:18:19 +00:00
|
|
|
const result = await sql.sqlCall(callName, args);
|
2018-07-27 01:13:56 +00:00
|
|
|
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}`
|
|
|
|
);
|
2019-08-20 13:24:43 +00:00
|
|
|
if (!event.sender.isDestroyed()) {
|
|
|
|
event.sender.send(`${SQL_CHANNEL_KEY}-done`, jobId, errorForDisplay);
|
|
|
|
}
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.on(ERASE_SQL_KEY, async event => {
|
|
|
|
try {
|
2018-08-28 21:53:05 +00:00
|
|
|
removeUserConfig();
|
|
|
|
removeEphemeralConfig();
|
2018-07-27 01:13:56 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|