Skip to content

Instantly share code, notes, and snippets.

@sonwh98
Created September 24, 2015 16:09
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 sonwh98/a5d34e47577bcd333eaf to your computer and use it in GitHub Desktop.
Save sonwh98/a5d34e47577bcd333eaf to your computer and use it in GitHub Desktop.
sub pub blocks subscriber from recieving messages when there is more than one subscriber to a topic
(require-macros '[cljs.core.async.macros :refer [go go-loop]])
(require '[cljs.core.async :refer [put! <! >! chan pub sub unsub]])
(defonce message-bus (chan))
(defonce message-publication (pub message-bus (fn [msg]
(if (vector? msg)
(first msg)
:no-topic))))
(def s1 (chan))
(def s2 (chan))
(sub message-publication :foo s1)
(go (while true (println "s1 " (<! s1))))
(put! message-bus [:foo "bar"]) ;works as expected
(sub message-publication :foo s2) ;blocks s1 from recieving messages
(put! message-bus [:foo "bar"]) ;s1 cannot recieve this message
(go (while true (println "s2 " (<! s2)))) ; unblocks s1; both s1 and s2 can recieve messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment