Search code examples
interceptorclojurescripteffectsre-frame

adding to :effects in :before function in re-frame interceptors


I have this working piece of code:

(rf/reg-fx :mr-effect (fn [] (js/console.log "I AM MR EFFECT.")))

(def mr-interceptor
  (rf/->interceptor
   :id    :mr-interceptor
   :after (fn [context] (assoc-in context [:effects :mr-effect] "some_arg"))))

So, when I attach mr-interceptor to an event handler, then all is good and I can see "I AM MR EFFECT." printed out.

But, as soon as I change :after to :before, like this:

(def mr-interceptor
  (rf/->interceptor
   :id    :mr-interceptor
   :before (fn [context] (assoc-in context [:effects :mr-effect] "some_arg"))))

Then the desired side effect does not happen. So, my reasoning is that :before cannot register such side effects to be triggered. But... am I right? Can :before change :effects in any way that's meaningful?


Solution

  • Your conclusion is correct. Event handlers are called in the last interceptor in the chain (it's added automatically by re-frame) and that interceptor uses simple (assoc ctx :effects ...). So it will overwrite whatever you put in :effects during any previous step.