feat: adapt electron part
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import { app, BrowserWindow } from 'electron';
|
import { app, BrowserWindow, ipcMain } from 'electron';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as url from 'url';
|
import * as url from 'url';
|
||||||
|
import { AbstractService } from '../services/abstract-service';
|
||||||
|
import { MultiplesService } from '../services/multiples-service';
|
||||||
|
|
||||||
export class Window {
|
export class Window {
|
||||||
private _window: BrowserWindow | any;
|
private _window: BrowserWindow | any;
|
||||||
@@ -12,6 +14,7 @@ export class Window {
|
|||||||
|
|
||||||
this.createWindow();
|
this.createWindow();
|
||||||
this.loadRender();
|
this.loadRender();
|
||||||
|
this.registerService(MultiplesService);
|
||||||
}
|
}
|
||||||
|
|
||||||
private createWindow(): void {
|
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 {
|
public get window(): BrowserWindow | any {
|
||||||
return this._window;
|
return this._window;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.');
|
console.log('The preload script has been injected successfully.');
|
||||||
|
|||||||
13
src/electron/services/abstract-service.ts
Normal file
13
src/electron/services/abstract-service.ts
Normal file
@@ -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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/electron/services/multiples-service.ts
Normal file
21
src/electron/services/multiples-service.ts
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
{
|
{
|
||||||
"extends": "../../tsconfig.json",
|
"extends": "../../tsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"outDir": "../../dist/electron"
|
"outDir": "../../dist/electron",
|
||||||
|
"composite": true,
|
||||||
|
"declaration": true,
|
||||||
|
"baseUrl": "../"
|
||||||
},
|
},
|
||||||
|
"references": [{ "path": "../shared" }],
|
||||||
"include": ["./**/*.ts"]
|
"include": ["./**/*.ts"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user