Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when calling .destroy() on a BrowserWindow #197

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-pandas-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'electron-trpc': patch
---

Fix a crash when calling `.destroy()` on a BrowserWindow.
15 changes: 10 additions & 5 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,23 +83,24 @@ class IPCHandler<TRouter extends AnyRouter> {
}

#attachSubscriptionCleanupHandlers(win: BrowserWindow) {
const webContentsId = win.webContents.id;
win.webContents.on('did-start-navigation', ({ isSameDocument, frame }) => {
// Check if it's a hard navigation
if (!isSameDocument) {
debug(
'Handling hard navigation event',
`webContentsId: ${win.webContents.id}`,
`webContentsId: ${webContentsId}`,
`frameRoutingId: ${frame.routingId}`
);
this.#cleanUpSubscriptions({
webContentsId: win.webContents.id,
webContentsId: webContentsId,
frameRoutingId: frame.routingId,
});
}
});
win.webContents.on('destroyed', () => {
debug('Handling webContents `destroyed` event');
this.detachWindow(win);
this.detachWindow(win, webContentsId);
});
}
}
Expand Down
Loading