| name | debugging-ipc |
| description | Troubleshooting guide for IPC communication issues between Main and Renderer processes Use when this capability is needed. |
| metadata | {"author":"eretica"} |
IPC通信のデバッグ
このガイドは、メインプロセスとレンダラープロセス間のプロセス間通信(IPC)の問題をデバッグするのに役立ちます。
一般的な問題
1. "Cannot read property of undefined" (window.api)
症状:レンダラープロセスでwindow.apiにアクセスするとエラーが発生する。
原因:プリロードスクリプトが読み込まれていない、またはコンテキストブリッジが公開されていない。
解決方法:
const window = new BrowserWindow({
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
contextIsolation: true,
nodeIntegration: false,
},
});
import { contextBridge, ipcRenderer } from 'electron';
contextBridge.exposeInMainWorld('api', {
listRepositories: () => ipcRenderer.invoke('repo:list'),
});
2. IPCハンドラが応答しない
症状:ipcRenderer.invoke()がハングまたは拒否される。
原因:ハンドラが登録されていない、またはチャネル名が一致しない。
解決方法:
export const IPC_CHANNELS = {
REPO_LIST: 'repo:list',
} as const;
import { IPC_CHANNELS } from '../shared/constants';
ipcMain.handle(IPC_CHANNELS.REPO_LIST, async () => { ... });
import { IPC_CHANNELS } from '../shared/constants';
api: {
listRepositories: () => ipcRenderer.invoke(IPC_CHANNELS.REPO_LIST),
}
import { setupIpcHandlers } from './ipc';
app.whenReady().then(() => {
setupIpcHandlers();
createWindow();
});
3. レンダラーでの型エラー
症状:window.apiメソッドを呼び出すとTypeScriptエラーが発生する。
原因:型定義が欠落または不正確。
解決方法:
import type { IpcApi } from '../preload';
declare global {
interface Window {
api: IpcApi;
}
}
export interface IpcApi {
listRepositories: () => Promise<Repository[]>;
addRepository: () => Promise<Repository | null>;
}
contextBridge.exposeInMainWorld('api', {
listRepositories: () => ipcRenderer.invoke('repo:list'),
addRepository: () => ipcRenderer.invoke('repo:add'),
} as IpcApi);
4. データベースクエリエラー
症状:IPCハンドラが"table not found"または"column not found"をスローする。
原因:マイグレーションが実行されていない、またはスキーマが同期されていない。
解決方法:
pnpm db:generate
rm ~/Library/Application\ Support/github-pr-reminder/github-pr-reminder.db
pnpm dev
console.log('Running migrations...');
migrate(db, { migrationsFolder });
console.log('Migrations completed');
デバッグ技術
1. コンソールログ
ipcMain.handle('repo:list', async (): Promise<Repository[]> => {
console.log('[Main] repo:list called');
try {
const repo = new RepositoryRepository(getDatabase());
const result = await repo.findAll();
console.log('[Main] repo:list result:', result);
return result;
} catch (error) {
console.error('[Main] repo:list error:', error);
throw error;
}
});
export function useRepositories() {
useEffect(() => {
console.log('[Renderer] Fetching repositories...');
window.api.listRepositories()
.then(repos => {
console.log('[Renderer] Repositories fetched:', repos);
setRepositories(repos);
})
.catch( {
.(, error);
(error);
});
}, []);
}
2. DevTools
if (process.env.NODE_ENV === 'development') {
window.webContents.openDevTools();
}
3. ネットワーク検査(IPC用)
IPC呼び出しはNetworkタブに表示されませんが、Electronの組み込みIPCインスペクターを使用できます:
if (process.env.NODE_ENV === 'development') {
app.on('web-contents-created', (_, contents) => {
contents.on('ipc-message', (event, channel, ...args) => {
console.log('[IPC]', channel, args);
});
});
}
4. データベース検査
Drizzle Studioを使用してデータベースを検査します:
pnpm db:studio
またはSQLite CLIを使用します:
sqlite3 ~/Library/Application\ Support/github-pr-reminder/github-pr-reminder.db
sqlite> .tables
sqlite> SELECT * FROM repositories;
IPCハンドラのテスト
IPCハンドラの統合テストを作成します:
import { ipcMain } from 'electron';
import { setupIpcHandlers } from './ipc';
describe('IPC Handlers', () => {
beforeEach(() => {
setupIpcHandlers();
});
it('should handle repo:list', async () => {
const handler = ipcMain.handle as jest.Mock;
const repoListHandler = handler.mock.calls.find(
call => call[0] === 'repo:list'
)[1];
const result = await repoListHandler();
expect(Array.isArray(result)).toBe(true);
});
});
よくある落とし穴
シリアライゼーションの問題
IPC通信はデータをシリアライズします - 一部の型は境界を越えられません:
window.api.getData().then(data => {
data.doSomething();
});
interface Data {
id: string;
value: number;
}
window.api.getData().then((data: Data) => {
});
非同期ハンドラの問題
ipcMain.handle('data:get', () => {
return fetchData();
});
ipcMain.handle('data:get', async () => {
return await fetchData();
});
IPC問題のチェックリスト
Converted and distributed by TomeVault — claim your Tome and manage your conversions.