Created
May 31, 2016 20:49
-
-
Save jaydeesimon/94edf60291abde68b42c6640bef6f7d7 to your computer and use it in GitHub Desktop.
Trying to work out how to structure my core.async channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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