Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created August 19, 2013 20:31
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 swannodette/6273757 to your computer and use it in GitHub Desktop.
Save swannodette/6273757 to your computer and use it in GitHub Desktop.
(defn tap
([in f] (tap in f (chan)))
([in f out]
(go (loop []
(let [v (<! in)]
(if-not (nil? v)
(do (f v)
(>! out v)
(recur))
:done))))
out))
(defprotocol IObservable
(-subscribe! [observable key chan])
(-unsubscribe! [observable key chan]))
(defn dispatch
([lookup] (dispatch lookup {}))
([lookup coll]
(let [table (atom coll)]
(reify
IObservable
(-subscribe! [_ key chan]
(swap! table update-in [key]
(fnil conj []) chan))
(-unsubscribe! [_ key chan]
(swap! table update-in [key]
#(into [] (filter #{chan} (get % key)))))
IFn
(-invoke [_ v]
(doseq [c (get @table (lookup v))]
(put! c v)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment