Skip to content

Instantly share code, notes, and snippets.

@aamedina
Created December 3, 2013 22:11
Show Gist options
  • Select an option

  • Save aamedina/7778465 to your computer and use it in GitHub Desktop.

Select an option

Save aamedina/7778465 to your computer and use it in GitHub Desktop.
(defn xhr
[uri method content headers]
(let [c (chan)
m (mult c)]
(if-let [cached (get-cached uri)]
(let [tapped (tap m (chan))]
(put! tapped cached)
tapped)
(if-let [source (get @outstanding-requests uri)]
(tap source (chan))
(try (.send xhr-pool uri uri method
(if (and (not (string? content))
(not (nil? content)))
(stringify content)
content)
(clj->js headers) 50
#(do (put! c (handle-response (.-target %)))
(swap! outstanding-requests dissoc uri))
0)
(swap! outstanding-requests assoc uri m)
(tap m (chan))
(catch js/Error e nil))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment