signal-desktop/app/sql_channel.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
const electron = require('electron');
const { remove: removeUserConfig } = require('./user_config');
const { remove: removeEphemeralConfig } = require('./ephemeral_config');
const { ipcMain } = electron;
let sql;
module.exports = {
initialize,
};
let initialized = false;
const SQL_CHANNEL_KEY = 'sql-channel';
const ERASE_SQL_KEY = 'erase-sql-key';
function initialize(mainSQL) {
if (initialized) {
throw new Error('sqlChannels: already initialized!');
}
initialized = true;
sql = mainSQL;
ipcMain.on(SQL_CHANNEL_KEY, async (event, jobId, callName, ...args) => {
try {
const result = await sql.sqlCall(callName, 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}`
);
if (!event.sender.isDestroyed()) {
event.sender.send(`${SQL_CHANNEL_KEY}-done`, jobId, errorForDisplay);
}
}
});
ipcMain.on(ERASE_SQL_KEY, async event => {
try {
removeUserConfig();
removeEphemeralConfig();
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);
}
});
}