Fork me on GitHub
#re-frame
<
2019-07-23
>
rgm16:07:34

highly possible this has been asked before, but is there any re-frame add-on that could spit out a subscription graph as graphviz?

đź‘Ť 4
jaide22:07:14

Is there a documented solution for Warning: unmountComponentAtNode(): The node you're attempting to unmount was rendered by another copy of React? I’m using reagent 0.8.1, day8.re-frame 0.10.8, and day8.re-frame-10x 0.4.2, day8.re-frame/async-flow-fx 0.1.0, and day8.re-frame/tracing 0.5.1

Lu23:07:37

@atticmaverick Well, you could do this without even using an interceptor.. You just need one condition in :try-different-server to either dispatch again with a different uri or to handle the failure as you want:

(rf/reg-event-fx
 :http-post
 (fn [{db :db} [_ uri]]
   {:http-xhrio
    {:method :post
     :uri uri
     :params {}
     :response-format "foo"
     :format "bar"
     :on-success [:success]
     :on-failure [:try-different-server]}}))

(rf/reg-event-fx
 :success
 (fn [_ _]
   "do what you want"))

(rf/reg-event-fx
 :try-different-server
 (fn [{db :db} _]
   (if (:second-attempt-fired? db)
     {:db (assoc db :error "both servers failed")}
     {:db (assoc db :second-attempt-fired? true)
      :dispatch [:http-post (:server-b-uri db)]})))

Lu23:07:54

If you can't help but using an interceptor a solution would be this:

(def replay-event
  (re-frame.core/->interceptor
   :id      :replay-event
   :after (fn [context]
              (when (get-in context [:effects :db :fire-second?])
                (-> context
                    (assoc-in [:effects :dispatch] [:http-post])
                    (assoc-in [:effects :db :uri] "/new-uri")
                    (assoc-in [:effects :db :fired-twice?] true))))))

(rf/reg-event-fx
 :http-post
 (fn [{db :db} _]
   (let [uri (:uri db)]
     {:http-xhrio
      {:method :post
       :uri uri
       :params {}
       :response-format (ajax/transit-request-format)
       :format (ajax/transit-request-format)
       :on-success [:success]
       :on-failure [:try-different-server]}})))

(rf/reg-event-fx
 :success
 (fn [_ _]
   "do what you want"))

(rf/reg-event-fx
 :try-different-server
 [replay-event]
 (fn [{db :db} _]
   (when-not (:fired-twice? db)
     {:db (assoc db :fire-second? true)})))