Skip to content

Commit a3ec50f

Browse files
committed
feat: update ipc dispatch type signatures
1 parent 4dc731a commit a3ec50f

File tree

6 files changed

+56
-20
lines changed

6 files changed

+56
-20
lines changed

electron/common/game/game.types.ts

+24
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,27 @@ export enum ExperienceMindState {
354354
NEARLY_LOCKED = 33,
355355
MIND_LOCK = 34,
356356
}
357+
358+
export interface GameConnectMessage {
359+
accountName: string;
360+
characterName: string;
361+
gameCode: string;
362+
}
363+
364+
export interface GameDisconnectMessage {
365+
accountName: string;
366+
characterName: string;
367+
gameCode: string;
368+
}
369+
370+
export interface GameErrorMessage {
371+
error: Error;
372+
}
373+
374+
export interface GameEventMessage {
375+
gameEvent: GameEvent;
376+
}
377+
378+
export interface GameCommandMessage {
379+
command: string;
380+
}

electron/main/app.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import { BrowserWindow, app, shell } from 'electron';
33
import * as path from 'node:path';
44
import serve from 'electron-serve';
55
import { runInBackground } from '../common/async';
6-
import type { IpcController } from './ipc';
6+
import type { IpcController, IpcDispatcher } from './ipc';
77
import { newIpcController } from './ipc';
88
import { createLogger } from './logger';
99
import { initializeMenu } from './menu';
1010
import { PreferenceKey, Preferences } from './preference';
11-
import type { Dispatcher } from './types';
1211

1312
app.setName('Phoenix');
1413
app.setAppUserModelId('com.github.dragonrealms-phoenix.phoenix');
@@ -99,7 +98,7 @@ const createMainWindow = async (): Promise<void> => {
9998
mainWindow.show();
10099
});
101100

102-
const dispatch: Dispatcher = (channel, ...args): void => {
101+
const dispatch: IpcDispatcher = (channel, ...args): void => {
103102
// When the window is closed or destroyed, we might still
104103
// receive async events from the ipc controller. Ignore them.
105104
// This usually happens when the app is quit while a game is being played.

electron/main/ipc/ipc.controller.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { Game } from '../game';
55
import { createLogger } from '../logger';
66
import type { SGEGameCode } from '../sge';
77
import { SGEServiceImpl } from '../sge';
8-
import type { Dispatcher } from '../types';
98
import type {
9+
IpcDispatcher,
1010
IpcHandlerRegistry,
1111
IpcInvokableEvent,
1212
IpcInvokeHandler,
@@ -16,12 +16,12 @@ import type {
1616
const logger = createLogger('ipc:controller');
1717

1818
export class IpcController {
19-
private dispatch: Dispatcher;
19+
private dispatch: IpcDispatcher;
2020
private accountService: AccountService;
2121
private ipcHandlerRegistry: IpcHandlerRegistry;
2222

2323
constructor(options: {
24-
dispatch: Dispatcher;
24+
dispatch: IpcDispatcher;
2525
accountService: AccountService;
2626
}) {
2727
this.dispatch = options.dispatch;
@@ -177,11 +177,7 @@ export class IpcController {
177177
});
178178

179179
const credentials = await sgeService.loginCharacter(characterName);
180-
181-
const gameInstance = await Game.newInstance({
182-
credentials,
183-
});
184-
180+
const gameInstance = await Game.newInstance({ credentials });
185181
const gameEvents$ = await gameInstance.connect();
186182

187183
this.dispatch('game:connect', {
@@ -194,11 +190,11 @@ export class IpcController {
194190
gameEvents$.subscribe({
195191
next: (gameEvent) => {
196192
logger.debug('game service stream event', { gameEvent });
197-
this.dispatch('game:event', gameEvent);
193+
this.dispatch('game:event', { gameEvent });
198194
},
199195
error: (error) => {
200196
logger.error('game service stream error', { error });
201-
this.dispatch('game:error', error);
197+
this.dispatch('game:error', { error });
202198
},
203199
complete: () => {
204200
logger.debug('game service stream completed');
@@ -221,7 +217,7 @@ export class IpcController {
221217
const gameInstance = Game.getInstance();
222218

223219
if (gameInstance) {
224-
this.dispatch('game:command', command);
220+
this.dispatch('game:command', { command });
225221
gameInstance.send(command);
226222
} else {
227223
throw new Error('[IPC:SEND_COMMAND:ERROR:GAME_INSTANCE_NOT_FOUND]');

electron/main/ipc/ipc.types.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import type {
2+
GameCommandMessage,
3+
GameConnectMessage,
4+
GameDisconnectMessage,
5+
GameErrorMessage,
6+
GameEventMessage,
7+
} from '../../common/game';
8+
19
/**
210
* Defines the IPC API exposed to the renderer process.
311
* The main process must provide call-response handlers for this API.
@@ -21,3 +29,16 @@ export type IpcSgeCharacter = {
2129
accountName: string;
2230
characterName: string;
2331
};
32+
33+
/**
34+
* Defines the channels and message types that can be dispatched
35+
* from the main process to the renderer process.
36+
*/
37+
export type IpcDispatcher = {
38+
(channel: 'pong', message: 'pong'): void;
39+
(channel: 'game:connect', message: GameConnectMessage): void;
40+
(channel: 'game:disconnect', message: GameDisconnectMessage): void;
41+
(channel: 'game:error', message: GameErrorMessage): void;
42+
(channel: 'game:event', message: GameEventMessage): void;
43+
(channel: 'game:command', message: GameCommandMessage): void;
44+
};

electron/main/ipc/ipc.utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { AccountService } from '../account';
22
import { AccountServiceImpl } from '../account';
33
import { Store } from '../store';
4-
import type { Dispatcher } from '../types';
54
import { IpcController } from './ipc.controller';
5+
import type { IpcDispatcher } from './ipc.types';
66

77
/**
88
* I didn't like the app nor controller needing to know about
@@ -12,7 +12,7 @@ import { IpcController } from './ipc.controller';
1212
* use this method or use the IpController constructor directly.
1313
*/
1414
export function newIpcController(options: {
15-
dispatch: Dispatcher;
15+
dispatch: IpcDispatcher;
1616
accountService?: AccountService;
1717
}): IpcController {
1818
const {

electron/main/types.ts

-4
This file was deleted.

0 commit comments

Comments
 (0)