Skip to content

Commit 559bd7b

Browse files
committed
#1 - Refactored code
Signed-off-by: Gregor Anders <[email protected]>
1 parent 199cfe6 commit 559bd7b

File tree

6 files changed

+70
-72
lines changed

6 files changed

+70
-72
lines changed

src/ipc.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { ipcMain, ipcRenderer } from "electron";
22

3-
export const mainChannel: string = "main-channel";
4-
5-
export const renderChannel: string = "render-channel";
6-
73
export type IPCMessageType = "ping" | "pong";
84

5+
export type IPCChannel = "main-channel" | "render-channel";
6+
97
export interface IIPCMessage<T> {
108
data: T;
119
timestamp: Date;

src/ipcService.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as log from "fancy-log";
2+
3+
import { IIPCMessage, IPCChannel, IPCMessageType } from "./ipc";
4+
5+
export abstract class IpcService<T extends Electron.IpcMainEvent | Electron.IpcRendererEvent> {
6+
7+
public register(ipc: NodeJS.EventEmitter, channel: string): void {
8+
ipc.on(channel, (event: T, ...args: any[]): void => {
9+
if (args && args.length === 1) {
10+
const ipcMessage: IIPCMessage<any> = args[0];
11+
this.handleMessage(event, ipcMessage);
12+
} else {
13+
log.warn(event, args);
14+
}
15+
});
16+
}
17+
18+
protected abstract handleMessage<MT>(event: T, message: IIPCMessage<MT>): void;
19+
20+
protected send<MT>(sender: Electron.WebContents | Electron.IpcRenderer,
21+
channel: IPCChannel, type: IPCMessageType, data: MT): void {
22+
sender.send(channel, {
23+
data,
24+
timestamp: new Date(),
25+
type,
26+
} as IIPCMessage<MT>);
27+
}
28+
}

src/main.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { app, BrowserWindow, dialog, ipcMain, ipcRenderer } from "electron";
1+
import { app, BrowserWindow, dialog, ipcMain } from "electron";
22

33
import { MainService } from "./mainService";
44

@@ -19,24 +19,22 @@ app.whenReady()
1919
},
2020
});
2121

22-
if (browserWindow) {
23-
browserWindow.hide();
24-
browserWindow.setMenuBarVisibility(false);
25-
browserWindow.loadFile(path.join(__dirname, "index.html"))
26-
.then((result: void): void => {
27-
if (debug) {
28-
browserWindow.webContents.openDevTools({
29-
mode: "right",
30-
});
31-
}
32-
const service: MainService = new MainService();
33-
service.register();
34-
browserWindow.show();
35-
})
36-
.catch((reason: any): void => {
37-
dialog.showErrorBox("Error", JSON.stringify(reason));
38-
});
39-
}
22+
browserWindow.hide();
23+
browserWindow.setMenuBarVisibility(false);
24+
browserWindow.loadFile(path.join(__dirname, "index.html"))
25+
.then((result: void): void => {
26+
if (debug) {
27+
browserWindow.webContents.openDevTools({
28+
mode: "right",
29+
});
30+
}
31+
const service: MainService = new MainService();
32+
service.register(ipcMain, "main-channel");
33+
browserWindow.show();
34+
})
35+
.catch((reason: any): void => {
36+
dialog.showErrorBox("Error", JSON.stringify(reason));
37+
});
4038
}).catch((reason: any): void => {
4139
log.error(reason);
4240
});

src/mainService.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1+
import { ipcMain } from "electron";
2+
13
import * as log from "fancy-log";
24

3-
import { ipcMain } from "electron";
5+
import { IpcService } from "./ipcService";
46

5-
import { IIPCMessage, IPCMessageType, mainChannel, renderChannel } from "./ipc";
7+
import { IIPCMessage } from "./ipc";
68

