2021-08-18 20:08:14 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { ipcRenderer } from 'electron';
|
|
|
|
|
|
|
|
import { strictAssert } from './assert';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { UnwrapPromise } from '../types/Util';
|
2021-08-18 20:08:14 +00:00
|
|
|
import type {
|
|
|
|
IPCEventsValuesType,
|
|
|
|
IPCEventsCallbacksType,
|
|
|
|
IPCEventGetterType,
|
|
|
|
IPCEventSetterType,
|
|
|
|
} from './createIPCEvents';
|
|
|
|
|
|
|
|
type SettingOptionsType = {
|
|
|
|
getter?: boolean;
|
|
|
|
setter?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SettingType<Value> = Readonly<{
|
|
|
|
getValue: () => Promise<Value>;
|
|
|
|
setValue: (value: Value) => Promise<Value>;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
function capitalize<Name extends keyof IPCEventsValuesType>(
|
|
|
|
name: Name
|
|
|
|
): Capitalize<Name> {
|
|
|
|
const result = name.slice(0, 1).toUpperCase() + name.slice(1);
|
|
|
|
|
|
|
|
return result as Capitalize<Name>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSetterName<Key extends keyof IPCEventsValuesType>(
|
|
|
|
name: Key
|
|
|
|
): IPCEventSetterType<Key> {
|
|
|
|
return `set${capitalize(name)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getGetterName<Key extends keyof IPCEventsValuesType>(
|
|
|
|
name: Key
|
|
|
|
): IPCEventGetterType<Key> {
|
|
|
|
return `get${capitalize(name)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createSetting<
|
|
|
|
Name extends keyof IPCEventsValuesType,
|
|
|
|
Value extends IPCEventsValuesType[Name]
|
|
|
|
>(name: Name, overrideOptions: SettingOptionsType = {}): SettingType<Value> {
|
|
|
|
const options = {
|
|
|
|
getter: true,
|
|
|
|
setter: true,
|
|
|
|
...overrideOptions,
|
|
|
|
};
|
|
|
|
|
|
|
|
function getValue(): Promise<Value> {
|
|
|
|
strictAssert(options.getter, `${name} has no getter`);
|
2021-11-10 00:56:56 +00:00
|
|
|
return ipcRenderer.invoke(`settings:get:${name}`);
|
2021-08-18 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setValue(value: Value): Promise<Value> {
|
|
|
|
strictAssert(options.setter, `${name} has no setter`);
|
2021-11-10 00:56:56 +00:00
|
|
|
return ipcRenderer.invoke(`settings:set:${name}`, value);
|
2021-08-18 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
getValue,
|
|
|
|
setValue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
type UnwrapReturn<
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
Callback extends (...args: Array<any>) => unknown
|
|
|
|
> = UnwrapPromise<ReturnType<Callback>>;
|
|
|
|
|
|
|
|
export function createCallback<
|
|
|
|
Name extends keyof IPCEventsCallbacksType,
|
|
|
|
Callback extends IPCEventsCallbacksType[Name]
|
|
|
|
>(
|
|
|
|
name: Name
|
|
|
|
): (...args: Parameters<Callback>) => Promise<UnwrapReturn<Callback>> {
|
|
|
|
return (...args: Parameters<Callback>): Promise<UnwrapReturn<Callback>> => {
|
2021-11-10 00:56:56 +00:00
|
|
|
return ipcRenderer.invoke(`settings:call:${name}`, args);
|
2021-08-18 20:08:14 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function installSetting(
|
|
|
|
name: keyof IPCEventsValuesType,
|
|
|
|
{ getter = true, setter = true }: { getter?: boolean; setter?: boolean } = {}
|
|
|
|
): void {
|
|
|
|
const getterName = getGetterName(name);
|
|
|
|
const setterName = getSetterName(name);
|
|
|
|
|
|
|
|
if (getter) {
|
2021-11-10 00:56:56 +00:00
|
|
|
ipcRenderer.on(`settings:get:${name}`, async (_event, { seq }) => {
|
2021-08-18 20:08:14 +00:00
|
|
|
const getFn = window.Events[getterName];
|
|
|
|
if (!getFn) {
|
|
|
|
ipcRenderer.send(
|
|
|
|
`settings:get:${name}`,
|
|
|
|
`installGetter: ${getterName} not found for event ${name}`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2021-11-10 00:56:56 +00:00
|
|
|
ipcRenderer.send('settings:response', seq, null, await getFn());
|
2021-08-18 20:08:14 +00:00
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
ipcRenderer.send('settings:response', seq, Errors.toLogFormat(error));
|
2021-08-18 20:08:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setter) {
|
2021-11-10 00:56:56 +00:00
|
|
|
ipcRenderer.on(`settings:set:${name}`, async (_event, { seq, value }) => {
|
2021-08-18 20:08:14 +00:00
|
|
|
// Some settings do not have setters...
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const setFn = (window.Events as any)[setterName] as (
|
|
|
|
value: unknown
|
|
|
|
) => Promise<void>;
|
|
|
|
if (!setFn) {
|
|
|
|
ipcRenderer.send(
|
2021-11-10 00:56:56 +00:00
|
|
|
'settings:response',
|
|
|
|
seq,
|
2021-08-18 20:08:14 +00:00
|
|
|
`installSetter: ${setterName} not found for event ${name}`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await setFn(value);
|
2021-11-10 00:56:56 +00:00
|
|
|
ipcRenderer.send('settings:response', seq, null);
|
2021-08-18 20:08:14 +00:00
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
ipcRenderer.send('settings:response', seq, Errors.toLogFormat(error));
|
2021-08-18 20:08:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function installCallback<Name extends keyof IPCEventsCallbacksType>(
|
|
|
|
name: Name
|
|
|
|
): void {
|
2021-11-10 00:56:56 +00:00
|
|
|
ipcRenderer.on(`settings:call:${name}`, async (_, { seq, args }) => {
|
2021-08-18 20:08:14 +00:00
|
|
|
const hook = window.Events[name] as (
|
|
|
|
...hookArgs: Array<unknown>
|
|
|
|
) => Promise<unknown>;
|
|
|
|
try {
|
2021-11-10 00:56:56 +00:00
|
|
|
ipcRenderer.send('settings:response', seq, null, await hook(...args));
|
2021-08-18 20:08:14 +00:00
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
ipcRenderer.send('settings:response', seq, Errors.toLogFormat(error));
|
2021-08-18 20:08:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|