Skip to content

Instantly share code, notes, and snippets.

@whodidthis
Created February 21, 2014 01:16
Show Gist options
  • Select an option

  • Save whodidthis/9126971 to your computer and use it in GitHub Desktop.

Select an option

Save whodidthis/9126971 to your computer and use it in GitHub Desktop.
(ns pondering
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
(:require [cljs.core.async :refer [<! >! put! take! close! chan timeout]]))
; The "library" part
(def unique (atom 0))
(def id-channel (atom {}))
(def output (chan))
(def input (chan))
(defn request
[id]
(let [channel (chan)]
(swap! id-channel assoc id channel)
channel))
(defn close-request!
[channel id]
(close! channel)
(swap! id-channel dissoc id))
(go-loop []
(let [[id & message] (<! input)]
(when-let [channel (get @id-channel id)]
(>! channel message))
(recur)))
(go-loop []
(<! output)
(recur))
; How to use it
(let [id (swap! unique inc)
channel (request id)]
(go
(>! output ["get" id "my-stuff"])
(let [message (<! channel)]
(println "This message was recived as risponse:" message)
(close-request! channel id))))
; simulate message from server
(put! input [1 "your-stuff"])
@id-channel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment