Skip to content

Commit

Permalink
fix: ipcRenderer listener not being removed in hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Rohloff committed Sep 19, 2024
1 parent 39df390 commit 9298136
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
43 changes: 40 additions & 3 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
/* eslint-disable no-bitwise */
/* eslint no-unused-vars: off */
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
import { IpcChannels } from './ipc';

export class IpcSubscription {
public readonly channel: IpcChannels;

public readonly subscription: any;

public constructor(channel: IpcChannels, subscription: any) {
this.channel = channel;
this.subscription = subscription;
}

public remove() {
ipcRenderer.removeListener(this.channel, this.subscription);
}
}

function guidGenerator() {
const S4 = () => {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return `${S4() + S4()}-${S4()}-${S4()}-${S4()}-${S4()}${S4()}${S4()}`;
}

const subscriptions: Map<string, IpcSubscription> = new Map();

const electronHandler = {
ipcRenderer: {
sendMessage(channel: IpcChannels, ...args: any[]) {
Expand All @@ -12,16 +37,28 @@ const electronHandler = {
func(...args);
ipcRenderer.on(channel, subscription);

return () => {
ipcRenderer.removeListener(channel, subscription);
};
const sub = new IpcSubscription(channel, subscription);
const id = guidGenerator();

subscriptions.set(id, sub);
return id;
},
once(channel: IpcChannels, func: (...args: any[]) => void) {
ipcRenderer.once(channel, (_event, ...args) => func(...args));
},
invoke(channel: IpcChannels, ...args: any[]): Promise<any> {
return ipcRenderer.invoke(channel, ...args);
},
removeListener(id: string) {
const sub = subscriptions.get(id);
if (sub) {
ipcRenderer.removeListener(sub.channel, sub.subscription);
subscriptions.delete(id);
}
},
removeAllListeners(channel: IpcChannels) {
ipcRenderer.removeAllListeners(channel);
},
},
};

Expand Down
22 changes: 13 additions & 9 deletions src/renderer/hooks/useMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ export default function useMessages(channel: RendererChannel | null) {
const [messages, setMessages] = useState<Message[]>([]);

useEffect(() => {
window.electron.ipcRenderer.on(
'discord:gateway:message-create',
(message: Message) => {
if (message.channel_id !== channel?.id) return;

setMessages((old) => [message, ...old]);
},
);

if (channel) {
channel
.fetchMessages()
Expand All @@ -24,6 +15,19 @@ export default function useMessages(channel: RendererChannel | null) {
})
.catch((err) => console.error(err));
}

const subscription = window.electron.ipcRenderer.on(
'discord:gateway:message-create',
(message: Message) => {
if (message.channel_id !== channel?.id) return;

setMessages((old) => [message, ...old]);
},
);

return () => {
window.electron.ipcRenderer.removeListener(subscription);
};
}, [channel]);

return messages;
Expand Down

0 comments on commit 9298136

Please sign in to comment.