Skip to content

Commit 6bf4183

Browse files
dguentherjsonnull
authored andcommitted
Fix crash when calling .destroy() on a BrowserWindow
1 parent bf2c1a4 commit 6bf4183

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

.changeset/giant-pandas-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'electron-trpc': patch
3+
---
4+
5+
Fix a crash when calling `.destroy()` on a BrowserWindow.

packages/electron-trpc/src/main/createIPCHandler.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ class IPCHandler<TRouter extends AnyRouter> {
5555
this.#attachSubscriptionCleanupHandlers(win);
5656
}
5757

58-
detachWindow(win: BrowserWindow) {
58+
detachWindow(win: BrowserWindow, webContentsId?: number) {
5959
debug('Detaching window', win.id);
6060

61+
if (win.isDestroyed() && webContentsId === undefined) {
62+
throw new Error('webContentsId is required when calling detachWindow on a destroyed window');
63+
}
64+
6165
this.#windows = this.#windows.filter((w) => w !== win);
62-
this.#cleanUpSubscriptions({ webContentsId: win.webContents.id });
66+
this.#cleanUpSubscriptions({ webContentsId: webContentsId ?? win.webContents.id });
6367
}
6468

6569
#cleanUpSubscriptions({
@@ -79,23 +83,24 @@ class IPCHandler<TRouter extends AnyRouter> {
7983
}
8084

8185
#attachSubscriptionCleanupHandlers(win: BrowserWindow) {
86+
const webContentsId = win.webContents.id;
8287
win.webContents.on('did-start-navigation', ({ isSameDocument, frame }) => {
8388
// Check if it's a hard navigation
8489
if (!isSameDocument) {
8590
debug(
8691
'Handling hard navigation event',
87-
`webContentsId: ${win.webContents.id}`,
92+
`webContentsId: ${webContentsId}`,
8893
`frameRoutingId: ${frame.routingId}`
8994
);
9095
this.#cleanUpSubscriptions({
91-
webContentsId: win.webContents.id,
96+
webContentsId: webContentsId,
9297
frameRoutingId: frame.routingId,
9398
});
9499
}
95100
});
96101
win.webContents.on('destroyed', () => {
97102
debug('Handling webContents `destroyed` event');
98-
this.detachWindow(win);
103+
this.detachWindow(win, webContentsId);
99104
});
100105
}
101106
}

0 commit comments

Comments
 (0)