Skip to content

Commit

Permalink
Fix crash when calling .destroy() on a BrowserWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
dguenther committed May 28, 2024
1 parent cfdb2e5 commit 7cf07e2
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/electron-trpc/src/main/createIPCHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ class IPCHandler<TRouter extends AnyRouter> {
this.#attachSubscriptionCleanupHandlers(win);
}

detachWindow(win: BrowserWindow) {
detachWindow(win: BrowserWindow, webContentsId?: number) {
debug('Detaching window', win.id);

if (win.isDestroyed() && webContentsId === undefined) {
throw new Error('webContentsId is required when calling detachWindow on a destroyed window');
}

this.#windows = this.#windows.filter((w) => w !== win);
this.#cleanUpSubscriptions({ webContentsId: win.webContents.id });
this.#cleanUpSubscriptions({ webContentsId: webContentsId ?? win.webContents.id });
}

#cleanUpSubscriptions({
Expand All @@ -79,6 +83,7 @@ class IPCHandler<TRouter extends AnyRouter> {
}

#attachSubscriptionCleanupHandlers(win: BrowserWindow) {
const webContentsId = win.webContents.id;
win.webContents.on('did-start-navigation', ({ frame }) => {
debug(
'Handling webContents `did-start-navigation` event',
Expand All @@ -92,7 +97,7 @@ class IPCHandler<TRouter extends AnyRouter> {
});
win.webContents.on('destroyed', () => {
debug('Handling webContents `destroyed` event');
this.detachWindow(win);
this.detachWindow(win, webContentsId);
});
}
}
Expand Down

0 comments on commit 7cf07e2

Please sign in to comment.