Fork me on GitHub
#re-frame
<
2020-06-10
>
valerauko15:06:20

what's the "ideal way" to listen for window events?

p-himik15:06:53

Just as with any other side effect - create an effect handler that adds event listeners and its counterpart that removes them.

valerauko15:06:37

i'm brand new to re-frame (literally just have the lein template stuff) so could you please point me to resources on how to do that?

isak18:06:24

lifecycle methods are cleaner for this imo (like component did mount, and component will unmount), but re-framers don't like them

p-himik18:06:35

In this case, listening to an event has nothing to do with any lifecycle method because the question doesn't say anything about any component. It's perfectly fine to listen to window events even if you don't have any React component on the page.

isak21:06:28

Usually related to a view IME

mikethompson23:06:18

@UAEH11THP the idea is that the window event handlers which you install will dispatch events which are then handled by event handlers

mikethompson23:06:26

There are two ways that the event handlers could be installed: 1. If they are installed and then removed, then that would happen via an effect handler 2. if they are installed once at the beginning on the program and never removed, then just mainline it

valerauko03:06:19

my use case is handling cross-process events in electron that are passed as window 'message' events. i can pass stuff from the renderer to main without problem, but going back i'd need window.addEventListener('message', ...) . specifically i use this to open system-native dialogs (file picker) and i'd need to pass back the result. i figured i'd add a window event handler that'd then dispatch re-frame events which i could then handle pretty -- but i couldn't figure out what was the right way to "add a window event handler".

valerauko03:06:24

seeing how re-com's readme specifically mentions it's aimed at electron i guess re-frame is also often used there. if the way i try to handle ipc isn't right please point me to the "right way"