7-
export class MainService {
89

9-
public register(): void {
10-
ipcMain.on(mainChannel, (event: Electron.IpcMainEvent, ...args: any[]): void => {
11-
if (args && args.length === 1) {
12-
const ipcMessage: IIPCMessage<any> = args[0];
13-
this.handleMainChannel(event, ipcMessage);
14-
} else {
15-
log.warn(event, args);
16-
}
17-
});
10+
export class MainService extends IpcService<Electron.IpcMainEvent> {
11+
12+
public constructor() {
13+
super();
14+
this.register(ipcMain, "main-channel");
1815
}
1916

20-
private handleMainChannel<T>(event: Electron.IpcMainEvent, message: IIPCMessage<T>): void {
17+
protected handleMessage<T>(event: Electron.IpcMainEvent, message: IIPCMessage<T>): void {
2118
switch (message.type) {
2219
case "ping":
2320
log.info(message);
24-
this.send(event.sender, "pong", message.data);
25-
this.send(event.sender, "ping", message.data);
21+
this.send(event.sender, "main-channel", "pong", message.data);
22+
this.send(event.sender, "main-channel", "ping", message.data);
2623
break;
2724
case "pong":
2825
log.info(message);
@@ -31,12 +28,4 @@ export class MainService {
3128
log.warn(message);
3229
}
3330
}
34-
35-
private send<T>(sender: Electron.WebContents, type: IPCMessageType, data: T): void {
36-
sender.send(renderChannel, {
37-
data,
38-
timestamp: new Date(),
39-
type,
40-
} as IIPCMessage<T>);
41-
}
4231
}

src/renderer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import * as React from "react";
22
import { render } from "react-dom";
33

4+
import { ipcRenderer } from "electron";
5+
46
import { Application } from "./application";
57

68
import { RendererService } from "./rendererService";
79

810
const service: RendererService = new RendererService();
9-
service.register();
11+
service.register(ipcRenderer, "render-channel");
1012

1113
render(<Application service={ service }/>, document.getElementById("content"));

src/rendererService.ts

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
import { ipcMain, ipcRenderer } from "electron";
1+
import { ipcRenderer } from "electron";
22

3-
import { IIPCMessage, IPCMessageType, mainChannel, renderChannel } from "./ipc";
3+
import { IIPCMessage } from "./ipc";
44

5-
export class RendererService {
5+
import { IpcService } from "./ipcService";
66

7-
public ping(): void {
8-
this.send("ping", [1, 2, 3]);
9-
}
7+
export class RendererService extends IpcService<Electron.IpcRendererEvent> {
108

11-
public register(): void {
12-
ipcRenderer.on(renderChannel, (event: Electron.IpcRendererEvent, ...args: any[]): void => {
13-
if (args && args.length === 1) {
14-
const ipcMessage: IIPCMessage<any> = args[0];
15-
this.handleRenderChannel(event, ipcMessage);
16-
} else {
17-
console.log(event, args);
18-
}
19-
});
9+
public ping(): void {
10+
this.send(ipcRenderer, "render-channel", "ping", [1, 2, 3]);
2011
}
2112

22-
private handleRenderChannel<T>(event: Electron.IpcRendererEvent, message: IIPCMessage<T>): void {
13+
protected handleMessage<MT>(event: Electron.IpcRendererEvent, message: IIPCMessage<MT>): void {
2314
switch (message.type) {
2415
case "ping":
2516
console.log(message);
26-
this.send("pong", message.data);
17+
this.send(event.sender, "render-channel", "pong", message.data);
2718
break;
2819
case "pong":
2920
console.log(message);
@@ -32,12 +23,4 @@ export class RendererService {
3223
console.log(message);
3324
}
3425
}
35-
36-
private send<T>(type: IPCMessageType, data: T): void {
37-
ipcRenderer.send(mainChannel, {
38-
data,
39-
timestamp: new Date(),
40-
type,
41-
} as IIPCMessage<T>);
42-
}
4326
}

0 commit comments

Comments
 (0)