re-frame

https://github.com/Day8/re-frame/blob/master/docs/README.md https://github.com/Day8/re-frame/blob/master/docs/External-Resources.md
anonimitoraf 2020-10-10T07:39:28.070600Z

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

anonimitoraf 2020-10-10T07:40:10.071Z

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

p-himik 2020-10-10T08:32:24.071200Z

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.

anonimitoraf 2020-10-10T08:55:23.071700Z

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-himik 2020-10-10T08:56:27.071900Z

I mean reg-fx.

anonimitoraf 2020-10-10T08:57:25.072100Z

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

p-himik 2020-10-10T09:00:39.072300Z

The effects are supposed to be impure.

anonimitoraf 2020-10-10T09:12:45.072500Z

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

1👍
bastilla 2020-10-10T13:03:18.074300Z

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-himik 2020-10-10T13:06:24.074500Z

What re-frame version are you using?

bastilla 2020-10-10T13:07:28.074700Z

1.0.0

bastilla 2020-10-10T13:12:56.074900Z

Just updated to 1.1.1 and it seems to work!

bastilla 2020-10-10T13:15:08.075100Z

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

1👍