(ns async
(:require #?(:clj [clojure.core.async :as a :refer [go-loop]])
#?@(:cljs [[cljs.core.async :as a]
[cljs.core.async.macros :refer [go-loop]]])))
(defmacro dochan
"Wrap the expressions in a go-loop over the single binding to a channel
till the chan closes"
[chan & body]
(let [[name channel-expr] binding]
`(let [channel# ~channel-expr]
(a/go-loop [~name (a/<! channel#)]
(if (some? ~name)
(do
~@body
(recur (a/<! channel#)))
(a/close! channel#))))))
;; in CLJS
(ns cljs
(:require [cljs.core.async :as a]))
(dochan [e (a/chan 10)]
(println "Auto-insert" e)) ;; keeps throwing exception saying it cannot find clojure.core.async