Skip to content

Commit

Permalink
Call after interceptor with db coeffect, if no db effect is produced
Browse files Browse the repository at this point in the history
after interceptors expect to always get a db value. If no db effect is
produced, then they may fail (for example schema validation on db if
no db is provided).

Fixes #239
  • Loading branch information
danielcompton committed Oct 12, 2016
1 parent e40ef73 commit dd34587
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
6 changes: 3 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## 0.8.1 (2016.08.XX) Unreleased

## 0.8.1 Unreleased

#### Improvements

- [#200](https://github.com/Day8/re-frame/pull/200) Remove trailing spaces from console logging
- [#200](https://github.com/Day8/re-frame/pull/200) Remove trailing spaces from console logging
- [

## 0.8.0 (2016.08.19)

Expand Down
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject re-frame "0.8.1"
(defproject re-frame "0.8.1-SNAPSHOT"
:description "A Clojurescript MVC-like Framework For Writing SPAs Using Reagent."
:url "https://github.com/Day8/re-frame.git"
:license {:name "MIT"}
Expand Down Expand Up @@ -65,4 +65,5 @@

:aliases {"test-once" ["do" "clean," "cljsbuild" "once" "test," "shell" "open" "test/test.html"]
"test-auto" ["do" "clean," "cljsbuild" "auto" "test,"]
"karma-once" ["do" "clean," "cljsbuild" "once" "karma,"]})
"karma-once" ["do" "clean," "cljsbuild" "once" "karma,"]
"karma-auto" ["do" "clean," "cljsbuild" "auto" "karma,"]})
2 changes: 1 addition & 1 deletion src/re_frame/interceptor.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"Add a collection of `interceptors` to the end of `context's` execution `:queue`.
Returns the updated `context`.
In an advanced case, this function would allow an interceptor could add new
In an advanced case, this function could allow an interceptor to add new
interceptors to the `:queue` of a context."
[context interceptors]
(update context :queue
Expand Down
9 changes: 6 additions & 3 deletions src/re_frame/std_interceptors.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@
"Interceptor factory which runs a given function `f` in the \"after\"
position, presumably for side effects.
`f` is called with two arguments: the `effects` value of `:db` and the event. It's return
value is ignored so `f` can only side-effect.
`f` is called with two arguments: the `effects` value of `:db`
(or the previous value of db if no db effect is returned) and the event.
Its return value is ignored so `f` can only side-effect.
Example use:
- `f` runs schema validation (reporting any errors found)
Expand All @@ -229,7 +230,9 @@
:id :after
:after (fn after-after
[context]
(let [db (get-effect context :db)
(let [db (or (get-effect context :db)
;; If no db effect is returned, we provide the original coeffect.
(get-coeffect context :db))
event (get-coeffect context :event)]
(f db event) ;; call f for side effects
context)))) ;; context is unchanged
Expand Down
17 changes: 13 additions & 4 deletions test/re-frame/interceptor_test.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(ns re-frame.interceptor-test
(:require [cljs.test :refer-macros [is deftest]]
[reagent.ratom :refer [atom]]
[reagent.ratom :refer [atom]]
[re-frame.interceptor :refer [context get-coeffect assoc-effect assoc-coeffect get-effect]]
[re-frame.std-interceptors :refer [trim-v path on-changes
db-handler->interceptor fx-handler->interceptor]]))
[re-frame.std-interceptors :refer [trim-v path on-changes after
db-handler->interceptor fx-handler->interceptor]]
[re-frame.interceptor :as interceptor]))

(enable-console-print!)

Expand Down Expand Up @@ -116,4 +117,12 @@
(get-effect :db))))))



(deftest test-after
(let [after-db-val (atom nil)]
(-> (context [:a :b]
[(after (fn [db] (reset! after-db-val db)))]
{:a 1})
(interceptor/invoke-interceptors :before)
interceptor/change-direction
(interceptor/invoke-interceptors :after))
(is (= @after-db-val {:a 1}))))

0 comments on commit dd34587

Please sign in to comment.