diff options
Diffstat (limited to 'src/VencordNative.ts')
-rw-r--r-- | src/VencordNative.ts | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/src/VencordNative.ts b/src/VencordNative.ts index 755c471..e5ccaaa 100644 --- a/src/VencordNative.ts +++ b/src/VencordNative.ts @@ -1,24 +1,31 @@ import IPC_EVENTS from './utils/IpcEvents'; import { IpcRenderer, ipcRenderer } from 'electron'; +function assertEventAllowed(event: string) { + if (!(event in IPC_EVENTS)) throw new Error(`Event ${event} not allowed.`); +} export default { getVersions: () => process.versions, ipc: { send(event: string, ...args: any[]) { - if (event in IPC_EVENTS) ipcRenderer.send(event, ...args); - else throw new Error(`Event ${event} not allowed.`); + assertEventAllowed(event); + ipcRenderer.send(event, ...args); }, sendSync<T = any>(event: string, ...args: any[]): T { - if (event in IPC_EVENTS) return ipcRenderer.sendSync(event, ...args); - else throw new Error(`Event ${event} not allowed.`); + assertEventAllowed(event); + return ipcRenderer.sendSync(event, ...args); }, on(event: string, listener: Parameters<IpcRenderer["on"]>[1]) { - if (event in IPC_EVENTS) ipcRenderer.on(event, listener); - else throw new Error(`Event ${event} not allowed.`); + assertEventAllowed(event); + ipcRenderer.on(event, listener); + }, + off(event: string, listener: Parameters<IpcRenderer["off"]>[1]) { + assertEventAllowed(event); + ipcRenderer.off(event, listener); }, invoke<T = any>(event: string, ...args: any[]): Promise<T> { - if (event in IPC_EVENTS) return ipcRenderer.invoke(event, ...args); - else throw new Error(`Event ${event} not allowed.`); + assertEventAllowed(event); + return ipcRenderer.invoke(event, ...args); } }, require(mod: string) { |