Skip to content

Commit

Permalink
feat: update ipc dispatch type signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
KatoakDR committed Jan 22, 2024
1 parent 4dc731a commit a3ec50f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
24 changes: 24 additions & 0 deletions electron/common/game/game.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,27 @@ export enum ExperienceMindState {
NEARLY_LOCKED = 33,
MIND_LOCK = 34,
}

export interface GameConnectMessage {
accountName: string;
characterName: string;
gameCode: string;
}

export interface GameDisconnectMessage {
accountName: string;
characterName: string;
gameCode: string;
}

export interface GameErrorMessage {
error: Error;
}

export interface GameEventMessage {
gameEvent: GameEvent;
}

export interface GameCommandMessage {
command: string;
}
5 changes: 2 additions & 3 deletions electron/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { BrowserWindow, app, shell } from 'electron';
import * as path from 'node:path';
import serve from 'electron-serve';
import { runInBackground } from '../common/async';
import type { IpcController } from './ipc';
import type { IpcController, IpcDispatcher } from './ipc';
import { newIpcController } from './ipc';
import { createLogger } from './logger';
import { initializeMenu } from './menu';
import { PreferenceKey, Preferences } from './preference';
import type { Dispatcher } from './types';

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

const dispatch: Dispatcher = (channel, ...args): void => {
const dispatch: IpcDispatcher = (channel, ...args): void => {
// When the window is closed or destroyed, we might still
// receive async events from the ipc controller. Ignore them.
// This usually happens when the app is quit while a game is being played.
Expand Down
18 changes: 7 additions & 11 deletions electron/main/ipc/ipc.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Game } from '../game';
import { createLogger } from '../logger';
import type { SGEGameCode } from '../sge';
import { SGEServiceImpl } from '../sge';
import type { Dispatcher } from '../types';
import type {
IpcDispatcher,
IpcHandlerRegistry,
IpcInvokableEvent,
IpcInvokeHandler,
Expand All @@ -16,12 +16,12 @@ import type {
const logger = createLogger('ipc:controller');

export class IpcController {
private dispatch: Dispatcher;
private dispatch: IpcDispatcher;
private accountService: AccountService;
private ipcHandlerRegistry: IpcHandlerRegistry;

constructor(options: {
dispatch: Dispatcher;
dispatch: IpcDispatcher;
accountService: AccountService;
}) {
this.dispatch = options.dispatch;
Expand Down Expand Up @@ -177,11 +177,7 @@ export class IpcController {
});

const credentials = await sgeService.loginCharacter(characterName);

const gameInstance = await Game.newInstance({
credentials,
});

const gameInstance = await Game.newInstance({ credentials });
const gameEvents$ = await gameInstance.connect();

this.dispatch('game:connect', {
Expand All @@ -194,11 +190,11 @@ export class IpcController {
gameEvents$.subscribe({
next: (gameEvent) => {
logger.debug('game service stream event', { gameEvent });
this.dispatch('game:event', gameEvent);
this.dispatch('game:event', { gameEvent });
},
error: (error) => {
logger.error('game service stream error', { error });
this.dispatch('game:error', error);
this.dispatch('game:error', { error });
},
complete: () => {
logger.debug('game service stream completed');
Expand All @@ -221,7 +217,7 @@ export class IpcController {
const gameInstance = Game.getInstance();

if (gameInstance) {
this.dispatch('game:command', command);
this.dispatch('game:command', { command });
gameInstance.send(command);
} else {
throw new Error('[IPC:SEND_COMMAND:ERROR:GAME_INSTANCE_NOT_FOUND]');
Expand Down
21 changes: 21 additions & 0 deletions electron/main/ipc/ipc.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import type {
GameCommandMessage,
GameConnectMessage,
GameDisconnectMessage,
GameErrorMessage,
GameEventMessage,
} from '../../common/game';

/**
* Defines the IPC API exposed to the renderer process.
* The main process must provide call-response handlers for this API.
Expand All @@ -21,3 +29,16 @@ export type IpcSgeCharacter = {
accountName: string;
characterName: string;
};

/**
* Defines the channels and message types that can be dispatched
* from the main process to the renderer process.
*/
export type IpcDispatcher = {
(channel: 'pong', message: 'pong'): void;
(channel: 'game:connect', message: GameConnectMessage): void;
(channel: 'game:disconnect', message: GameDisconnectMessage): void;
(channel: 'game:error', message: GameErrorMessage): void;
(channel: 'game:event', message: GameEventMessage): void;
(channel: 'game:command', message: GameCommandMessage): void;
};
4 changes: 2 additions & 2 deletions electron/main/ipc/ipc.utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { AccountService } from '../account';
import { AccountServiceImpl } from '../account';
import { Store } from '../store';
import type { Dispatcher } from '../types';
import { IpcController } from './ipc.controller';
import type { IpcDispatcher } from './ipc.types';

/**
* I didn't like the app nor controller needing to know about
Expand All @@ -12,7 +12,7 @@ import { IpcController } from './ipc.controller';
* use this method or use the IpController constructor directly.
*/
export function newIpcController(options: {
dispatch: Dispatcher;
dispatch: IpcDispatcher;
accountService?: AccountService;
}): IpcController {
const {
Expand Down
4 changes: 0 additions & 4 deletions electron/main/types.ts

This file was deleted.

0 comments on commit a3ec50f

Please sign in to comment.