 
 ;; -- Interceptor Factories - PART 1 ---------------------------------------------------------------
 ;;
-;; These factories wrap handlers.
-
-;; XXX add names to annonomous `fn`
+;; These factories wrap the 3 kinds of handlers.
 ;;
+
 (defn db-handler->interceptor
   "Returns an interceptor which wraps the kind of event handler given to `reg-event-db`.
 

  [handler-fn]
  (->interceptor
    :name   :fx-handler

    :before (fn [context]
              (let [{:keys [event] :as coeffects} (:coeffects context)]
                (->> (handler-fn coeffects event)
                     (assoc context :effects))))))


(defn ctx-handler->interceptor

;; -- Interceptors Factories -  PART 2 ------------------------------------------------------------
;;

  Implementation:
    - in :before, store the original db in within `context`
    - ib :after, re-establish original db with modification
    - remember: path may be used twice within the one interceptor chain.
  "
  [& args]
  (let [path   (flatten args)
       db-store-key :re-frame-path/original-dbs]    ;; this is where, within `context`, we store the original db
    (when (empty? path)
      (console :error "re-frame: \"path\" interceptor given no params."))
    (->interceptor
      :name    :path
      :before  (fn
                 [context]
                 (let [original-db (get-coeffect context :db)]
                   (-> context
                       (update db-store-key conj original-db)
                       (assoc-coeffect :db (get-in original-db path)))))
      :after   (fn [context]
                 (let [db-store    (db-store-key context)
                       original-db (peek db-store)
                       new-store   (pop db-store)
                       full-db      (->> (get-effect context :db)
                                        (assoc-in original-db path))]
                   (-> context
                        (assoc db-store-key new-store)
                        (assoc-effect :db full-db)))))))



