From cfbf8a67568d044eecdc207d437b8114683ad9dd Mon Sep 17 00:00:00 2001 From: Samuele de Tomasi Date: Thu, 8 Jul 2021 14:14:29 +0200 Subject: [PATCH] Create IPC module and refractored systemInfo and updateInfo --- dist/IPC/General/IPC.js | 29 +++++++++++ dist/IPC/General/contextBridge.js | 9 ++-- dist/IPC/systemInfo.js | 23 ++++----- dist/IPC/updaterInfo.js | 55 ++++++++++---------- package.json | 2 +- src/electron/IPC/General/IPC.ts | 32 ++++++++++++ src/electron/IPC/General/contextBridge.ts | 13 +++-- src/electron/IPC/systemInfo.ts | 23 ++++----- src/electron/IPC/updaterInfo.ts | 61 ++++++++++++----------- src/electron/index.ts | 4 +- src/electron/preload.ts | 6 +-- 11 files changed, 165 insertions(+), 92 deletions(-) create mode 100644 dist/IPC/General/IPC.js create mode 100644 src/electron/IPC/General/IPC.ts diff --git a/dist/IPC/General/IPC.js b/dist/IPC/General/IPC.js new file mode 100644 index 0000000..9f491b3 --- /dev/null +++ b/dist/IPC/General/IPC.js @@ -0,0 +1,29 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +class IPC { + constructor(channels) { + this.nameAPI = "api"; + this.validSendChannel = {}; + this.validReceiveChannel = []; + this.nameAPI = channels.nameAPI; + this.validSendChannel = channels.validSendChannel; + this.validReceiveChannel = channels.validReceiveChannel; + } + get channels() { + return { + nameAPI: this.nameAPI, + validSendChannel: this.validSendChannel, + validReceiveChannel: this.validReceiveChannel + }; + } + initIpcMain(ipcMain, mainWindow) { + if (mainWindow) { + Object.keys(this.validSendChannel).forEach(key => { + ipcMain.on(key, async (event, message) => { + this.validSendChannel[key](mainWindow, event, message); + }); + }); + } + } +} +exports.default = IPC; diff --git a/dist/IPC/General/contextBridge.js b/dist/IPC/General/contextBridge.js index 0790bf2..3255b08 100644 --- a/dist/IPC/General/contextBridge.js +++ b/dist/IPC/General/contextBridge.js @@ -2,7 +2,11 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.generateContextBridge = void 0; const electron_1 = require("electron"); -function generateContextBridge(listChannels) { +function generateContextBridge(listIPC) { + let listChannels = []; + listIPC.forEach(el => { + listChannels.push(el.channels); + }); let listAPI = {}; listChannels.forEach(el => { const api = getContextBridge(el); @@ -27,8 +31,7 @@ function getContextBridge(obj) { receive: (channel, func) => { if (validReceiveChannel.includes(channel)) { // Deliberately strip event as it includes `sender` - // @ts-ignore - electron_1.ipcRenderer.on(channel, (event, ...args) => func(...args)); + electron_1.ipcRenderer.on(channel, (event, ...args) => { func(...args); }); } } }; diff --git a/dist/IPC/systemInfo.js b/dist/IPC/systemInfo.js index 950a4de..eb0b9ed 100644 --- a/dist/IPC/systemInfo.js +++ b/dist/IPC/systemInfo.js @@ -1,6 +1,9 @@ "use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", { value: true }); -exports.initIpcMain = exports.channels = void 0; +const IPC_1 = __importDefault(require("./General/IPC")); const nameAPI = "systemInfo"; // to Main const validSendChannel = { @@ -10,17 +13,13 @@ const validSendChannel = { const validReceiveChannel = [ "getSystemInfo", ]; -exports.channels = { nameAPI, validSendChannel, validReceiveChannel }; -function initIpcMain(ipcMain, mainWindow) { - if (mainWindow) { - Object.keys(validSendChannel).forEach(key => { - ipcMain.on(key, async (event, message) => { - validSendChannel[key](mainWindow, event, message); - }); - }); - } -} -exports.initIpcMain = initIpcMain; +const systemInfo = new IPC_1.default({ + nameAPI, + validSendChannel, + validReceiveChannel +}); +exports.default = systemInfo; +// Enter here the functions for ElectronJS function requestSystemInfo(mainWindow, event, message) { const versionChrome = process.versions.chrome; const versionNode = process.versions.node; diff --git a/dist/IPC/updaterInfo.js b/dist/IPC/updaterInfo.js index 9453b70..f1db07f 100644 --- a/dist/IPC/updaterInfo.js +++ b/dist/IPC/updaterInfo.js @@ -1,8 +1,11 @@ "use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", { value: true }); -exports.initAutoUpdater = exports.initIpcMain = exports.channels = void 0; const electron_1 = require("electron"); const electron_updater_1 = require("electron-updater"); +const IPC_1 = __importDefault(require("./General/IPC")); const nameAPI = "updaterInfo"; // to Main const validSendChannel = { @@ -20,32 +23,18 @@ const validReceiveChannel = [ "downloadProgress", "updateDownloaded", ]; -exports.channels = { nameAPI, validSendChannel, validReceiveChannel }; -function initIpcMain(ipcMain, mainWindow) { - if (mainWindow) { - Object.keys(validSendChannel).forEach(key => { - ipcMain.on(key, async (event, message) => { - validSendChannel[key](mainWindow, event, message); - }); - }); +class UpdaterInfo extends IPC_1.default { + initAutoUpdater(autoUpdater, mainWindow) { + initAutoUpdater(autoUpdater, mainWindow); } } -exports.initIpcMain = initIpcMain; -function requestVersionNumber(mainWindow, event, message) { - const version = electron_1.app.getVersion(); - const result = { version }; - mainWindow.webContents.send("getVersionNumber", result); -} -function checkForUpdate(mainWindow, event, message) { - electron_updater_1.autoUpdater.autoDownload = false; - electron_updater_1.autoUpdater.checkForUpdates(); -} -function startDownloadUpdate(mainWindow, event, message) { - electron_updater_1.autoUpdater.downloadUpdate(); -} -function quitAndInstall(mainWindow, event, message) { - electron_updater_1.autoUpdater.quitAndInstall(); -} +const updaterInfo = new UpdaterInfo({ + nameAPI, + validSendChannel, + validReceiveChannel +}); +exports.default = updaterInfo; +// Enter here the functions for ElectronJS function initAutoUpdater(autoUpdater, mainWindow) { autoUpdater.on('checking-for-update', () => { mainWindow.webContents.send("checkingForUpdate", null); @@ -64,4 +53,18 @@ function initAutoUpdater(autoUpdater, mainWindow) { mainWindow.webContents.send("updateNotAvailable", info); }); } -exports.initAutoUpdater = initAutoUpdater; +function requestVersionNumber(mainWindow, event, message) { + const version = electron_1.app.getVersion(); + const result = { version }; + mainWindow.webContents.send("getVersionNumber", result); +} +function checkForUpdate(mainWindow, event, message) { + electron_updater_1.autoUpdater.autoDownload = false; + electron_updater_1.autoUpdater.checkForUpdates(); +} +function startDownloadUpdate(mainWindow, event, message) { + electron_updater_1.autoUpdater.downloadUpdate(); +} +function quitAndInstall(mainWindow, event, message) { + electron_updater_1.autoUpdater.quitAndInstall(); +} diff --git a/package.json b/package.json index 293b8e0..f4850fd 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Memento: how to use Svelte with Electron and TypeScript", "author": "Samuele de Tomasi ", "license": "MIT", - "version": "0.0.5", + "version": "0.0.6", "main": "dist/index.js", "scripts": { "nodemon": "nodemon", diff --git a/src/electron/IPC/General/IPC.ts b/src/electron/IPC/General/IPC.ts new file mode 100644 index 0000000..6e48710 --- /dev/null +++ b/src/electron/IPC/General/IPC.ts @@ -0,0 +1,32 @@ +import { BrowserWindow, IpcMain } from "electron"; +import { APIChannels, SendChannels } from "./channelsInterface"; + +export default class IPC { + nameAPI: string = "api"; + validSendChannel: SendChannels = {}; + validReceiveChannel: string[] = []; + + constructor(channels: APIChannels) { + this.nameAPI = channels.nameAPI; + this.validSendChannel = channels.validSendChannel; + this.validReceiveChannel = channels.validReceiveChannel; + } + + get channels():APIChannels { + return { + nameAPI: this.nameAPI, + validSendChannel: this.validSendChannel, + validReceiveChannel: this.validReceiveChannel + } + } + + initIpcMain(ipcMain:IpcMain, mainWindow: BrowserWindow) { + if (mainWindow) { + Object.keys(this.validSendChannel).forEach(key => { + ipcMain.on(key, async( event, message) => { + this.validSendChannel[key](mainWindow, event, message); + }); + }); + } + } +} \ No newline at end of file diff --git a/src/electron/IPC/General/contextBridge.ts b/src/electron/IPC/General/contextBridge.ts index 0c98202..636dbe7 100644 --- a/src/electron/IPC/General/contextBridge.ts +++ b/src/electron/IPC/General/contextBridge.ts @@ -1,7 +1,13 @@ import { contextBridge, ipcRenderer } from "electron"; import { APIContextBridge, APIChannels } from "./channelsInterface"; +import IPC from "./IPC"; -export function generateContextBridge(listChannels: APIChannels[]) { +export function generateContextBridge(listIPC: IPC[]) { + + let listChannels: APIChannels[] = []; + listIPC.forEach(el => { + listChannels.push(el.channels); + }); let listAPI: {[key: string]: APIContextBridge} = {}; @@ -30,8 +36,7 @@ function getContextBridge(obj: APIChannels): APIContextBridge { receive: (channel: string, func: (arg0: any) => void) => { if (validReceiveChannel.includes(channel)) { // Deliberately strip event as it includes `sender` - // @ts-ignore - ipcRenderer.on(channel, (event, ...args) => func(...args)); + ipcRenderer.on(channel, (event, ...args: [any]) => {func(...args);}); } } } @@ -41,4 +46,4 @@ function getArrayOfValidSendChannel(obj: APIChannels): string[] { const { validSendChannel } = { ...obj }; let result: string[] = Object.keys(validSendChannel); return result; -} \ No newline at end of file +} diff --git a/src/electron/IPC/systemInfo.ts b/src/electron/IPC/systemInfo.ts index c04298a..6947618 100644 --- a/src/electron/IPC/systemInfo.ts +++ b/src/electron/IPC/systemInfo.ts @@ -1,5 +1,6 @@ -import { BrowserWindow, IpcMain } from "electron"; -import { APIChannels, SendChannels } from "./General/channelsInterface"; +import { BrowserWindow } from "electron"; +import { SendChannels } from "./General/channelsInterface"; +import IPC from "./General/IPC"; const nameAPI = "systemInfo"; @@ -13,17 +14,15 @@ const validReceiveChannel: string[] = [ "getSystemInfo", ]; -export const channels: APIChannels = { nameAPI, validSendChannel, validReceiveChannel } +const systemInfo = new IPC ({ + nameAPI, + validSendChannel, + validReceiveChannel +}); -export function initIpcMain(ipcMain:IpcMain, mainWindow: BrowserWindow) { - if (mainWindow) { - Object.keys(validSendChannel).forEach(key => { - ipcMain.on(key, async( event, message) => { - validSendChannel[key](mainWindow, event, message); - }); - }); - } -} +export default systemInfo; + +// Enter here the functions for ElectronJS function requestSystemInfo(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, message: any) { const versionChrome = process.versions.chrome; diff --git a/src/electron/IPC/updaterInfo.ts b/src/electron/IPC/updaterInfo.ts index 38dea62..d2c20e9 100644 --- a/src/electron/IPC/updaterInfo.ts +++ b/src/electron/IPC/updaterInfo.ts @@ -1,7 +1,7 @@ -import { BrowserWindow, IpcMain, app } from "electron"; +import { BrowserWindow, app } from "electron"; import { AppUpdater, autoUpdater } from "electron-updater"; - -import { APIChannels, SendChannels } from "./General/channelsInterface"; +import { SendChannels } from "./General/channelsInterface"; +import IPC from "./General/IPC"; const nameAPI = "updaterInfo"; @@ -23,38 +23,23 @@ const validReceiveChannel: string[] = [ "updateDownloaded", ]; -export const channels: APIChannels = { nameAPI, validSendChannel, validReceiveChannel } - -export function initIpcMain(ipcMain:IpcMain, mainWindow: BrowserWindow) { - if (mainWindow) { - Object.keys(validSendChannel).forEach(key => { - ipcMain.on(key, async( event, message) => { - validSendChannel[key](mainWindow, event, message); - }); - }); +class UpdaterInfo extends IPC { + initAutoUpdater(autoUpdater: AppUpdater, mainWindow: BrowserWindow) { + initAutoUpdater(autoUpdater, mainWindow); } } -function requestVersionNumber(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, message: any) { - const version = app.getVersion(); - const result = {version}; - mainWindow.webContents.send("getVersionNumber", result); -} - -function checkForUpdate(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, message: any) { - autoUpdater.autoDownload = false; - autoUpdater.checkForUpdates(); -} +const updaterInfo = new UpdaterInfo ({ + nameAPI, + validSendChannel, + validReceiveChannel +}); -function startDownloadUpdate(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, message: any) { - autoUpdater.downloadUpdate(); -} +export default updaterInfo; -function quitAndInstall(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, message: any) { - autoUpdater.quitAndInstall(); -} +// Enter here the functions for ElectronJS -export function initAutoUpdater(autoUpdater: AppUpdater, mainWindow: BrowserWindow) { +function initAutoUpdater(autoUpdater: AppUpdater, mainWindow: BrowserWindow) { autoUpdater.on('checking-for-update', () => { mainWindow.webContents.send("checkingForUpdate", null); }); @@ -79,6 +64,24 @@ export function initAutoUpdater(autoUpdater: AppUpdater, mainWindow: BrowserWind }); } +function requestVersionNumber(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, message: any) { + const version = app.getVersion(); + const result = {version}; + mainWindow.webContents.send("getVersionNumber", result); +} + +function checkForUpdate(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, message: any) { + autoUpdater.autoDownload = false; + autoUpdater.checkForUpdates(); +} + +function startDownloadUpdate(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, message: any) { + autoUpdater.downloadUpdate(); +} + +function quitAndInstall(mainWindow: BrowserWindow, event: Electron.IpcMainEvent, message: any) { + autoUpdater.quitAndInstall(); +} diff --git a/src/electron/index.ts b/src/electron/index.ts index 4b7c967..4a5d3cf 100644 --- a/src/electron/index.ts +++ b/src/electron/index.ts @@ -2,8 +2,8 @@ import { ipcMain } from 'electron'; import { autoUpdater } from "electron-updater"; import Main from "./mainWindow"; -import * as systemInfo from "./IPC/systemInfo"; -import * as updaterInfo from "./IPC/updaterInfo"; +import systemInfo from './IPC/systemInfo'; +import updaterInfo from './IPC/updaterInfo'; require('electron-reload')(__dirname); diff --git a/src/electron/preload.ts b/src/electron/preload.ts index 60b553f..07c5214 100644 --- a/src/electron/preload.ts +++ b/src/electron/preload.ts @@ -1,6 +1,6 @@ import { generateContextBridge } from "./IPC/General/contextBridge" -import * as systemInfo from "./IPC/systemInfo"; -import * as updaterInfo from "./IPC/updaterInfo"; +import systemInfo from "./IPC/systemInfo"; +import updaterInfo from './IPC/updaterInfo'; -generateContextBridge([systemInfo.channels, updaterInfo.channels]); +generateContextBridge([systemInfo, updaterInfo]);