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
[*]
indent_style = tab
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true

View File

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

View File

@ -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 ------------------------------------------------
@ -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)))))
@ -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

View File

@ -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 ------------------------------------------------------------------
@ -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)))))))))

View File

@ -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)

View File

@ -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.
@ -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)
@ -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 ---------------------------------------------------------------
@ -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
@ -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
@ -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)))))

View File

@ -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