A core part of the project I'm working on involves the main process sending messages to the renderer, which then get logged and shown in the UI. The sending of messages between the main and renderer processes is something I seem to understand, but I'm encountering issues regarding React's useEffect cleanup where I unregister the previously registered IPC events to avoid memory leaks. As a result, when multiple messages are sent to the window's webContents in quick succession, and then React state is mutated, the useEffect cleanup runs which unregisters the listeners and results in most of the messages not having a callback associated and are simply lost to the void.
I understand why this is happening, my useEffect is mutating state which causes it to cleanup and unregister the listeners before re-rendering again, I just don't understand any other way to do this. I suppose the bigger question overall is how can I ensure messages sent from the main process will actually be handled if the very place I register them (useEffect) is responsible for un-registering them any time there is a state change/re-render? I'm unsure if I'm missing an obvious piece to this, but I can't think of another way to reliably send messages from main to renderer with this setup.
I've taken a look at as many resources, blog posts, docs, and example projects as I could find and they all seem to do the same thing: setup the listener in the useEffect and then remove it in the cleanup. This works fine for main -> renderer messages that aren't sent at a high rate, but in my use case the rate at which messages (logs) get sent is both high and not really predictable, as it's caused by any number of external events.
I may very well just not understand what the appropriate React/state management architecture for this is, as it seems like the use of a single useEffect to manage the IPC listeners and mutate the state is a pretty clear conflict, but I'm not sure what an alternative structure would be.
Any advice on this would be much appreciated!