Skip to content

Commit

Permalink
Create IPC module and refractored systemInfo and updateInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
el3um4s committed Jul 8, 2021
1 parent 7a59396 commit cfbf8a6
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 92 deletions.
29 changes: 29 additions & 0 deletions dist/IPC/General/IPC.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 6 additions & 3 deletions dist/IPC/General/contextBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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); });
}
}
};
Expand Down
23 changes: 11 additions & 12 deletions dist/IPC/systemInfo.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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;
Expand Down
55 changes: 29 additions & 26 deletions dist/IPC/updaterInfo.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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);
Expand All @@ -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();
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Memento: how to use Svelte with Electron and TypeScript",
"author": "Samuele de Tomasi <[email protected]>",
"license": "MIT",
"version": "0.0.5",
"version": "0.0.6",
"main": "dist/index.js",
"scripts": {
"nodemon": "nodemon",
Expand Down
32 changes: 32 additions & 0 deletions src/electron/IPC/General/IPC.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
}
}
}
13 changes: 9 additions & 4 deletions src/electron/IPC/General/contextBridge.ts
Original file line number Diff line number Diff line change
@@ -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} = {};

Expand Down Expand Up @@ -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);});
}
}
}
Expand All @@ -41,4 +46,4 @@ function getArrayOfValidSendChannel(obj: APIChannels): string[] {
const { validSendChannel } = { ...obj };
let result: string[] = Object.keys(validSendChannel);
return result;
}
}
23 changes: 11 additions & 12 deletions src/electron/IPC/systemInfo.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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;
Expand Down
61 changes: 32 additions & 29 deletions src/electron/IPC/updaterInfo.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
});
Expand All @@ -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();
}



Expand Down
4 changes: 2 additions & 2 deletions src/electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/electron/preload.ts
Original file line number Diff line number Diff line change
@@ -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]);

0 comments on commit cfbf8a6

Please sign in to comment.