aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorV <vendicated@riseup.net>2023-05-02 02:50:51 +0200
committerGitHub <noreply@github.com>2023-05-02 02:50:51 +0200
commitc62d05e1b32361425474928004a92338d98ba200 (patch)
tree63e2d718807055dbdbbe7d2624270d8aa138fa2d /src/utils
parent6a1cb133cd6cbe2946e4c830291318e873da1d8b (diff)
downloadVencord-c62d05e1b32361425474928004a92338d98ba200.tar.gz
Vencord-c62d05e1b32361425474928004a92338d98ba200.tar.bz2
Vencord-c62d05e1b32361425474928004a92338d98ba200.zip
Refactor ipc to be strongly typed and hide impl details (#1018)
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/IpcEvents.ts43
-rw-r--r--src/utils/index.ts1
-rw-r--r--src/utils/quickCss.ts5
-rw-r--r--src/utils/settingsSync.ts13
-rw-r--r--src/utils/updater.ts20
5 files changed, 30 insertions, 52 deletions
diff --git a/src/utils/IpcEvents.ts b/src/utils/IpcEvents.ts
index 57e4bb2..30a68e8 100644
--- a/src/utils/IpcEvents.ts
+++ b/src/utils/IpcEvents.ts
@@ -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);
diff --git a/src/utils/index.ts b/src/utils/index.ts
index cfded6b..98e923f 100644
--- a/src/utils/index.ts
+++ b/src/utils/index.ts
@@ -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";
diff --git a/src/utils/quickCss.ts b/src/utils/quickCss.ts
index 4ae1023..1b3f78d 100644
--- a/src/utils/quickCss.ts
+++ b/src/utils/quickCss.ts
@@ -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;
diff --git a/src/utils/settingsSync.ts b/src/utils/settingsSync.ts
index ff49529..3ec2d43 100644
--- a/src/utils/settingsSync.ts
+++ b/src/utils/settingsSync.ts
@@ -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)
diff --git a/src/utils/updater.ts b/src/utils/updater.ts
index 2fd1561..ce99aa4 100644
--- a/src/utils/updater.ts
+++ b/src/utils/updater.ts
@@ -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();
}
}