Skip to content

Commit

Permalink
Refactor IPC handlers (#197)
Browse files Browse the repository at this point in the history
* refactor IPC handler.

* Refactor.

* Test IPC hander.

* Fix imports.

* Fix tests.

* Fix tests.
  • Loading branch information
robinjhuang authored Nov 10, 2024
1 parent 88084c4 commit 94ce295
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 29 deletions.
23 changes: 23 additions & 0 deletions src/handlers/appInfoHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { app, ipcMain, shell } from 'electron';
import { IPC_CHANNELS } from '../constants';
/**
* Handles static information about the app in IPC channels.
*/
export class AppInfoHandlers {
constructor() {}

registerHandlers() {
ipcMain.handle(IPC_CHANNELS.IS_PACKAGED, () => {
return app.isPackaged;
});

ipcMain.handle(IPC_CHANNELS.GET_ELECTRON_VERSION, () => {
return app.getVersion();
});

ipcMain.handle(IPC_CHANNELS.OPEN_FORUM, () => {
shell.openExternal('https://forum.comfy.org');
});
ipcMain.handle(IPC_CHANNELS.DEFAULT_INSTALL_LOCATION, () => app.getPath('documents'));
}
}
28 changes: 28 additions & 0 deletions src/handlers/pathHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { app, ipcMain, shell } from 'electron';
import { IPC_CHANNELS } from '../constants';
import log from 'electron-log/main';
import { getModelConfigPath } from '../config/extra_model_config';
import { getBasePath } from '../install/resourcePaths';

export class PathHandlers {
constructor() {}

registerHandlers() {
ipcMain.on(IPC_CHANNELS.OPEN_LOGS_PATH, (): void => {
shell.openPath(app.getPath('logs'));
});

ipcMain.handle(IPC_CHANNELS.GET_MODEL_CONFIG_PATH, (): string => {
return getModelConfigPath();
});

ipcMain.handle(IPC_CHANNELS.GET_BASE_PATH, async (): Promise<string | null> => {
return getBasePath();
});

ipcMain.on(IPC_CHANNELS.OPEN_PATH, (event, folderPath: string): void => {
log.info(`Opening path: ${folderPath}`);
shell.openPath(folderPath);
});
}
}
36 changes: 7 additions & 29 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { buildMenu } from './menu/menu';
import { ComfyConfigManager } from './config/comfyConfigManager';
import { AppWindow } from './main-process/appWindow';
import { getAppResourcesPath, getBasePath, getPythonInstallPath } from './install/resourcePaths';
import { PathHandlers } from './handlers/pathHandlers';
import { AppInfoHandlers } from './handlers/appInfoHandlers';

dotenv.config();

Expand Down Expand Up @@ -140,45 +142,25 @@ if (!gotTheLock) {
log.error('Error getting GPU info: ', e);
});

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.

app.on('ready', async () => {
log.info('App ready');

try {
createWindow();
new PathHandlers().registerHandlers();
new AppInfoHandlers().registerHandlers();

ipcMain.handle(IPC_CHANNELS.OPEN_FORUM, () => {
shell.openExternal('https://forum.comfy.org');
});
ipcMain.handle(IPC_CHANNELS.DEFAULT_INSTALL_LOCATION, () => app.getPath('documents'));
ipcMain.handle(IPC_CHANNELS.OPEN_DIALOG, (event, options: Electron.OpenDialogOptions) => {
log.info('Open dialog');
return dialog.showOpenDialogSync({
...options,
});
});
ipcMain.on(IPC_CHANNELS.OPEN_LOGS_PATH, () => {
shell.openPath(app.getPath('logs'));
});
ipcMain.handle(IPC_CHANNELS.GET_BASE_PATH, async () => {
return await getBasePath();
});
ipcMain.handle(IPC_CHANNELS.GET_MODEL_CONFIG_PATH, () => {
return getModelConfigPath();
});
ipcMain.on(IPC_CHANNELS.OPEN_PATH, (event, folderPath: string) => {
log.info(`Opening path: ${folderPath}`);
shell.openPath(folderPath);
});

ipcMain.on(IPC_CHANNELS.OPEN_DEV_TOOLS, () => {
appWindow.openDevTools();
});
ipcMain.handle(IPC_CHANNELS.IS_PACKAGED, () => {
return app.isPackaged;
});

await handleFirstTimeSetup();
const basePath = await getBasePath();
const pythonInstallPath = await getPythonInstallPath();
Expand Down Expand Up @@ -237,10 +219,6 @@ if (!gotTheLock) {
}
);

ipcMain.handle(IPC_CHANNELS.GET_ELECTRON_VERSION, () => {
return app.getVersion();
});

ipcMain.handle(IPC_CHANNELS.SEND_ERROR_TO_SENTRY, async (_event, { error, extras }): Promise<string | null> => {
try {
return Sentry.captureMessage(error, {
Expand Down Expand Up @@ -583,7 +561,7 @@ function isFirstTimeSetup(): boolean {
async function selectedInstallDirectory(): Promise<string> {
return new Promise((resolve, reject) => {
ipcMain.on(IPC_CHANNELS.SELECTED_DIRECTORY, (_event, value) => {
log.info('Directory selected:', value);
log.info('User selected to install ComfyUI in:', value);
resolve(value);
});
});
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/handlers/appinfoHandlers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ipcMain } from 'electron';
import { AppInfoHandlers } from '../../../src/handlers/appInfoHandlers';
import { IPC_CHANNELS } from '../../../src/constants';

jest.mock('electron', () => ({
ipcMain: {
handle: jest.fn(),
},
}));

describe('AppInfoHandlers', () => {
let handler: AppInfoHandlers;
beforeEach(() => {
handler = new AppInfoHandlers();
handler.registerHandlers();
});

it('should register all expected handle channels', () => {
const expectedChannels = [
IPC_CHANNELS.IS_PACKAGED,
IPC_CHANNELS.GET_ELECTRON_VERSION,
IPC_CHANNELS.OPEN_FORUM,
IPC_CHANNELS.DEFAULT_INSTALL_LOCATION,
];

expectedChannels.forEach((channel) => {
expect(ipcMain.handle).toHaveBeenCalledWith(channel, expect.any(Function));
});
});
});
35 changes: 35 additions & 0 deletions tests/unit/handlers/pathHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ipcMain } from 'electron';

import { PathHandlers } from '../../../src/handlers/pathHandlers';
import { IPC_CHANNELS } from '../../../src/constants';

jest.mock('electron', () => ({
ipcMain: {
on: jest.fn(),
handle: jest.fn(),
},
}));

describe('PathHandlers', () => {
let handler: PathHandlers;
beforeEach(() => {
handler = new PathHandlers();
handler.registerHandlers();
});

it('should register all expected handle channels', () => {
const expectedChannelsForHandle = [IPC_CHANNELS.GET_MODEL_CONFIG_PATH, IPC_CHANNELS.GET_BASE_PATH];

expectedChannelsForHandle.forEach((channel) => {
expect(ipcMain.handle).toHaveBeenCalledWith(channel, expect.any(Function));
});
});

it('should register all expected on channels', () => {
const expectedChannelsForOn = [IPC_CHANNELS.OPEN_LOGS_PATH, IPC_CHANNELS.OPEN_PATH];

expectedChannelsForOn.forEach((channel) => {
expect(ipcMain.on).toHaveBeenCalledWith(channel, expect.any(Function));
});
});
});

0 comments on commit 94ce295

Please sign in to comment.