Skip to content

Commit 9d3a9cf

Browse files
committed
fix(desktop): error when removing listener
1 parent f06515d commit 9d3a9cf

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

components/tabs.tsx

+20-14
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,28 @@ export const TabManager: React.FC = () => {
3030
setCurrentTab(tab)
3131
}
3232

33-
window.eidos.on("tabs-updated", (event, tabs: string[]) => {
34-
console.log("tabs-updated", tabs)
35-
handleTabsUpdated(tabs)
36-
})
37-
window.eidos.on("current-tab-updated", (event, tab: string) => {
38-
console.log("current-tab-updated", tab)
39-
handleCurrentTabUpdated(tab)
40-
})
41-
42-
return () => {
43-
window.eidos.off("tabs-updated", (event, tabs: string[]) =>
33+
let listenerId1 = window.eidos.on(
34+
"tabs-updated",
35+
(event, tabs: string[]) => {
36+
console.log("tabs-updated", tabs)
4437
handleTabsUpdated(tabs)
45-
)
46-
window.eidos.off("current-tab-updated", (event, tab: string) =>
38+
}
39+
)
40+
let listenerId2 = window.eidos.on(
41+
"current-tab-updated",
42+
(event, tab: string) => {
43+
console.log("current-tab-updated", tab)
4744
handleCurrentTabUpdated(tab)
48-
)
45+
}
46+
)
47+
48+
return () => {
49+
if (listenerId1) {
50+
window.eidos.off("tabs-updated", listenerId1)
51+
}
52+
if (listenerId2) {
53+
window.eidos.off("current-tab-updated", listenerId2)
54+
}
4955
}
5056
}, [setTabs, setCurrentTab])
5157
const tabNodes = Array.from(openTabs)

electron/preload.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function main() {
2323
contextBridge.exposeInMainWorld('eidos', {
2424
on(channel: string, listener: IpcListener) {
2525
if (typeof channel !== 'string' || typeof listener !== 'function') {
26-
throw new Error('Invalid parameters');
26+
throw new Error('Invalid parameters for add listener for channel: ' + channel);
2727
}
2828
if (!listenerMap.has(channel)) {
2929
listenerMap.set(channel, new Map());
@@ -48,7 +48,7 @@ async function main() {
4848

4949
off(channel: string, listenerId: string) {
5050
if (typeof channel !== 'string' || typeof listenerId !== 'string') {
51-
throw new Error('Invalid parameters');
51+
throw new Error('Invalid parameters for remove listener for channel: ' + channel);
5252
}
5353

5454
const channelListeners = listenerMap.get(channel);

hooks/use-update-status.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ export const useUpdateStatus = () => {
4545
}
4646
};
4747

48-
window.eidos.on('update-status', (event, status: UpdateStatus, data?: any) => {
48+
let listenerId = window.eidos.on('update-status', (event, status: UpdateStatus, data?: any) => {
4949
handleUpdateStatus(status, data);
5050
});
5151

5252
return () => {
53-
window.eidos.off('update-status', (event, status: UpdateStatus, data?: any) => {
54-
handleUpdateStatus(status, data);
55-
});
53+
if (listenerId) {
54+
window.eidos.off('update-status', listenerId);
55+
}
5656
};
5757
}, []);
5858

hooks/use-worker.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ export const useWorker = () => {
9191
window.eidos.send(`response-${requestId}`, result);
9292
}
9393
let listenerId: string | undefined;
94+
let listenerId2: string | undefined;
9495
if (isDesktopMode) {
9596
listenerId = window.eidos.on('request-from-main', requestHandler);
96-
window.eidos.on(EidosMessageChannelName, async (event, arg) => {
97+
listenerId2 = window.eidos.on(EidosMessageChannelName, async (event, arg) => {
9798
await handle(new MessageEvent('message', { data: arg }))
98-
});
99+
}) as unknown as string;
99100
setInitialized(true)
100101
} else {
101102
const worker = getWorker()
@@ -106,6 +107,9 @@ export const useWorker = () => {
106107
if (listenerId) {
107108
window.eidos.off('request-from-main', listenerId);
108109
}
110+
if (listenerId2) {
111+
window.eidos.off(EidosMessageChannelName, listenerId2);
112+
}
109113
} else {
110114
const worker = getWorker()
111115
worker.removeEventListener("message", handle)

0 commit comments

Comments
 (0)