day8 / re-frame-http-fx

A re-frame "effects handler" for performing Ajax tasks (via cljs-ajax)
MIT License
259 stars 27 forks source link

Incremental Processing of Streaming Download? #27

Open Folcon opened 5 years ago

Folcon commented 5 years ago

I've been working out how to get incremental results, not sure if you want to support it as cljs-http might be moving from xhrio (#21). I've got an issue open there, and it can be plugged in at either level.

It's based on me reading through kennethkalmer's PR (#18) and working backwards to figure out where the change should start.

The essence of it is here:

(defn request->xhrio-options
  [{:as   request
    :keys [on-success on-failure on-progress]
    :or   {on-success      [:http-no-on-success]
           on-failure      [:http-no-on-failure]}}]
  ; wrap events in cljs-ajax callback
  (let [api (new js/goog.net.XhrIo)
        _ (when (and on-progress (fn? on-progress))
            (doto api
              (.setProgressEventsEnabled true)
              (events/listen goog.net.EventType.PROGRESS on-progress)))
        ]
    (-> request
        (assoc
          :api     api
          :handler (partial ajax-xhrio-handler
                            #(dispatch (conj on-success %))
                            #(dispatch (conj on-failure %))
                            api))
        (dissoc :on-success :on-failure :on-progress))))

You can now define an on-progress fn that does something with the partial response:

:on-progress
(fn [e]
    (let [resp-text (.getResponseText (.-currentTarget e))]
        resp-text))

Not sure if you want to support it, or should I try upstream?

danielcompton commented 5 years ago

The goal of re-frame-http-fx has always been to be a very thin wrapper around cljs-ajax that introduces as few new concepts of features as possible, so I would suggest trying upstream first. Appreciate the feedback and research though!

Folcon commented 5 years ago

@danielcompton No problem and totally understand that, I'll see what the response is there :)... Do you mind if I leave this open here if someone's looking how they'd do it?

danielcompton commented 5 years ago

Sure, that’s fine.