Sometimes I don't want event handlers to change app-db #663
Replies: 2 comments
-
If you are checking with foo if you should change the db, why not perform the check at callsite and avoid the roundtrip/dispatch? |
Beta Was this translation helpful? Give feedback.
-
@Yunfeng-Song I don't think it is an issue to conditionally update app-db. As you know in reg-event-db as opposed -fx variant, you must return it, modified or not, but even in -fx you'd need to check the condition to decide to return
But in the simpler case as @schpaa points out, you could test at the call site if what you are testing is in scope to avoid the dispatch, however your condition to modify or not may well depend on something more complex outside of the scope or responsibility of the call site, like something else in app-db which the call site should probably not know about. concocted example: (rf/reg-event-db
:example
(fn [db [_ foo]]
(if (and (:editable? db) foo)
(assoc-in db [:foo :bar] foo)
db))) So to answer your question, I don't think it is a code smell to return an unmodified db. |
Beta Was this translation helpful? Give feedback.
-
For example, I might have a handler like this:
(rf/reg-event-db
:example
(fn [db [_ foo]]
(if foo
(assoc-in db [:foo :bar] foo)
db)))
I mean, is it a good practice when return db if I don't want to change the db. Or should I check the foo(in the example) before I dispatch this event?
PS: I know it's not a problem for -fx because you can return {}
Beta Was this translation helpful? Give feedback.
All reactions