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
(unrelated: screen
should probably be injected as a co-effect)
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.
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)
I mean reg-fx
.
Right. But that means that effect will be impure then?
The effects are supposed to be impure.
Oh right. Sorry, I confused effects and event handlers. Thanks
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)!What re-frame version are you using?
1.0.0
Just updated to 1.1.1 and it seems to work!
Yes, it works. A 1000 Thanks to you. hands @p-himik hero of my day price... Awesome!