From b2493fd29657ecdef9298c87b52b6f4630cb3519 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Mon, 4 Mar 2024 21:41:53 +0100 Subject: [PATCH] Simpler function binding --- src/configuration/index.ts | 19 +++++++++++++++ src/main.ts | 38 ++--------------------------- src/preload/bindChannel.ts | 7 ++++++ src/preload/config.ts | 28 +++++++-------------- src/preload/configurationChannel.ts | 7 ++++++ src/preload/sql.ts | 10 ++++---- src/preload/sqlChannel.ts | 5 ++++ src/sql/index.ts | 7 +++--- 8 files changed, 58 insertions(+), 63 deletions(-) create mode 100644 src/preload/bindChannel.ts create mode 100644 src/preload/configurationChannel.ts create mode 100644 src/preload/sqlChannel.ts diff --git a/src/configuration/index.ts b/src/configuration/index.ts index 96617e8..5920231 100644 --- a/src/configuration/index.ts +++ b/src/configuration/index.ts @@ -2,6 +2,7 @@ import { dialog, safeStorage } from 'electron'; import { existsSync, mkdirSync, readFileSync, writeFile } from 'node:fs'; import { resolve } from 'node:path'; import envPaths from 'env-paths'; +import { CONFIGURATION_CHANNEL } from '../preload/configurationChannel'; import { ConnectionObject } from '../sql/types'; import { DEFAULT_THEME } from './themes'; import { @@ -164,4 +165,22 @@ export function updateConnectionState( writeConfiguration(config); } +const IPC_EVENT_BINDING = { + [CONFIGURATION_CHANNEL.GET]: getConfiguration, + [CONFIGURATION_CHANNEL.ADD_CONNECTION]: addConnectionToConfig, + [CONFIGURATION_CHANNEL.EDIT_CONNECTION]: editConnection, + [CONFIGURATION_CHANNEL.CHANGE_THEME]: changeTheme, + [CONFIGURATION_CHANNEL.UPDATE_CONNECTION_STATE]: updateConnectionState, +} as const; + +export function bindIpcMain(ipcMain: Electron.IpcMain): void { + for (const [channel, handler] of Object.entries(IPC_EVENT_BINDING)) { + ipcMain.handle(channel, (event, ...args: unknown[]) => + // convert the first argument to senderId and bind the rest + // @ts-expect-error issue with strict type in tsconfig, but seems to work at runtime + handler(...args) + ); + } +} + export const testables = { getBaseConfig }; diff --git a/src/main.ts b/src/main.ts index 77d008f..09f34a1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,16 +4,8 @@ import installExtension, { REACT_DEVELOPER_TOOLS, } from 'electron-devtools-installer'; import { updateElectronApp } from 'update-electron-app'; -import { - addConnectionToConfig, - changeTheme, - editConnection, - getConfiguration, - updateConnectionState, -} from './configuration'; -import type { Configuration, ConnectionAppState } from './configuration/type'; +import { bindIpcMain as bindIpcMainConfiguration } from './configuration'; import connectionStackInstance from './sql'; -import type { ConnectionObject } from './sql/types'; // Handle creating/removing shortcuts on Windows when installing/uninstalling. if (require('electron-squirrel-startup')) { @@ -82,33 +74,7 @@ app.whenReady().then(() => { installReactDevToolsExtension(); - ipcMain.handle('config:get', getConfiguration); - ipcMain.handle( - 'config:connection:add', - (event: unknown, connection: ConnectionObject): Configuration => - addConnectionToConfig(connection) - ); - ipcMain.handle( - 'config:connection:edit', - ( - event: unknown, - connectionName: string, - connection: ConnectionObject - ): Configuration => editConnection(connectionName, connection) - ); - ipcMain.handle('config:theme:change', (event: unknown, name: string) => - changeTheme(name) - ); - ipcMain.handle( - 'config:connection:updateState', - ( - event: unknown, - connectionName: string, - key: K, - value: ConnectionAppState[K] - ) => updateConnectionState(connectionName, key, value) - ); - + bindIpcMainConfiguration(ipcMain); connectionStackInstance.bindIpcMain(ipcMain); // createWindow(); diff --git a/src/preload/bindChannel.ts b/src/preload/bindChannel.ts new file mode 100644 index 0000000..21d7975 --- /dev/null +++ b/src/preload/bindChannel.ts @@ -0,0 +1,7 @@ +import { ipcRenderer } from 'electron'; + +export function bindChannel(channel: string) { + return function (...args: unknown[]) { + return ipcRenderer.invoke(channel, ...args); + }; +} diff --git a/src/preload/config.ts b/src/preload/config.ts index cf842b0..ae942b0 100644 --- a/src/preload/config.ts +++ b/src/preload/config.ts @@ -1,6 +1,7 @@ -import { ipcRenderer } from 'electron'; import type { Configuration, ConnectionAppState } from '../configuration/type'; import type { ConnectionObject } from '../sql/types'; +import { bindChannel } from './bindChannel'; +import { CONFIGURATION_CHANNEL } from './configurationChannel'; interface Config { getConfiguration(): Promise; @@ -22,22 +23,11 @@ interface Config { } export const config: Config = { - getConfiguration: () => ipcRenderer.invoke('config:get'), - addConnectionToConfig: (connection: ConnectionObject) => - ipcRenderer.invoke('config:connection:add', connection), - editConnection: (connectionName: string, connection: ConnectionObject) => - ipcRenderer.invoke('config:connection:edit', connectionName, connection), - changeTheme: (theme: string) => - ipcRenderer.invoke('config:theme:change', theme), - updateConnectionState: ( - connectionName: string, - key: K, - value: ConnectionAppState[K] - ) => - ipcRenderer.invoke( - 'config:connection:updateState', - connectionName, - key, - value - ), + getConfiguration: bindChannel(CONFIGURATION_CHANNEL.GET), + addConnectionToConfig: bindChannel(CONFIGURATION_CHANNEL.ADD_CONNECTION), + changeTheme: bindChannel(CONFIGURATION_CHANNEL.CHANGE_THEME), + updateConnectionState: bindChannel( + CONFIGURATION_CHANNEL.UPDATE_CONNECTION_STATE + ), + editConnection: bindChannel(CONFIGURATION_CHANNEL.EDIT_CONNECTION), }; diff --git a/src/preload/configurationChannel.ts b/src/preload/configurationChannel.ts new file mode 100644 index 0000000..9492f6f --- /dev/null +++ b/src/preload/configurationChannel.ts @@ -0,0 +1,7 @@ +export enum CONFIGURATION_CHANNEL { + GET = 'config:get', + ADD_CONNECTION = 'config:connection:add', + EDIT_CONNECTION = 'config:connection:edit', + CHANGE_THEME = 'config:theme:change', + UPDATE_CONNECTION_STATE = 'config:connection:updateState', +} diff --git a/src/preload/sql.ts b/src/preload/sql.ts index 7e00d2f..f27cd6d 100644 --- a/src/preload/sql.ts +++ b/src/preload/sql.ts @@ -1,10 +1,11 @@ -import { ipcRenderer } from 'electron'; import { Connection } from 'mysql2/promise'; import type { ConnectionObject, QueryResult, QueryReturnType, } from '../sql/types'; +import { bindChannel } from './bindChannel'; +import { SQL_CHANNEL } from './sqlChannel'; interface Sql { openConnection(params: ConnectionObject): Promise; @@ -12,9 +13,8 @@ interface Sql { closeAllConnections(): Promise; } -// TODO : clone the binder object in sql/index.ts ? export const sql: Sql = { - openConnection: (params) => ipcRenderer.invoke('sql:connect', params), - executeQuery: (query) => ipcRenderer.invoke('sql:executeQuery', query), - closeAllConnections: () => ipcRenderer.invoke('sql:closeAll'), + openConnection: bindChannel(SQL_CHANNEL.CONNECT), + executeQuery: bindChannel(SQL_CHANNEL.EXECUTE_QUERY), + closeAllConnections: bindChannel(SQL_CHANNEL.CLOSE_ALL), }; diff --git a/src/preload/sqlChannel.ts b/src/preload/sqlChannel.ts new file mode 100644 index 0000000..8a26274 --- /dev/null +++ b/src/preload/sqlChannel.ts @@ -0,0 +1,5 @@ +export enum SQL_CHANNEL { + CONNECT = 'sql:connect', + EXECUTE_QUERY = 'sql:executeQuery', + CLOSE_ALL = 'sql:closeAll', +} diff --git a/src/sql/index.ts b/src/sql/index.ts index d2eb770..059fe67 100644 --- a/src/sql/index.ts +++ b/src/sql/index.ts @@ -1,4 +1,5 @@ import { Connection, createConnection } from 'mysql2/promise'; +import { SQL_CHANNEL } from '../preload/sqlChannel'; import { ConnectionObject, QueryResult } from './types'; class ConnectionStack { @@ -6,9 +7,9 @@ class ConnectionStack { // List of IPC events and their handlers #ipcEventBinding = { - 'sql:connect': this.connect, - 'sql:executeQuery': this.executeQuery, - 'sql:closeAll': this.closeAllConnections, + [SQL_CHANNEL.CONNECT]: this.connect, + [SQL_CHANNEL.EXECUTE_QUERY]: this.executeQuery, + [SQL_CHANNEL.CLOSE_ALL]: this.closeAllConnections, }; bindIpcMain(ipcMain: Electron.IpcMain): void {