Fork me on GitHub
#re-frame
<
2020-10-10
>
anonimitoraf07:10:28

I've read quite a bit on effects and co-effects but haven't found anything so far that is applicable to my use case Basically, I need to register hotkeys (terminal UI) to a react-blessed component which is a big fat side-effect. What's the most acceptable way of doing this? I've got this so far:

(rf/reg-event-db
 :hotkeys/set
 (fn [db [_ hotkeys]]
   (let [wrapped-hotkeys (wrap-hotkeys hotkeys)]
     ;; TODO: Is it ok to have side-effects here?
     ;; If not, where do we put side-effects
     (doseq [hs wrapped-hotkeys] (bind-keys @screen hs))
     (assoc db :hotkeys wrapped-hotkeys)))) ;; Kept for later so we can de-register when needed

anonimitoraf07:10:10

(unrelated: screen should probably be injected as a co-effect)

p-himik08:10:24

Create a custom effect that would call bind-keys, that's all. screen does not need to be a co-effect if it will be referenced only in the effect handler.

anonimitoraf08:10:23

Sorry, what do you mean by custom effect? And no, screen is referenced by other stuff in the application (it's sort of a global singleton)

p-himik08:10:27

I mean reg-fx.

anonimitoraf08:10:25

Right. But that means that effect will be impure then?

p-himik09:10:39

The effects are supposed to be impure.

anonimitoraf09:10:45

Oh right. Sorry, I confused effects and event handlers. Thanks

👍 3
bastilla13:10:18

Hey! I want to trigger (dispatch) an event from within an event. Why? I don't want to copy/paste code. From here https://day8.github.io/re-frame/Effects/ I learned it can be done via :fx key... "the :fx key instructs that an ordered list of other effects should be executed. In this case a :dispatch key instructs that an event should be dispatched. The value is the vector to dispatch." But my code:

(rf/reg-event-fx ::choose-root
  (fn [coeffects event]
    (let [db (:db coeffects)
          location (get event 1)
          page (get event 2)]
      {:db (a/set-user-status db :edit-root location)
       :fx [[:dispatch [::fetch-fs-obj location page]]]}
      )))
fails with re-frame: no handler registered for effect: Object { ns: null, name: "fx", fqn: "fx", But maybe it's just not possible to trigger an event from within an event and I need to copy/paste code. I'd really appreciate any hints (or links)!

p-himik13:10:24

What re-frame version are you using?

bastilla13:10:56

Just updated to 1.1.1 and it seems to work!

bastilla13:10:08

Yes, it works. A 1000 Thanks to you. hands @U2FRKM4TW hero of my day price... Awesome!

👍 3