signal-desktop/app/sql_channel.ts

66 lines
1.7 KiB
TypeScript
Raw Normal View History

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
import { ipcMain } from 'electron';
import type { MainSQL } from '../ts/sql/main';
import { remove as removeUserConfig } from './user_config';
import { remove as removeEphemeralConfig } from './ephemeral_config';
2024-07-22 18:16:33 +00:00
let sql:
| Pick<
MainSQL,
'sqlRead' | 'sqlWrite' | 'pauseWriteAccess' | 'resumeWriteAccess'
>
| undefined;
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';
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';
2024-07-22 18:16:33 +00:00
export function initialize(mainSQL: typeof sql): void {
if (initialized) {
throw new Error('sqlChannels: already initialized!');
}
initialized = true;
sql = mainSQL;
2024-07-22 18:16:33 +00:00
ipcMain.handle(SQL_READ_KEY, (_event, callName, ...args) => {
if (!sql) {
2024-07-22 18:16:33 +00:00
throw new Error(`${SQL_READ_KEY}: Not yet initialized!`);
}
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);
});
ipcMain.handle(ERASE_SQL_KEY, () => {
removeUserConfig();
removeEphemeralConfig();
});
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();
});
}