mirror of
https://github.com/penpot/penpot.git
synced 2025-07-22 04:17:12 +02:00
✨ Add the ability to stream events on rpc methods
This commit is contained in:
parent
f3e9efa6fe
commit
03518a8da1
21 changed files with 408 additions and 106 deletions
|
@ -105,17 +105,22 @@
|
|||
|
||||
(defn send!
|
||||
[{:keys [response-type] :or {response-type :text} :as params}]
|
||||
(letfn [(on-response [response]
|
||||
(let [body (case response-type
|
||||
:json (.json ^js response)
|
||||
:text (.text ^js response)
|
||||
:blob (.blob ^js response))]
|
||||
(->> (rx/from body)
|
||||
(rx/map (fn [body]
|
||||
{::response response
|
||||
:status (.-status ^js response)
|
||||
:headers (parse-headers (.-headers ^js response))
|
||||
:body body})))))]
|
||||
(letfn [(on-response [^js response]
|
||||
(if (= :stream response-type)
|
||||
(rx/of {:status (.-status response)
|
||||
:headers (parse-headers (.-headers response))
|
||||
:body (.-body response)
|
||||
::response response})
|
||||
(let [body (case response-type
|
||||
:json (.json ^js response)
|
||||
:text (.text ^js response)
|
||||
:blob (.blob ^js response))]
|
||||
(->> (rx/from body)
|
||||
(rx/map (fn [body]
|
||||
{::response response
|
||||
:status (.-status ^js response)
|
||||
:headers (parse-headers (.-headers ^js response))
|
||||
:body body}))))))]
|
||||
(->> (fetch params)
|
||||
(rx/mapcat on-response))))
|
||||
|
||||
|
|
54
frontend/src/app/util/sse.cljs
Normal file
54
frontend/src/app/util/sse.cljs
Normal file
|
@ -0,0 +1,54 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) KALEIDOS INC
|
||||
|
||||
(ns app.util.sse
|
||||
(:require
|
||||
["eventsource-parser/stream" :as sse]
|
||||
[beicon.core :as rx]))
|
||||
|
||||
(defn create-stream
|
||||
[^js/ReadableStream stream]
|
||||
(.. stream
|
||||
(pipeThrough (js/TextDecoderStream.))
|
||||
(pipeThrough (sse/EventSourceParserStream.))))
|
||||
|
||||
(defn read-stream
|
||||
[^js/ReadableStream stream decode-fn]
|
||||
(letfn [(read-items [^js reader]
|
||||
(->> (rx/from (.read reader))
|
||||
(rx/mapcat (fn [result]
|
||||
(if (.-done result)
|
||||
(rx/empty)
|
||||
(rx/concat
|
||||
(rx/of (.-value result))
|
||||
(read-items reader)))))))]
|
||||
(->> (read-items (.getReader stream))
|
||||
(rx/mapcat (fn [^js event]
|
||||
(let [type (.-event event)
|
||||
data (.-data event)
|
||||
data (decode-fn data)]
|
||||
(if (= "error" type)
|
||||
(rx/throw (ex-info "stream exception" data))
|
||||
(rx/of #js {:type type :data data}))))))))
|
||||
|
||||
(defn get-type
|
||||
[event]
|
||||
(unchecked-get event "type"))
|
||||
|
||||
(defn get-payload
|
||||
[event]
|
||||
(unchecked-get event "data"))
|
||||
|
||||
(defn end-of-stream?
|
||||
[event]
|
||||
(= "end" (get-type event)))
|
||||
|
||||
(defn event?
|
||||
[event]
|
||||
(= "event" (get-type event)))
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue