2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// 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
|
|
|
|
2022-11-17 00:29:15 +00:00
|
|
|
import type { MainSQL } from '../ts/sql/main';
|
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
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
let sql:
|
|
|
|
| Pick<
|
|
|
|
MainSQL,
|
2024-08-12 19:54:24 +00:00
|
|
|
| 'sqlRead'
|
|
|
|
| 'sqlWrite'
|
|
|
|
| 'pauseWriteAccess'
|
|
|
|
| 'resumeWriteAccess'
|
|
|
|
| 'removeDB'
|
2024-07-22 18:16:33 +00:00
|
|
|
>
|
|
|
|
| undefined;
|
2021-06-18 17:04:27 +00:00
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
let initialized = false;
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const SQL_READ_KEY = 'sql-channel:read';
|
|
|
|
const SQL_WRITE_KEY = 'sql-channel:write';
|
2024-08-12 19:54:24 +00:00
|
|
|
const SQL_REMOVE_DB_KEY = 'sql-channel:remove-db';
|
2018-07-27 01:13:56 +00:00
|
|
|
const ERASE_SQL_KEY = 'erase-sql-key';
|
2024-07-22 18:16:33 +00:00
|
|
|
const PAUSE_WRITE_ACCESS = 'pause-sql-writes';
|
|
|
|
const RESUME_WRITE_ACCESS = 'resume-sql-writes';
|
2018-07-27 01:13:56 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
export function initialize(mainSQL: typeof sql): 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;
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
ipcMain.handle(SQL_READ_KEY, (_event, callName, ...args) => {
|
2022-11-17 00:29:15 +00:00
|
|
|
if (!sql) {
|
2024-07-22 18:16:33 +00:00
|
|
|
throw new Error(`${SQL_READ_KEY}: Not yet initialized!`);
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|
2024-07-22 18:16:33 +00:00
|
|
|
return sql.sqlRead(callName, ...args);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(SQL_WRITE_KEY, (_event, callName, ...args) => {
|
|
|
|
if (!sql) {
|
|
|
|
throw new Error(`${SQL_WRITE_KEY}: Not yet initialized!`);
|
|
|
|
}
|
|
|
|
return sql.sqlWrite(callName, ...args);
|
2018-07-27 01:13:56 +00:00
|
|
|
});
|
|
|
|
|
2024-08-12 19:54:24 +00:00
|
|
|
ipcMain.handle(SQL_REMOVE_DB_KEY, () => {
|
|
|
|
if (!sql) {
|
|
|
|
throw new Error(`${SQL_REMOVE_DB_KEY}: Not yet initialized!`);
|
|
|
|
}
|
|
|
|
return sql.removeDB();
|
|
|
|
});
|
|
|
|
|
2022-11-17 00:29:15 +00:00
|
|
|
ipcMain.handle(ERASE_SQL_KEY, () => {
|
|
|
|
removeUserConfig();
|
|
|
|
removeEphemeralConfig();
|
2018-07-27 01:13:56 +00:00
|
|
|
});
|
2024-07-22 18:16:33 +00:00
|
|
|
|
|
|
|
ipcMain.handle(PAUSE_WRITE_ACCESS, () => {
|
|
|
|
if (!sql) {
|
|
|
|
throw new Error(`${PAUSE_WRITE_ACCESS}: Not yet initialized!`);
|
|
|
|
}
|
|
|
|
return sql.pauseWriteAccess();
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle(RESUME_WRITE_ACCESS, () => {
|
|
|
|
if (!sql) {
|
|
|
|
throw new Error(`${PAUSE_WRITE_ACCESS}: Not yet initialized!`);
|
|
|
|
}
|
|
|
|
return sql.resumeWriteAccess();
|
|
|
|
});
|
2018-07-27 01:13:56 +00:00
|
|
|
}
|