This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-11-03
Channels
- # announcements (35)
- # aws (20)
- # babashka (4)
- # beginners (88)
- # cider (9)
- # clara (1)
- # clj-kondo (6)
- # cljsrn (3)
- # clojure (107)
- # clojure-dev (7)
- # clojure-europe (99)
- # clojure-nl (3)
- # clojure-spec (9)
- # clojure-uk (2)
- # clojurescript (28)
- # core-async (53)
- # cursive (11)
- # datascript (1)
- # datomic (2)
- # emacs (20)
- # fulcro (3)
- # graalvm (4)
- # holy-lambda (18)
- # jobs (1)
- # kaocha (7)
- # leiningen (2)
- # lsp (25)
- # luminus (1)
- # membrane-term (52)
- # missionary (8)
- # nextjournal (19)
- # off-topic (16)
- # other-languages (3)
- # podcasts-discuss (2)
- # polylith (23)
- # re-frame (4)
- # reclojure (6)
- # remote-jobs (1)
- # rewrite-clj (36)
- # ring (1)
- # sci (10)
- # shadow-cljs (7)
- # spacemacs (5)
- # sql (20)
- # uncomplicate (1)
- # vscode (3)
- # xtdb (27)
(let [reader (.. response -body getReader)]
(-> (.read reader)
(.then (fn [result]
;; repeat the .read call when not done
(tap> {:done? (.-done result)})
;; emit chunk to flow
#(js/console.log :chunk (.decode decoder (.-value result)))))))
I’m trying to get flow from a reader object like above. It requires me to repeatedly .read
, wait for the promise and then call .read
again. I’m struggling a bit with figuring out how to turn that into a flowI guess I can use observe
:thinking_face:
Assuming you have a task to read the next chunk, you can use this pattern :
(m/ap
(loop []
(let [result (m/? (read! reader))]
(if (.-done result) result (m/amb> result (recur))))))
(defn read-chunks
[reader emit!]
(let [decoder (js/TextDecoder.)]
(-> (.read reader)
(.then (fn [result]
;; repeat the .read call when not done
(js/console.log :read-result result)
(if (.-done result)
(emit! {:done? (.-done result)})
(do (emit! {:chunk (.decode decoder (.-value result))})
(read-chunks reader emit!))))))))
found this approach nowwhat does amb>
mean in the above?
(defn read-chunks [reader]
(m/ap
(let [decoder (js/TextDecoder.)]
(loop []
(let [result (m/? (fn [s f] (.then (.read reader) s f) #()))]
(js/console.log :read-result result)
(if (.-done result)
{:done? (.-done result)}
(m/amb> {:chunk (.decode decoder (.-value result))}
(recur))))))))