Misc re-frame tweaks

This commit is contained in:
Daniel Compton 2016-08-24 00:47:22 +12:00
parent 9d533d2f23
commit 6a86431a44
7 changed files with 40 additions and 39 deletions

View File

@ -1,7 +1,7 @@
root = true root = true
[*] [*]
indent_style = tab indent_style = space
end_of_line = lf end_of_line = lf
charset = utf-8 charset = utf-8
trim_trailing_whitespace = true trim_trailing_whitespace = true

View File

@ -12,6 +12,5 @@
</value> </value>
</option> </option>
<option name="USE_PER_PROJECT_SETTINGS" value="true" /> <option name="USE_PER_PROJECT_SETTINGS" value="true" />
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default (2)" />
</component> </component>
</project> </project>

View File

@ -30,14 +30,12 @@
call the registered effects handlers for each of the map's keys: call the registered effects handlers for each of the map's keys:
`:dispatch`, `:undo` and `:db`." `:dispatch`, `:undo` and `:db`."
(->interceptor (->interceptor
:id :do-fx :id :do-fx
:after (fn do-fx-after :after (fn do-fx-after
[context] [context]
(->> (:effects context) (doseq [[effect-k value] (:effects context)]
(map (fn [[k value]] (if-let [effect-fn (get-handler kind effect-k true)]
(if-let [effect-fn (get-handler kind k true)] (effect-fn value))))))
(effect-fn value))))
doall))))
;; -- Builtin Effect Handlers ------------------------------------------------ ;; -- Builtin Effect Handlers ------------------------------------------------
@ -54,8 +52,8 @@
(register (register
:dispatch-later :dispatch-later
(fn [value] (fn [value]
(doseq [{:keys [ms dispatch] :as effect} value] (doseq [{:keys [ms dispatch] :as effect} value]
(if (or (empty? dispatch) (-> ms number? not)) (if (or (empty? dispatch) (not (number? ms)))
(console :error "re-frame: ignoring bad :dispatch-later value:" effect) (console :error "re-frame: ignoring bad :dispatch-later value:" effect)
(set-timeout! #(router/dispatch dispatch) ms))))) (set-timeout! #(router/dispatch dispatch) ms)))))
@ -106,8 +104,8 @@
(fn [value] (fn [value]
(let [clear-event (partial clear-handlers events/kind)] (let [clear-event (partial clear-handlers events/kind)]
(if (sequential? value) (if (sequential? value)
(doall (map clear-event value)) (doseq [event (if (sequential? value) value [value])]
(clear-event value))))) (clear-event event))))))
;; :db ;; :db

View File

@ -52,13 +52,17 @@
(:coeffects context)) (:coeffects context))
([context key] ([context key]
(get-in context [:coeffects key])) (get-in context [:coeffects key]))
([context key not-fount] ([context key not-found]
(get-in context [:coeffects key] not-fount))) (get-in context [:coeffects key] not-found)))
(defn assoc-coeffect (defn assoc-coeffect
[context key value] [context key value]
(assoc-in context [:coeffects key] value)) (assoc-in context [:coeffects key] value))
(defn update-coeffect
[context key f & args]
(apply update context key f args))
;; -- Execute Interceptor Chain ------------------------------------------------------------------ ;; -- Execute Interceptor Chain ------------------------------------------------------------------
@ -101,8 +105,8 @@
(let [interceptor (peek queue) ;; next interceptor to call (let [interceptor (peek queue) ;; next interceptor to call
stack (:stack context)] ;; already completed interceptors stack (:stack context)] ;; already completed interceptors
(recur (-> context (recur (-> context
(assoc :queue (pop queue)) (assoc :queue (pop queue)
(assoc :stack (conj stack interceptor)) :stack (conj stack interceptor))
(invoke-interceptor-fn interceptor direction))))))))) (invoke-interceptor-fn interceptor direction)))))))))

View File

