Fork me on GitHub
#re-frame
<
2019-02-13
>
andrea.crotti08:02:01

I was playing around with the idea of checking the spec of the db in my small multipage SPA and I came up with something like this:

(defn ->interceptor
  [page db-spec]
  (rf/->interceptor
   :id :validate
   :after (fn [context]
            (let [local-db (-> context :effects :db page)]
              (if (s/valid? db-spec local-db)
                context
                (throw (ex-info (str "spec check failed: " (s/explain-str db-spec local-db)) {})))))))

(defn ->safe-event-db
  [page db-spec]
  (fn [id handler]
    (rf/reg-event-db
     id
     [(->interceptor page db-spec)]
     handler)))

andrea.crotti08:02:45

which can then be used like

(def safe-event-db (common/->safe-event-db page ::db))

(safe-event-db ::name
               (setter [:player :name]))
seems to work well enough, but is this is as good way to do it ?

Teemu Kaukoranta11:02:12

Can I use the :dispatch effect from other effects? I have a generic event that makes http-calls, and I'd like to add an effect that dispatches it.

Teemu Kaukoranta11:02:43

Right now my options are to either call (dispatch) from the effect directly, or turn the effect into another event. No preference either way really

valtteri11:02:30

Reading your message again and I’m not sure anymore what you’re trying to achieve. 😅 So you would like to have an effect that dispatches an event that would be executed by your event-handler? :dispatch and :dispatch-n effects are meant exactly for that. But I guess you wanted something more elaborate?

Teemu Kaukoranta12:02:14

My first attempt was just to use :dispatch from my event, but I felt like it didn't work. The error could have been elsewhere though

Lu11:02:29

@teemu.kaukoranta Sure you can. Just add in your map the key dispatch like this:

{:db db
:dispatch [:make-http-call params]}
or if you want to dispatch multiple events in one go you can use :dispatch-n like this:
{:db db
:dispatch-n [[:make-http-call params]
            [:do-something-else params]]}

Teemu Kaukoranta12:02:14

My first attempt was just to use :dispatch from my event, but I felt like it didn't work. The error could have been elsewhere though

Teemu Kaukoranta12:02:22

Nope, when I try to use the :dispatch effect from my effect, it doesn't seem to fire. Basically I'm trying to build a event->effect->event->... chain. I could be heading into the wrong direction, but this feels like a solid idea for this case 😅

Teemu Kaukoranta12:02:33

I think I'm just going to turn my effect into another event. All it does is :dispatch, but whatever

andrea.crotti17:02:56

small docs PR about the namespaced keywords https://github.com/Day8/re-frame/pull/528

ag18:02:49

Andrea there’s a typo https://github.com/Day8/re-frame/pull/528/files#diff-6cdbf45b44314b03d02cc796aca036d7R90 Also I vaguely remember that using dots in keywords is discouraged (?) Not sure exactly for what reason

andrea.crotti18:02:35

ah thanks @U0G75ARHC, I fixed it

andrea.crotti18:02:52

and well where am I using dots in keywords?

andrea.crotti18:02:16

if you mean :project.panel.handlers/set-x that's just the other way to refer to a namespaced keyword

ag18:02:39

ah… okay