Re-frame and multimethod don't like each other?

one question, i am using sente+re-frame
and in my handler on client-side looks like this.

(defmethod -event-msg-handler :bus-line/coord
 [{:as ev-msg :keys [?data]}]
 (do (println "Push coord from server: %s" ?data)
   (rf/dispatch [:bus-line/coord (:coord ?data)])))

I made broadcaster on backend to trigger every 10 sec and send new data
this is what println gives me

Push event from server: %s [:bus-line/coord {:what-is-this An async broadcast pushed from 
 server, :how-often Every 10 seconds, :to-whom 8e4c47c3-f46a-43e8-92fc-e375601ac324, 
 :coord {:bus-line string, :current-lat string, :current-long string, :timestamp 6}, :i 420}]

this dispatch just doesn’t work
println does get triggered but dispatch doesnt
but it works if i eval it

what am i missing?
why doesn’t it work

Hey and welcome @stefankurcubic2 — I took the liberty to slightly improve the formatting of your question.

I don’t have a clear cut answer to your question but can you show your code that registers the event handler for the event type bus-line/coord that you’re dispatching?

Also maybe elaborate on what you’re trying to achieve by using the multimethod that you pasted.

Thank you, i struggled to format correctly :smiley:

     (reg-event-db
     :bus-line/coord
     (fn-traced [db [_ coord]]
                (assoc db :bus-line/coord coord)))

Well my goal is to make subscription for type of data i need on client.

Basically choose what i want > chsk-send! > return latest data. I got this working.

What this mutlimethod should do is act as a refresher of that data.
I’ve got broadcaster on backend that sends this event bus-line/coord with latest data to subscribed client
i want to dispatch that data (update rf/db) so subscribe can pick it up and display it.

(defmethod -event-msg-handler :bus-line/coord
 [{:as ev-msg :keys [?data]}]
 (do (println "Push coord from server: %s" ?data)
     (rf/dispatch [:bus-line/coord (:coord ?data)])))
(reg-event-db
  :bus-line/coord
  (fn-traced [db [_ coord]]
    (assoc db :bus-line/coord coord)))

So with these two code snippets you are seing the println in the console but you’re not seeing the handler that updates app-db being triggered?

If so a few ideas:

  • Put a println in the reg-event-db function to get some further visibility into whether it is called
  • Replace fn-traced with a regular fn just to exclude any issues stemming from that (I don’t know what fn-traced does or where it comes from)
  • Ensure that the namespace that contains the reg-event-db form is loaded. You could do that by calling re-frame.registrar/get-handler like so (I think):
    (re-frame.registrar/get-handler :event :bus-line/coord)
    

Hope that helps!

  1. i put println in reg-event-db - nothing happened. it’s like reg-event-db is not triggered at all

  2. fn-traced https://github.com/Day8/re-frame-10x/blob/master/docs/HyperlinkedInformation/EventCodeTracing.md
    (nothing special i am just able to see events in debugging dashboard)
    it didnt work while it was fn

  3. handler was registered before, i tried your way also doing it by hand it didnt help

i am open for suggestions.

It must be something stupid that i am missing

I know next to nothing about re-frame but it looks like ?data is the vector above? So getting (:coord [:bus-line/coord {...}]) just ends up as (rf/dispatch [:bus-line/coord nil])? Not sure if that matters.

Solved

  [{:as ev-msg :keys [?data]}]
  (let [_ (println "Push event from server: " ?data)
        [event msg] ?data
        _ (println "event" event "msg: " msg)]
    (rf/dispatch ?data)))

actually this handler is triggered once data is being pushed from the server…
maybe i should’ve paid more attention

but @thheller put me on the track to debug this.

it seems that i need to do one more multimethod to handle receiving messages

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.