Refactor ipc to be strongly typed and hide impl details (#1018)

This commit is contained in:
V 2023-05-02 02:50:51 +02:00 committed by GitHub
parent 6a1cb133cd
commit c62d05e1b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 158 additions and 218 deletions

View file

@ -1,6 +1,6 @@
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
* Copyright (c) 2023 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -16,31 +16,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
type Enum<T extends Record<string, string>> = {
[k in keyof T]: T[k];
} & { [v in keyof T as T[v]]: v; };
function strEnum<T extends Record<string, string>>(obj: T): T {
const o = {} as T;
for (const key in obj) {
o[key] = obj[key] as any;
o[obj[key]] = key as any;
}
return Object.freeze(o);
export const enum IpcEvents {
QUICK_CSS_UPDATE = "VencordQuickCssUpdate",
GET_QUICK_CSS = "VencordGetQuickCss",
SET_QUICK_CSS = "VencordSetQuickCss",
GET_SETTINGS_DIR = "VencordGetSettingsDir",
GET_SETTINGS = "VencordGetSettings",
SET_SETTINGS = "VencordSetSettings",
OPEN_EXTERNAL = "VencordOpenExternal",
OPEN_QUICKCSS = "VencordOpenQuickCss",
GET_UPDATES = "VencordGetUpdates",
GET_REPO = "VencordGetRepo",
UPDATE = "VencordUpdate",
BUILD = "VencordBuild",
OPEN_MONACO_EDITOR = "VencordOpenMonacoEditor",
}
export default strEnum({
QUICK_CSS_UPDATE: "VencordQuickCssUpdate",
GET_QUICK_CSS: "VencordGetQuickCss",
SET_QUICK_CSS: "VencordSetQuickCss",
GET_SETTINGS_DIR: "VencordGetSettingsDir",
GET_SETTINGS: "VencordGetSettings",
SET_SETTINGS: "VencordSetSettings",
OPEN_EXTERNAL: "VencordOpenExternal",
OPEN_QUICKCSS: "VencordOpenQuickCss",
GET_UPDATES: "VencordGetUpdates",
GET_REPO: "VencordGetRepo",
UPDATE: "VencordUpdate",
BUILD: "VencordBuild",
OPEN_MONACO_EDITOR: "VencordOpenMonacoEditor",
} as const);

View file

@ -20,7 +20,6 @@ export * from "./ChangeList";
export * as Constants from "./constants";
export * from "./debounce";
export * as Discord from "./discord";
export { default as IpcEvents } from "./IpcEvents";
export { default as Logger } from "./Logger";
export * from "./margins";
export * from "./misc";

View file

@ -18,7 +18,6 @@
import { addSettingsListener, Settings } from "@api/settings";
import IpcEvents from "./IpcEvents";
let style: HTMLStyleElement;
let themesStyle: HTMLStyleElement;
@ -29,8 +28,8 @@ export async function toggle(isEnabled: boolean) {
style = document.createElement("style");
style.id = "vencord-custom-css";
document.head.appendChild(style);
VencordNative.ipc.on(IpcEvents.QUICK_CSS_UPDATE, (_, css: string) => style.textContent = css);
style.textContent = await VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS);
VencordNative.quickCss.addChangeListener(css => style.textContent = css);
style.textContent = await VencordNative.quickCss.get();
}
} else
style.disabled = !isEnabled;

View file

@ -22,7 +22,6 @@ import { Toasts } from "@webpack/common";
import { deflateSync, inflateSync } from "fflate";
import { getCloudAuth, getCloudUrl } from "./cloud";
import IpcEvents from "./IpcEvents";
import Logger from "./Logger";
import { saveFile } from "./web";
@ -36,15 +35,15 @@ export async function importSettings(data: string) {
if ("settings" in parsed && "quickCss" in parsed) {
Object.assign(PlainSettings, parsed.settings);
await VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(parsed.settings, null, 4));
await VencordNative.ipc.invoke(IpcEvents.SET_QUICK_CSS, parsed.quickCss);
await VencordNative.settings.set(JSON.stringify(parsed.settings, null, 4));
await VencordNative.quickCss.set(parsed.quickCss);
} else
throw new Error("Invalid Settings. Is this even a Vencord Settings file?");
}
export async function exportSettings() {
const settings = JSON.parse(VencordNative.ipc.sendSync(IpcEvents.GET_SETTINGS));
const quickCss = await VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS);
const settings = JSON.parse(VencordNative.settings.get());
const quickCss = await VencordNative.quickCss.get();
return JSON.stringify({ settings, quickCss }, null, 4);
}
@ -147,7 +146,7 @@ export async function putCloudSettings() {
const { written } = await res.json();
PlainSettings.cloud.settingsSyncVersion = written;
VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(PlainSettings, null, 4));
VencordNative.settings.set(JSON.stringify(PlainSettings, null, 4));
cloudSettingsLogger.info("Settings uploaded to cloud successfully");
showNotification({
@ -230,7 +229,7 @@ export async function getCloudSettings(shouldNotify = true, force = false) {
// sync with server timestamp instead of local one
PlainSettings.cloud.settingsSyncVersion = written;
VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(PlainSettings, null, 4));
VencordNative.settings.set(JSON.stringify(PlainSettings, null, 4));
cloudSettingsLogger.info("Settings loaded from cloud successfully");
if (shouldNotify)

View file

@ -18,7 +18,6 @@
import gitHash from "~git-hash";
import IpcEvents from "./IpcEvents";
import Logger from "./Logger";
import { relaunch } from "./native";
import { IpcRes } from "./types";
@ -39,7 +38,7 @@ async function Unwrap<T>(p: Promise<IpcRes<T>>) {
}
export async function checkForUpdates() {
changes = await Unwrap(VencordNative.ipc.invoke<IpcRes<typeof changes>>(IpcEvents.GET_UPDATES));
changes = await Unwrap(VencordNative.updater.getUpdates());
if (changes.some(c => c.hash === gitHash)) {
isNewer = true;
return (isOutdated = false);
@ -50,22 +49,18 @@ export async function checkForUpdates() {
export async function update() {
if (!isOutdated) return true;
const res = await Unwrap(VencordNative.ipc.invoke<IpcRes<boolean>>(IpcEvents.UPDATE));
const res = await Unwrap(VencordNative.updater.update());
if (res)
if (res) {
isOutdated = false;
if (!await Unwrap(VencordNative.updater.rebuild()))
throw new Error("The Build failed. Please try manually building the new update");
}
return res;
}
export function getRepo() {
return Unwrap(VencordNative.ipc.invoke<IpcRes<string>>(IpcEvents.GET_REPO));
}
export async function rebuild() {
if (!await Unwrap(VencordNative.ipc.invoke<IpcRes<boolean>>(IpcEvents.BUILD)))
throw new Error("The Build failed. Please try manually building the new update");
}
export const getRepo = () => Unwrap(VencordNative.updater.getRepo());
export async function maybePromptToUpdate(confirmMessage: string, checkForDev = false) {
if (IS_WEB) return;
@ -78,7 +73,6 @@ export async function maybePromptToUpdate(confirmMessage: string, checkForDev =
if (wantsUpdate && isNewer) return alert("Your local copy has more recent commits. Please stash or reset them.");
if (wantsUpdate) {
await update();
await rebuild();
relaunch();
}
}