@ -26,15 +26,15 @@
([kind id required?] ([kind id required?]
(let [handler (get-handler kind id)] (let [handler (get-handler kind id)]
(when debug-enabled? (when debug-enabled? ;; This is in a separate when so Closure DCE can run
(when (and required? (nil? handler)) (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))) (console :error "re-frame: no " (str kind) " handler registered for:" id)))
handler))) handler)))
(defn register-handler (defn register-handler
[kind id handler-fn] [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) (when (get-handler kind id false)
(console :warn "re-frame: overwriting" (str kind) "handler for:" id))) ;; allow it, but warn. Happens on figwheel reloads. (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) (swap! kind->id->handler assoc-in [kind id] handler-fn)

View File

@ -2,10 +2,11 @@
"contains re-frame supplied, standard interceptors" "contains re-frame supplied, standard interceptors"
(:require (:require
[re-frame.interceptor :refer [->interceptor get-effect get-coeffect assoc-coeffect assoc-effect]] [re-frame.interceptor :refer [->interceptor get-effect get-coeffect assoc-coeffect assoc-effect]]
[re-frame.loggers :refer [console]] [re-frame.loggers :refer [console]]
[re-frame.registrar :as registrar] [re-frame.registrar :as registrar]
[re-frame.db :refer [app-db]] [re-frame.db :refer [app-db]]
[clojure.data :as data])) [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. ;; XXX provide a way to set what handler should be called when there is no registered handler.
@ -33,9 +34,9 @@
context) context)
:after (fn debug-after :after (fn debug-after
[context] [context]
(let [event (get-coeffect context :event) (let [event (get-coeffect context :event)
orig-db (get-coeffect context :db) orig-db (get-coeffect context :db)
new-db (get-effect context :db ::not-found)] new-db (get-effect context :db ::not-found)]
(if (= new-db ::not-found) (if (= new-db ::not-found)
(console :log "No :db changes caused by:" event) (console :log "No :db changes caused by:" event)
(let [[only-before only-after] (data/diff orig-db new-db) (let [[only-before only-after] (data/diff orig-db new-db)
@ -62,10 +63,7 @@
:id :trim-v :id :trim-v
:before (fn trimv-before :before (fn trimv-before
[context] [context]
(->> (get-coeffect context :event) (update-in context [:coeffects :event] subvec 1))))
rest
vec
(assoc-coeffect context :event)))))
;; -- Interceptor Factories - PART 1 --------------------------------------------------------------- ;; -- Interceptor Factories - PART 1 ---------------------------------------------------------------
@ -91,8 +89,8 @@
:before (fn db-handler-before :before (fn db-handler-before
[context] [context]
(let [{:keys [db event]} (:coeffects context)] (let [{:keys [db event]} (:coeffects context)]
(->> (handler-fn db event) (->> (handler-fn db event)
(assoc-effect context :db)))))) (assoc-effect context :db))))))
(defn fx-handler->interceptor (defn fx-handler->interceptor
@ -153,7 +151,7 @@
(let [path (flatten args) (let [path (flatten args)
db-store-key :re-frame-path/db-store] ;; this is where, within `context`, we store the original dbs db-store-key :re-frame-path/db-store] ;; this is where, within `context`, we store the original dbs
(when (empty? path) (when (empty? path)
(console :error "re-frame: \"path\" interceptor given no params" )) (console :error "re-frame: \"path\" interceptor given no params"))
(->interceptor (->interceptor
:id :path :id :path
:before (fn :before (fn
@ -272,9 +270,9 @@
;; if one of the inputs has changed, then run 'f' ;; if one of the inputs has changed, then run 'f'
(if changed-ins? (if changed-ins?
(->> (apply f new-ins) (->> (apply f new-ins)
(assoc-in new-db out-path) (assoc-in new-db out-path)
(assoc-effect context :db)) (assoc-effect context :db))
context))))) context)))))

View File

@ -87,7 +87,9 @@
"Returns a new version of 'm' in which 'f' has been applied to each value. "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}" (map-vals inc {:a 4, :b 2}) => {:a 5, :b 3}"
[f m] [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 (defn- deref-input-signals