Skip to content

Instantly share code, notes, and snippets.

@jaydeesimon
Created May 31, 2016 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaydeesimon/94edf60291abde68b42c6640bef6f7d7 to your computer and use it in GitHub Desktop.
Save jaydeesimon/94edf60291abde68b42c6640bef6f7d7 to your computer and use it in GitHub Desktop.
Trying to work out how to structure my core.async channel
(ns core-async-playground.core
(:require [clojure.core.async
:as a
:refer [>! <! >!! <!! go go-loop chan buffer close! thread
alts! alts!! timeout]])
(:gen-class))
(defn create-bulb-chans [bulbs]
(into {} (map (fn [bulb] [bulb (chan)]) bulbs)))
(defn bulb-chan-listeners [bulb-chans]
(doseq [[bulb c] bulb-chans]
(go-loop []
(let [val (<! c)]
(if (= :close val)
(close! c)
(do (println (format "Got value from %s: %s" bulb val))
(recur)))))))
(defn health-chan []
(let [c (chan)]
(go-loop []
(let [[v _] (alts! [c (timeout 10000)])]
(if v
(println "Doing health check because it was requested.")
(println "Doing health check because it's been at least 10 seconds.")))
(recur))
c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment