Skip to content

Commit

Permalink
Misc re-frame tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcompton committed Aug 23, 2016
1 parent 9d533d2 commit 6a86431
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
root = true

[*]
indent_style = tab
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
Expand Down
1 change: 0 additions & 1 deletion .idea/codeStyleSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions src/re_frame/fx.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@
call the registered effects handlers for each of the map's keys:
`:dispatch`, `:undo` and `:db`."
(->interceptor
:id :do-fx
:after (fn do-fx-after
[context]
(->> (:effects context)
(map (fn [[k value]]
(if-let [effect-fn (get-handler kind k true)]
(effect-fn value))))
doall))))
:id :do-fx
:after (fn do-fx-after
[context]
(doseq [[effect-k value] (:effects context)]
(if-let [effect-fn (get-handler kind effect-k true)]
(effect-fn value))))))

;; -- Builtin Effect Handlers ------------------------------------------------

Expand All @@ -54,8 +52,8 @@
(register
:dispatch-later
(fn [value]
(doseq [{:keys [ms dispatch] :as effect} value]
(if (or (empty? dispatch) (-> ms number? not))
(doseq [{:keys [ms dispatch] :as effect} value]
(if (or (empty? dispatch) (not (number? ms)))
(console :error "re-frame: ignoring bad :dispatch-later value:" effect)
(set-timeout! #(router/dispatch dispatch) ms)))))

Expand Down Expand Up @@ -106,8 +104,8 @@
(fn [value]
(let [clear-event (partial clear-handlers events/kind)]
(if (sequential? value)
(doall (map clear-event value))
(clear-event value)))))
(doseq [event (if (sequential? value) value [value])]
(clear-event event))))))


;; :db
Expand Down
12 changes: 8 additions & 4 deletions src/re_frame/interceptor.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@
(:coeffects context))
([context key]
(get-in context [:coeffects key]))
([context key not-fount]
(get-in context [:coeffects key] not-fount)))
([context key not-found]
(get-in context [:coeffects key] not-found)))

(defn assoc-coeffect
[context key value]
(assoc-in context [:coeffects key] value))

(defn update-coeffect
[context key f & args]
(apply update context key f args))

;; -- Execute Interceptor Chain ------------------------------------------------------------------


Expand Down Expand Up @@ -101,8 +105,8 @@
(let [interceptor (peek queue) ;; next interceptor to call
stack (:stack context)] ;; already completed interceptors
(recur (-> context
(assoc :queue (pop queue))
(assoc :stack (conj stack interceptor))
(assoc :queue (pop queue)
:stack (conj stack interceptor))
(invoke-interceptor-fn interceptor direction)))))))))


Expand Down
6 changes: 3 additions & 3 deletions src/re_frame/registrar.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@

([kind id required?]
(let [handler (get-handler kind id)]
(when debug-enabled?
(when (and required? (nil? handler))
(when debug-enabled? ;; This is in a separate when so Closure DCE can run
(when (and required? (nil? handler)) ;; Otherwise you'd need to type hint the and with a ^boolean for DCE.
(console :error "re-frame: no " (str kind) " handler registered for:" id)))
handler)))


(defn register-handler
[kind id handler-fn]
(when debug-enabled?
(when debug-enabled? ;; This is in a separate when so Closure DCE can run
(when (get-handler kind id false)
(console :warn "re-frame: overwriting" (str kind) "handler for:" id))) ;; allow it, but warn. Happens on figwheel reloads.
(swap! kind->id->handler assoc-in [kind id] handler-fn)
Expand Down
32 changes: 15 additions & 17 deletions src/re_frame/std_interceptors.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"contains re-frame supplied, standard interceptors"
(:require
[re-frame.interceptor :refer [->interceptor get-effect get-coeffect assoc-coeffect assoc-effect]]
[re-frame.loggers :refer [console]]
[re-frame.registrar :as registrar]
[re-frame.db :refer [app-db]]
[clojure.data :as data]))
[re-frame.loggers :refer [console]]
[re-frame.registrar :as registrar]
[re-frame.db :refer [app-db]]
[clojure.data :as data]
[re-frame.cofx :as cofx]))


;; XXX provide a way to set what handler should be called when there is no registered handler.
Expand Down Expand Up @@ -33,9 +34,9 @@
context)
:after (fn debug-after
[context]
(let [event (get-coeffect context :event)
orig-db (get-coeffect context :db)
new-db (get-effect context :db ::not-found)]
(let [event (get-coeffect context :event)
orig-db (get-coeffect context :db)
new-db (get-effect context :db ::not-found)]
(if (= new-db ::not-found)
(console :log "No :db changes caused by:" event)
(let [[only-before only-after] (data/diff orig-db new-db)
Expand All @@ -62,10 +63,7 @@
:id :trim-v
:before (fn trimv-before
[context]
(->> (get-coeffect context :event)
rest
vec
(assoc-coeffect context :event)))))
(update-in context [:coeffects :event] subvec 1))))


;; -- Interceptor Factories - PART 1 ---------------------------------------------------------------
Expand All @@ -91,8 +89,8 @@
:before (fn db-handler-before
[context]
(let [{:keys [db event]} (:coeffects context)]
(->> (handler-fn db event)
(assoc-effect context :db))))))
(->> (handler-fn db event)
(assoc-effect context :db))))))


(defn fx-handler->interceptor
Expand Down Expand Up @@ -153,7 +151,7 @@
(let [path (flatten args)
db-store-key :re-frame-path/db-store] ;; this is where, within `context`, we store the original dbs
(when (empty? path)
(console :error "re-frame: \"path\" interceptor given no params" ))
(console :error "re-frame: \"path\" interceptor given no params"))
(->interceptor
:id :path
:before (fn
Expand Down Expand Up @@ -272,9 +270,9 @@

;; if one of the inputs has changed, then run 'f'
(if changed-ins?
(->> (apply f new-ins)
(assoc-in new-db out-path)
(assoc-effect context :db))
(->> (apply f new-ins)
(assoc-in new-db out-path)
(assoc-effect context :db))
context)))))


4 changes: 3 additions & 1 deletion src/re_frame/subs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@
"Returns a new version of 'm' in which 'f' has been applied to each value.
(map-vals inc {:a 4, :b 2}) => {:a 5, :b 3}"
[f m]
(into {} (for [[k val] m] [k (f val)])))
(into (empty m)
(map (fn [[k v]] [k (f v)]))
m))


(defn- deref-input-signals
Expand Down

0 comments on commit 6a86431

Please sign in to comment.