Fork me on GitHub
#re-frame
<
2018-09-30
>
lepistane08:09:00

hi, what is the correct way of fetching initial data during load? i am using re-frame and sente and i want to fetch dropdown items during init but it doesnt happen re-frame dispatch happens before sente initialization and i am not sure where and how to 'delay' dispatch so it happens after sente init

ferossgp07:10:24

(defn- send-event-when-open [chsk-send! chsk-state event]
  (if (:open? @chsk-state)
    (do
      (timbre/info "Sending event on an already open connection: "
                    event)
      (chsk-send! event))
    (do
      (timbre/info "Connection closed, waiting until open: "
                    event)
      (add-watch chsk-state :subscribe
                 #(when (:open? %4)
                    (timbre/debug "Connection open sending event: "
                                  event)
                    (chsk-send! event)
                    (remove-watch chsk-state :subscribe))))))
I've added a workaround so my events are sent only when connection is open

👍 4
ferossgp07:10:29

Also I do not use callback but reading from channel

lepistane08:09:20

this feels dirty but it works

lepistane08:09:29

(defn start-ws []
  (start-websocket)
  (sente/start-client-chsk-router! ch-chsk event-msg-handler)
  (js/setTimeout #(rf/dispatch [:all-items]) 500)
)
any suggestions to make it better?

curlyfry08:09:51

@lepistane So the :all-items handler fetches data using a websocket?

lepistane08:09:43

(reg-event-db
 :all-items
 (fn-traced [db [_]]
            (ws/chsk-send! [:all-items]
                           3000
                           (fn [reply]
                             (if (sente/cb-success? reply)
                               (dispatch [:show])
                               (js/alert "No reply"))))
            db))

lepistane08:09:20

i know effect should be used but i am making it as simple as i can and slowly making it right

lepistane08:09:38

@curlyfry this is how it looks like

curlyfry08:09:25

Do you know for sure after the (sente/start-client-chsk-router! ch-chsk event-msg-handler) if the websocket is inited correctly? If so you shouldn't need the timeout

curlyfry08:09:32

(I haven't used sente before)

lepistane08:09:25

i am sure it is cause if i put that dispatch on button click it works

curlyfry08:09:34

Why do you have a setTimeout?

lepistane09:09:57

@curlyfry i get (js/alert) if i dont

lepistane09:09:11

ok i solved it

curlyfry09:09:14

Hmm, I don't think I fully understand the issue

lepistane09:09:22

Pageload: How do I know when Sente is ready client-side?
You'll want to listen on the receive channel for a [:chsk/state [_ {:first-open? true}]] event. That's the signal that the socket's been established.

lepistane09:09:49

so i just made method in event handler

lepistane09:09:55

which if true dispatches event

lepistane09:09:00

thank you for your help!

curlyfry09:09:17

Cool, that definitely sounded like the root issue 🙂 (knowing when the client is actually ready) Glad you solved it!