Refactor: use go-loop macro
This commit is contained in:
parent
8891e402d0
commit
1b2f67a961
2 changed files with 17 additions and 17 deletions
|
@ -3,7 +3,7 @@
|
||||||
[cljs.core.async :as async :refer [<! >! chan close! timeout]])
|
[cljs.core.async :as async :refer [<! >! chan close! timeout]])
|
||||||
(:require-macros [dommy.macros :refer [deftemplate sel1 node]]
|
(:require-macros [dommy.macros :refer [deftemplate sel1 node]]
|
||||||
[cljs.core.async.macros :as m :refer [go alt! alts!]]
|
[cljs.core.async.macros :as m :refer [go alt! alts!]]
|
||||||
[grub-client.macros :refer [log]]))
|
[grub-client.macros :refer [log go-loop]]))
|
||||||
|
|
||||||
(deftemplate grub-template [grub]
|
(deftemplate grub-template [grub]
|
||||||
[:tr
|
[:tr
|
||||||
|
@ -60,20 +60,17 @@
|
||||||
(dommy/append! (sel1 :#grubList) (grub-template grub)))
|
(dommy/append! (sel1 :#grubList) (grub-template grub)))
|
||||||
|
|
||||||
(defn append-new-grubs [chan]
|
(defn append-new-grubs [chan]
|
||||||
(go (while true
|
(go-loop (let [grub (<! chan)]
|
||||||
(let [grub (<! chan)]
|
(append-new-grub grub))))
|
||||||
(append-new-grub grub)))))
|
|
||||||
|
|
||||||
(defn add-grubs-to-list [in]
|
(defn add-grubs-to-list [in]
|
||||||
(go (while true
|
(go-loop (let [new-grub (<! in)]
|
||||||
(let [new-grub (<! in)]
|
(append-new-grub new-grub))))
|
||||||
(append-new-grub new-grub)))))
|
|
||||||
|
|
||||||
(defn filter-empty-grubs [in]
|
(defn filter-empty-grubs [in]
|
||||||
(let [out (chan)]
|
(let [out (chan)]
|
||||||
(go (while true
|
(go-loop (let [grub (<! in)]
|
||||||
(let [grub (<! in)]
|
(when-not (empty? grub) (>! out grub))))
|
||||||
(when-not (empty? grub) (>! out grub)))))
|
|
||||||
out))
|
out))
|
||||||
|
|
||||||
(def websocket* (atom nil))
|
(def websocket* (atom nil))
|
||||||
|
@ -84,17 +81,15 @@
|
||||||
(let [grub (.-data event)]
|
(let [grub (.-data event)]
|
||||||
(log "Received grub:" grub)
|
(log "Received grub:" grub)
|
||||||
(append-new-grub grub))))
|
(append-new-grub grub))))
|
||||||
(go (while true
|
(go-loop (let [grub (<! chan)]
|
||||||
(let [grub (<! chan)]
|
(.send websocket grub)))))
|
||||||
(.send websocket grub))))))
|
|
||||||
|
|
||||||
(defn fan-out [in num-chans]
|
(defn fan-out [in num-chans]
|
||||||
(let [out-channels (repeatedly num-chans chan)]
|
(let [out-channels (repeatedly num-chans chan)]
|
||||||
(go (while true
|
(go-loop (let [x (<! in)]
|
||||||
(let [x (<! in)]
|
|
||||||
(doseq [out out-channels]
|
(doseq [out out-channels]
|
||||||
(>! out x)))))
|
(>! out x))))
|
||||||
out-channels))
|
out-channels))
|
||||||
|
|
||||||
(defn add-new-grubs-as-they-come []
|
(defn add-new-grubs-as-they-come []
|
||||||
(let [added-grubs (get-added-grubs)
|
(let [added-grubs (get-added-grubs)
|
||||||
|
|
|
@ -2,3 +2,8 @@
|
||||||
|
|
||||||
(defmacro log [& args]
|
(defmacro log [& args]
|
||||||
`(.log js/console ~@args))
|
`(.log js/console ~@args))
|
||||||
|
|
||||||
|
(defmacro go-loop [& body]
|
||||||
|
`(cljs.core.async.macros/go
|
||||||
|
(while true
|
||||||
|
~@body)))
|
||||||
|
|
Loading…
Add table
Reference in a new issue