diff options
Diffstat (limited to 'gui/src')
| -rw-r--r-- | gui/src/main/index.ts | 2 | ||||
| -rw-r--r-- | gui/src/main/ipc-event-channel.ts | 5 | ||||
| -rw-r--r-- | gui/src/main/logging.ts | 2 | ||||
| -rw-r--r-- | gui/src/main/window-controller.ts | 2 | ||||
| -rw-r--r-- | gui/src/renderer/app.tsx | 4 | ||||
| -rw-r--r-- | gui/src/renderer/lib/ipc-event-channel.ts | 5 | ||||
| -rw-r--r-- | gui/src/renderer/lib/logging.ts | 3 | ||||
| -rw-r--r-- | gui/src/renderer/preload.ts | 2 | ||||
| -rw-r--r-- | gui/src/shared/ipc-helpers.ts | 60 | ||||
| -rw-r--r-- | gui/src/shared/ipc-schema.ts (renamed from gui/src/shared/ipc-event-channel.ts) | 14 |
10 files changed, 51 insertions, 48 deletions
diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts index 872c043f89..fa522b66d4 100644 --- a/gui/src/main/index.ts +++ b/gui/src/main/index.ts @@ -36,9 +36,9 @@ import { } from '../shared/daemon-rpc-types'; import { messages, relayLocations } from '../shared/gettext'; import { SYSTEM_PREFERRED_LOCALE_KEY } from '../shared/gui-settings-state'; -import { IpcMainEventChannel } from '../shared/ipc-event-channel'; import log, { ConsoleOutput, Logger } from '../shared/logging'; import { LogLevel } from '../shared/logging-types'; +import { IpcMainEventChannel } from './ipc-event-channel'; import { AccountExpiredNotificationProvider, CloseToAccountExpiryNotificationProvider, diff --git a/gui/src/main/ipc-event-channel.ts b/gui/src/main/ipc-event-channel.ts new file mode 100644 index 0000000000..9bd8af1490 --- /dev/null +++ b/gui/src/main/ipc-event-channel.ts @@ -0,0 +1,5 @@ +import { ipcMain } from 'electron'; +import { createIpcMain } from '../shared/ipc-helpers'; +import { ipcSchema } from '../shared/ipc-schema'; + +export const IpcMainEventChannel = createIpcMain(ipcSchema, ipcMain); diff --git a/gui/src/main/logging.ts b/gui/src/main/logging.ts index fb21c54b48..7029625d3f 100644 --- a/gui/src/main/logging.ts +++ b/gui/src/main/logging.ts @@ -1,7 +1,7 @@ import { app } from 'electron'; import fs from 'fs'; import path from 'path'; -import { IpcMainEventChannel } from '../shared/ipc-event-channel'; +import { IpcMainEventChannel } from './ipc-event-channel'; import { LogLevel, ILogInput, ILogOutput } from '../shared/logging-types'; export const OLD_LOG_FILES = ['frontend-renderer.log']; diff --git a/gui/src/main/window-controller.ts b/gui/src/main/window-controller.ts index a44724d23f..bed94c37da 100644 --- a/gui/src/main/window-controller.ts +++ b/gui/src/main/window-controller.ts @@ -1,5 +1,5 @@ import { BrowserWindow, Display, screen, Tray, WebContents } from 'electron'; -import { IpcMainEventChannel } from '../shared/ipc-event-channel'; +import { IpcMainEventChannel } from './ipc-event-channel'; interface IPosition { x: number; diff --git a/gui/src/renderer/app.tsx b/gui/src/renderer/app.tsx index 7f243b6101..21f8568445 100644 --- a/gui/src/renderer/app.tsx +++ b/gui/src/renderer/app.tsx @@ -16,11 +16,11 @@ import userInterfaceActions from './redux/userinterface/actions'; import versionActions from './redux/version/actions'; import { ICurrentAppVersionInfo } from '../main'; +import { ILinuxSplitTunnelingApplication } from '../shared/application-types'; import { messages, relayLocations } from '../shared/gettext'; import { IGuiSettingsState, SYSTEM_PREFERRED_LOCALE_KEY } from '../shared/gui-settings-state'; -import { IRelayListPair } from '../shared/ipc-event-channel'; -import { ILinuxSplitTunnelingApplication } from '../shared/application-types'; import log, { ConsoleOutput } from '../shared/logging'; +import { IRelayListPair } from '../shared/ipc-schema'; import consumePromise from '../shared/promise'; import History from './lib/history'; import { loadTranslations } from './lib/load-translations'; diff --git a/gui/src/renderer/lib/ipc-event-channel.ts b/gui/src/renderer/lib/ipc-event-channel.ts new file mode 100644 index 0000000000..bcb816e918 --- /dev/null +++ b/gui/src/renderer/lib/ipc-event-channel.ts @@ -0,0 +1,5 @@ +import { ipcRenderer } from 'electron'; +import { createIpcRenderer } from '../../shared/ipc-helpers'; +import { ipcSchema } from '../../shared/ipc-schema'; + +export const IpcRendererEventChannel = createIpcRenderer(ipcSchema, ipcRenderer); diff --git a/gui/src/renderer/lib/logging.ts b/gui/src/renderer/lib/logging.ts index 433ba20f8f..7c8dc9e542 100644 --- a/gui/src/renderer/lib/logging.ts +++ b/gui/src/renderer/lib/logging.ts @@ -1,10 +1,9 @@ -import { IpcRendererEventChannel } from '../../shared/ipc-event-channel'; import { ILogOutput, LogLevel } from '../../shared/logging-types'; export default class IpcOutput implements ILogOutput { constructor(public level: LogLevel) {} public write(level: LogLevel, message: string) { - IpcRendererEventChannel.logging.log({ level: level, message }); + window.ipc.logging.log({ level: level, message }); } } diff --git a/gui/src/renderer/preload.ts b/gui/src/renderer/preload.ts index c5d1ca6e6a..e97278ba5b 100644 --- a/gui/src/renderer/preload.ts +++ b/gui/src/renderer/preload.ts @@ -1,3 +1,3 @@ -import { IpcRendererEventChannel } from '../shared/ipc-event-channel'; +import { IpcRendererEventChannel } from './lib/ipc-event-channel'; window.ipc = IpcRendererEventChannel; diff --git a/gui/src/shared/ipc-helpers.ts b/gui/src/shared/ipc-helpers.ts index d1a72a2df6..59eee46459 100644 --- a/gui/src/shared/ipc-helpers.ts +++ b/gui/src/shared/ipc-helpers.ts @@ -1,4 +1,4 @@ -import { ipcMain, ipcRenderer, WebContents } from 'electron'; +import { IpcMain as EIpcMain, IpcRenderer as EIpcRenderer, WebContents } from 'electron'; import { capitalize } from './string-helpers'; import log from './logging'; @@ -7,26 +7,21 @@ type Sender<T, R> = (arg: T) => R; type Notifier<T> = (webContents: WebContents, arg: T) => void; type Listener<T> = (callback: (arg: T) => void) => void; -interface IpcCall<T, R> { - direction: 'renderer-to-main' | 'main-to-renderer'; - send: (event: string) => Notifier<T> | Sender<T, R>; - receive: (event: string) => Listener<T> | Handler<T, R>; -} - -interface MainToRenderer<T> extends IpcCall<T, never> { +interface MainToRenderer<T> { direction: 'main-to-renderer'; - send: (event: string) => Notifier<T>; - receive: (event: string) => Listener<T>; + send: (event: string, ipcMain: EIpcMain) => Notifier<T>; + receive: (event: string, ipcRenderer: EIpcRenderer) => Listener<T>; } -interface RendererToMain<T, R> extends IpcCall<T, R> { +interface RendererToMain<T, R> { direction: 'renderer-to-main'; - send: (event: string) => Sender<T, R>; - receive: (event: string) => Handler<T, R>; + send: (event: string, ipcRenderer: EIpcRenderer) => Sender<T, R>; + receive: (event: string, ipcMain: EIpcMain) => Handler<T, R>; } // eslint-disable-next-line @typescript-eslint/no-explicit-any -type AnyIpcCall = IpcCall<any, any>; +type AnyIpcCall = MainToRenderer<any> | RendererToMain<any, any>; + type Schema = Record<string, Record<string, AnyIpcCall>>; // Renames all IPC calls, e.g. `callName` to either `notifyCallName` or `handleCallName` depending @@ -66,22 +61,31 @@ type IpcRenderer<S extends Schema> = { }; // Preforms the transformation of the main event channel in accordance with the above types. -export function createIpcMain<S extends Schema>(ipc: S): IpcMain<S> { - return createIpc(ipc, (event, key, spec) => { +export function createIpcMain<S extends Schema>(schema: S, ipcMain: EIpcMain): IpcMain<S> { + return createIpc(schema, (event, key, spec) => { const capitalizedKey = capitalize(key); const newKey = spec.direction === 'main-to-renderer' ? `notify${capitalizedKey}` : `handle${capitalizedKey}`; - const newValue = spec.direction === 'main-to-renderer' ? spec.send(event) : spec.receive(event); + const newValue = + spec.direction === 'main-to-renderer' + ? spec.send(event, ipcMain) + : spec.receive(event, ipcMain); return [newKey, newValue]; }); } // Preforms the transformation of the renderer event channel in accordance with the above types. -export function createIpcRenderer<S extends Schema>(ipc: S): IpcRenderer<S> { - return createIpc(ipc, (event, key, spec) => { +export function createIpcRenderer<S extends Schema>( + schema: S, + ipcRenderer: EIpcRenderer, +): IpcRenderer<S> { + return createIpc(schema, (event, key, spec) => { const newKey = spec.direction === 'main-to-renderer' ? `listen${capitalize(key)}` : key; - const newValue = spec.direction === 'main-to-renderer' ? spec.receive(event) : spec.send(event); + const newValue = + spec.direction === 'main-to-renderer' + ? spec.receive(event, ipcRenderer) + : spec.send(event, ipcRenderer); return [newKey, newValue]; }); @@ -105,8 +109,8 @@ function createIpc<S extends Schema, T, R extends IpcMain<S> | IpcRenderer<S>>( export function send<T>(): RendererToMain<T, void> { return { direction: 'renderer-to-main', - send: (event: string) => (newValue: T) => ipcRenderer.send(event, newValue), - receive: (event: string) => (handlerFn: (value: T) => void) => { + send: (event, ipcRenderer) => (newValue: T) => ipcRenderer.send(event, newValue), + receive: (event, ipcMain) => (handlerFn: (value: T) => void) => { ipcMain.on(event, (_event, newValue: T) => { handlerFn(newValue); }); @@ -118,8 +122,8 @@ export function send<T>(): RendererToMain<T, void> { export function invokeSync<T, R>(): RendererToMain<T, R> { return { direction: 'renderer-to-main', - send: (event: string) => (newValue: T) => ipcRenderer.sendSync(event, newValue), - receive: (event: string) => (handlerFn: (value: T) => R) => { + send: (event, ipcRenderer) => (newValue: T) => ipcRenderer.sendSync(event, newValue), + receive: (event, ipcMain) => (handlerFn: (value: T) => R) => { ipcMain.on(event, (ipcEvent, newValue: T) => { ipcEvent.returnValue = handlerFn(newValue); }); @@ -141,13 +145,13 @@ export function notifyRenderer<T>(): MainToRenderer<T> { return { direction: 'main-to-renderer', send: notifyRendererImpl, - receive: (event: string) => (fn: (value: T) => void) => { + receive: (event, ipcRenderer) => (fn: (value: T) => void) => { ipcRenderer.on(event, (_event, newState: T) => fn(newState)); }, }; } -function notifyRendererImpl<T>(event: string): Notifier<T> { +function notifyRendererImpl<T>(event: string, _ipcMain: EIpcMain): Notifier<T> { return (webContents: WebContents, value: T) => { if (webContents.isDestroyed()) { log.error(`sender(${event}): webContents is already destroyed!`); @@ -159,7 +163,7 @@ function notifyRendererImpl<T>(event: string): Notifier<T> { type RequestResult<T> = { type: 'success'; value: T } | { type: 'error'; message: string }; -function invokeImpl<T, R>(event: string): Sender<T, Promise<R>> { +function invokeImpl<T, R>(event: string, ipcRenderer: EIpcRenderer): Sender<T, Promise<R>> { return async (arg: T): Promise<R> => { const result: RequestResult<R> = await ipcRenderer.invoke(event, arg); switch (result.type) { @@ -171,7 +175,7 @@ function invokeImpl<T, R>(event: string): Sender<T, Promise<R>> { }; } -function handle<T, R>(event: string): Handler<T, Promise<R>> { +function handle<T, R>(event: string, ipcMain: EIpcMain): Handler<T, Promise<R>> { return (fn: (arg: T) => Promise<R>) => { ipcMain.handle(event, async (_ipcEvent, arg: T) => { try { diff --git a/gui/src/shared/ipc-event-channel.ts b/gui/src/shared/ipc-schema.ts index 59304e3678..046f938835 100644 --- a/gui/src/shared/ipc-event-channel.ts +++ b/gui/src/shared/ipc-schema.ts @@ -19,20 +19,13 @@ import { VoucherResponse, } from './daemon-rpc-types'; import { IGuiSettingsState } from './gui-settings-state'; -import { - createIpcMain, - createIpcRenderer, - invoke, - invokeSync, - notifyRenderer, - send, -} from './ipc-helpers'; import { LogLevel } from './logging-types'; interface ILogEntry { level: LogLevel; message: string; } +import { invoke, invokeSync, notifyRenderer, send } from './ipc-helpers'; export interface ITranslations { locale: string; @@ -101,7 +94,7 @@ export interface IAppStateSnapshot { // listenFourth: (fn: (arg: boolean) => void) => void, // }, // } -const ipc = { +export const ipcSchema = { state: { get: invokeSync<void, IAppStateSnapshot>(), }, @@ -197,6 +190,3 @@ const ipc = { log: send<ILogEntry>(), }, }; - -export const IpcMainEventChannel = createIpcMain(ipc); -export const IpcRendererEventChannel = createIpcRenderer(ipc); |
