Skip to content

Commit

Permalink
async handlers (#4)
Browse files Browse the repository at this point in the history
* async handlers

* fix test

* split async tests

* fix indent

* 0.2.1
  • Loading branch information
darkleaf authored Feb 13, 2017
1 parent b069c99 commit 4cba502
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 28 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject darkleaf/router "0.2.0"
(defproject darkleaf/router "0.2.1"
:description "Bidirectional Ring router. REST oriented."
:url "https://github.com/darkleaf/router"
:license {:name "Eclipse Public License"
Expand Down
13 changes: 6 additions & 7 deletions src/darkleaf/router/action_impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
(:require [darkleaf.router.keywords :as k]
[darkleaf.router.protocols :as p]))

(defn- handle-with-middlewares [req handler]
(defn- process-with-middlewares [req handler]
(if (empty? (k/middlewares req))
(-> req
(dissoc k/middlewares)
(handler))
(let [req (dissoc req k/middlewares)]
[req handler])
(let [middleware (peek (k/middlewares req))
new-req (update req k/middlewares pop)
new-handler (middleware handler)]
(recur new-req new-handler))))

(defrecord Action [id handle-impl fill-impl handler]
p/Item
(handle [_ req]
(process [_ req]
(some-> req
(handle-impl)
(dissoc k/segments)
(assoc k/action id)
(handle-with-middlewares handler)))
(process-with-middlewares handler)))
(fill [_ req]
(when (and (= id (k/action req))
(empty? (k/scope req)))
Expand All @@ -31,7 +30,7 @@
(let [handle-impl (fn [req]
(when (and (= request-method (:request-method req))
(empty? (k/segments req)))
req))
req))
fill-impl (fn [req]
(-> req
(assoc :request-method request-method)))]
Expand Down
4 changes: 2 additions & 2 deletions src/darkleaf/router/composite_impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

(defrecord Composite [children]
p/Item
(handle [_ req]
(p/some-handle req children))
(process [_ req]
(p/some-process req children))
(fill [_ req]
(p/some-fill req children)))

Expand Down
27 changes: 18 additions & 9 deletions src/darkleaf/router/helpers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@
(assoc r :uri (segments->uri (k/segments r)))
(dissoc r k/action k/scope k/params k/segments))))))


(defn make-handler [item]
(let [request-for (make-request-for item)]
(fn [req]
(as-> req r
(assoc r k/request-for request-for)
(assoc r k/scope empty-scope)
(assoc r k/params {})
(assoc r k/segments (uri->segments (:uri r)))
(assoc r k/middlewares empty-middlewares)
(p/handle item r)))))
(let [request-for (make-request-for item)
set-init (fn [req]
(assoc req
k/request-for request-for
k/scope empty-scope
k/params {}
k/segments (uri->segments (:uri req))
k/middlewares empty-middlewares))]
(fn
([req]
(let [req (set-init req)
[req handler] (p/process item req)]
(handler req)))
([req resp raise]
(let [req (set-init req)
[req handler] (p/process item req)]
(handler req resp raise))))))
2 changes: 1 addition & 1 deletion src/darkleaf/router/nil_item_impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

(deftype NilItem []
p/Item
(handle [_ _] nil)
(process [_ _] nil)
(fill [_ _] nil))

(defn nil-item []
Expand Down
10 changes: 6 additions & 4 deletions src/darkleaf/router/protocols.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
(:require [darkleaf.router.keywords :as k]))

(defprotocol Item
(handle [this req])
(fill [this req-template]))
(process [this req]
"return [req handler]")
(fill [this req-template]
"return req"))

(defn some-handle [req xs]
(some #(handle % req) xs))
(defn some-process [req xs]
(some #(process % req) xs))

(defn some-fill [req xs]
(some #(fill % req) xs))
4 changes: 2 additions & 2 deletions src/darkleaf/router/scope_impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

(defrecord Scope [id handle-impl fill-impl middleware children]
p/Item
(handle [_ req]
(process [_ req]
(some-> req
(handle-impl)
(update k/scope conj id)
(update k/middlewares conj middleware)
(p/some-handle children)))
(p/some-process children)))
(fill [_ req]
(when (= id (peek (k/scope req)))
(-> req
Expand Down
4 changes: 2 additions & 2 deletions src/darkleaf/router/wrapper_impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

(defrecord Wrapper [middleware children]
p/Item
(handle [_ req]
(process [_ req]
(-> req
(update k/middlewares conj middleware)
(p/some-handle children)))
(p/some-process children)))
(fill [_ req]
(p/some-fill req children)))

Expand Down
18 changes: 18 additions & 0 deletions test/darkleaf/router_async_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns darkleaf.router-async-test
(:require [darkleaf.router :as r]
[clojure.test :refer [deftest is]]))

(deftest async-handler
(let [pages-controller {:index (fn [req resp raise]
(future
(resp "index resp"))
:something)}
pages (r/resources :pages :page pages-controller)
handler (r/make-handler pages)
test-req {:uri "/pages", :request-method :get}
done? (promise)
check (fn [val]
(is (= "index resp" val))
(deliver done? true))]
(handler test-req check check)
(is (deref done? 100 false))))
17 changes: 17 additions & 0 deletions test/darkleaf/router_async_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns darkleaf.router-async-test
(:require [darkleaf.router :as r])
(:import [goog.async nextTick])
(:require-macros [cljs.test :refer [deftest is async]]))

(deftest async-handler
(async done
(let [pages-controller {:index (fn [req resp raise]
(nextTick #(resp "index resp"))
:something)}
pages (r/resources :pages :page pages-controller)
handler (r/make-handler pages)
test-req {:uri "/pages", :request-method :get}
check (fn [val]
(is (= "index resp" val))
(done))]
(handler test-req check check))))
2 changes: 2 additions & 0 deletions test/darkleaf/test_runner.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns darkleaf.test-runner
(:require [doo.runner :refer-macros [doo-tests]]
[darkleaf.router-test]
[darkleaf.router-async-test]
[darkleaf.router.util-test]))

(doo-tests 'darkleaf.router-test
'darkleaf.router-async-test
'darkleaf.router.util-test)

0 comments on commit 4cba502

Please sign in to comment.