Fork me on GitHub
#re-frame
<
2020-06-07
>
afleck13:06:48

I'm doing something like (rf/dispatch [:set-foo (js/setInterval #(bar) 1000)]) to both start an interval and save the id for clearing later. it feels a little dirty to me, I was just wondering if there's a more idiomatic way to do this

p-himik13:06:54

IMO a more idiomatic way would be create an effect handler that calls setInterval for you. Along with the effect handler there would be a map of some ID that you pass to the effect to the value returned by setInterval. That's exactly what :dispatch-debounce effect handler in re-frame-fx does: https://github.com/7theta/re-frame-fx/blob/master/src/re_frame_fx/dispatch.cljs#L16

afleck13:06:47

it seems like maybe js/setInterval should be a 'coeffect'? though i'm not exactly sure how to accomplish this

dpsutton13:06:53

i would probably make an api that would take a keyword to name the interval as well. and you would call [:interval/schedule ::my-interval interval-ms f & args] and then there would be an associated [:interval/stop ::my-interval]