Skip to content

Commit 1687ef2

Browse files
committed
feat: abstract game disconnect in ipc destroy fn
1 parent afeda7f commit 1687ef2

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

electron/main/ipc/ipc.controller.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ export class IpcController {
2828
this.dispatch = options.dispatch;
2929
this.accountService = options.accountService;
3030
this.ipcHandlerRegistry = this.createIpcHandlerRegistry();
31+
this.registerHandlers(this.ipcHandlerRegistry);
32+
}
33+
34+
/**
35+
* Unregisters all ipc handlers and disconnects from the game server.
36+
*/
37+
public async destroy(): Promise<void> {
38+
Object.keys(this.ipcHandlerRegistry).forEach((channel) => {
39+
ipcMain.removeHandler(channel);
40+
});
41+
await Game.getInstance()?.disconnect();
3142
}
3243

3344
private createIpcHandlerRegistry(): IpcHandlerRegistry {
@@ -43,9 +54,9 @@ export class IpcController {
4354
};
4455
}
4556

46-
public registerHandlers(): void {
47-
Object.keys(this.ipcHandlerRegistry).forEach((channel) => {
48-
const handler = this.ipcHandlerRegistry[channel as IpcInvokableEvent];
57+
private registerHandlers(registry: IpcHandlerRegistry): void {
58+
Object.keys(registry).forEach((channel) => {
59+
const handler = registry[channel as IpcInvokableEvent];
4960

5061
if (!handler) {
5162
logger.error('no handler registered for channel', { channel });
@@ -68,13 +79,6 @@ export class IpcController {
6879
});
6980
}
7081

71-
public deregisterHandlers(): void {
72-
Object.keys(this.ipcHandlerRegistry).forEach((channel) => {
73-
ipcMain.removeHandler(channel);
74-
ipcMain.removeAllListeners(channel);
75-
});
76-
}
77-
7882
private pingHandler: IpcInvokeHandler<'ping'> = async (): Promise<string> => {
7983
return 'pong';
8084
};

0 commit comments

Comments
 (0)