Created
February 21, 2014 01:16
-
-
Save whodidthis/9126971 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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