From 73acf88a079984e50c15e23e5ad0aa98b0a7c2a1 Mon Sep 17 00:00:00 2001 From: Amadou Ada DIENE Date: Sat, 18 Apr 2020 17:13:02 +0200 Subject: [PATCH] feat: adapt electron part --- src/electron/components/window.ts | 20 +++++++++++++++++++- src/electron/preload.ts | 22 ++++++++++++++++++++++ src/electron/services/abstract-service.ts | 13 +++++++++++++ src/electron/services/multiples-service.ts | 21 +++++++++++++++++++++ src/electron/tsconfig.json | 6 +++++- 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 src/electron/services/abstract-service.ts create mode 100644 src/electron/services/multiples-service.ts diff --git a/src/electron/components/window.ts b/src/electron/components/window.ts index 40ca932..efe5b68 100644 --- a/src/electron/components/window.ts +++ b/src/electron/components/window.ts @@ -1,6 +1,8 @@ -import { app, BrowserWindow } from 'electron'; +import { app, BrowserWindow, ipcMain } from 'electron'; import * as path from 'path'; import * as url from 'url'; +import { AbstractService } from '../services/abstract-service'; +import { MultiplesService } from '../services/multiples-service'; export class Window { private _window: BrowserWindow | any; @@ -12,6 +14,7 @@ export class Window { this.createWindow(); this.loadRender(); + this.registerService(MultiplesService); } private createWindow(): void { @@ -61,6 +64,21 @@ export class Window { }); } + private registerService(AnyService: typeof AbstractService) { + const service = new AnyService(); + ipcMain.on(service.receptionChannel(), async (event, ...args) => { + // Handling input + console.log(`Received [${service.receptionChannel()}] : `, args); + const data = await service.process(...args); + + // Handling output + if (service.sendingChannel()) { + console.log(`Sent [${service.sendingChannel()}] : `, data); + this._window.webContents.send(service.sendingChannel(), ...data); + } + }); + } + public get window(): BrowserWindow | any { return this._window; } diff --git a/src/electron/preload.ts b/src/electron/preload.ts index 5301eda..7a382a9 100644 --- a/src/electron/preload.ts +++ b/src/electron/preload.ts @@ -1 +1,23 @@ +// To secure user platform when running renderer process stuff, +// Node.JS and Electron APIs are only available in this script +import { contextBridge, ipcRenderer } from 'electron'; +import { WindowApi, WindowApiConst } from 'shared'; + +// So we expose protected methods that allow the renderer process +// to use the ipcRenderer without exposing the entire object +const windowApi: WindowApi = { + send: (channel: any, ...data: any) => { + if (WindowApiConst.SENDING_SAFE_CHANNELS.includes(channel)) { + ipcRenderer.send(channel, ...data); + } + }, + receive: (channel: string, func: (...data: any) => void) => { + if (WindowApiConst.RECEIVING_SAFE_CHANNELS.includes(channel)) { + // Deliberately strip event as it includes `sender` + ipcRenderer.on(channel, (event, ...args) => func(...args)); + } + }, +}; +contextBridge.exposeInMainWorld('api', windowApi); + console.log('The preload script has been injected successfully.'); diff --git a/src/electron/services/abstract-service.ts b/src/electron/services/abstract-service.ts new file mode 100644 index 0000000..d62acb1 --- /dev/null +++ b/src/electron/services/abstract-service.ts @@ -0,0 +1,13 @@ +export class AbstractService { + receptionChannel(): string { + throw new Error('Method not implemented.'); + } + + sendingChannel(): string { + throw new Error('Method not implemented.'); + } + + process(...args: any): any { + throw new Error('Method not implemented.'); + } +} diff --git a/src/electron/services/multiples-service.ts b/src/electron/services/multiples-service.ts new file mode 100644 index 0000000..09d8b48 --- /dev/null +++ b/src/electron/services/multiples-service.ts @@ -0,0 +1,21 @@ +import { WindowApiConst } from 'shared'; +import { AbstractService } from './abstract-service'; + +export class MultiplesService extends AbstractService { + receptionChannel(): string { + return WindowApiConst.MULTIPLES_INPUT; + } + + sendingChannel(): string { + return WindowApiConst.MULTIPLES_OUTPUT; + } + + process(...args: any): any { + // From 1 to 10, return input multiples + const multiples = []; + for (let n = 1; n <= 10; n++) { + multiples.push(n * args[0]); + } + return multiples; + } +} diff --git a/src/electron/tsconfig.json b/src/electron/tsconfig.json index 430f6d3..52bcaf4 100644 --- a/src/electron/tsconfig.json +++ b/src/electron/tsconfig.json @@ -1,7 +1,11 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "outDir": "../../dist/electron" + "outDir": "../../dist/electron", + "composite": true, + "declaration": true, + "baseUrl": "../" }, + "references": [{ "path": "../shared" }], "include": ["./**/*.ts"] }