add clj-browserchannel-immutant-adapter

This commit is contained in:
Gered 2016-05-07 16:52:22 -04:00
parent 02b2f551e1
commit 1ade049b2b
2 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,7 @@
(defproject clj-browserchannel-immutant-adapter "0.0.1"
:description "Immutant async adapter for BrowserChannel"
:dependencies [[org.immutant/web "2.1.4"]
[net.thegeez/clj-browserchannel-server "0.2.2"]]
:profiles {:provided
{:dependencies
[[org.clojure/clojure "1.8.0"]]}})

View file

@ -0,0 +1,37 @@
(ns clj-browserchannel-immutant-adapter.core
(:require
[immutant.web :as iweb]
[immutant.web.async :as iasync]
[net.thegeez.async-adapter :as bc-async-adapter]))
(deftype ImmutantResponse [channel]
bc-async-adapter/IAsyncAdapter
(head [this status headers]
(let [headers (assoc headers "Transfer-Encoding" "chunked")]
(iasync/send! channel {:status status :headers headers})))
(write-chunk [this data]
(if (iasync/open? channel)
(iasync/send! channel data)
(throw bc-async-adapter/ConnectionClosedException)))
(close [this]
(iasync/close channel)))
(defn wrap-immutant-async-adapter
[handler]
(fn [request]
(let [resp (handler request)]
(if (= :http (:async resp))
(iasync/as-channel
request
{:on-open
(fn [channel]
(let [reactor (:reactor resp)
emit (ImmutantResponse. channel)]
(reactor emit)))})
resp))))
(defn run-immutant [handler options]
(-> handler
(wrap-immutant-async-adapter)
(iweb/run options)))