Fork me on GitHub
Lucy Wang02:11:27

@p-himik I just found that yesterday and wrote a custom version of reg-global-interceptor to work around that

(ns demo
  (:require [re-frame.core :as rf]
            [re-frame.settings :as rf.settings]))

(defn safe-reg-global-interceptor
  "rf/reg-global-interceptor appends instead of replacing. This wrapper
  function ensures replacing the existing global interceptor with the
  same id, if any."
  [{:keys [id] :as interceptor}]
  (let [global-interceptors (rf.settings/get-global-interceptors)]
    (->> global-interceptors
         (remove #(= (:id %) id))
         ;; global-interceptors is a queue
         (into (empty global-interceptors))
         (swap! rf.settings/store assoc :global-interceptors))
    (rf/reg-global-interceptor interceptor)))


Nice! One issue though - your code removes and appends the interceptor. It doesn't preserve the order.


Here's what I've come up with (didn't test it that well but seems to work just fine):

(def reg-global-interceptor
  (if re-frame.interop/debug-enabled?
    (fn [interceptor]
      (let [existing-interceptors (re-frame.settings/get-global-interceptors)]
        (if (some #(= (:id %) (:id interceptor)) existing-interceptors)
            (doseq [i existing-interceptors]
                (if (= (:id i) (:id interceptor))
          (rf/reg-global-interceptor interceptor))))


And it was fixed rather quickly. :)


Hi there. Can anyone point my to a blog entry / article / whatever with code snippets on how to upload a file to a server? Would greatly appreciate it. (As so often I fail utterly in finding actual / official docs on clojure libs.


What I found so far on re-frame is not going into details. Just superficial stuff with lots of text but little of documentation and actual code snippets.


I assume you mean HTTP POST. Do you know how to upload files using vanilla JS?


Hey @p-himik Thanks for responding! Well, yes and no. I know but not from the top of my head. I need to look up the details. I am pretty sure I could implement it in plain JS in like 15 minutes. And I am sure could do it in Python in like 15 minutes - and I don't know Python - since it's easy to find the docs.


You do it the same way as you do with JS. Just use ClojureScript's JS interop


Only difference is with re-frame, you'd probably have the fetch function (i.e. the HTTP POST) as an effect called from an event handler (ex. The process of creating formdata and passing it onto the body of the POST is the same though.


And yes, you won't be able to search for specific problems and find snippets ready for copy-and-paste with Clojure, as you can with the mainstream languages. You're kind of on your own to put the pieces together, but don't worry as this is a skill you'll get better at with practice!


thanks so far guys. I'll post my solution.

👍 1

I use re-frame-http-fx and in the :body parameter I put a manually created js/FormData.

👍 1