From dfe5acb1e638bb656fe7193f2c805a45026010eb Mon Sep 17 00:00:00 2001 From: Inge Solvoll Date: Wed, 14 Oct 2020 11:42:18 +0200 Subject: [PATCH] Re frame 1.0.0 (#98) * re-frame 1.0.0 * Latest expound * Noisy logging no longer a problem in re-frame 1.0 * Using new re-frame api for global interceptors * 1.0 version range * re-frame 1.0 final * Branch-specific version to avoid confusion * Logging for app debugging * Missing require --- project.clj | 8 ++++---- src/kee_frame/controller.cljc | 17 ++++++++++++----- src/kee_frame/core.cljc | 5 ++--- src/kee_frame/fsm/alpha.cljc | 32 ++++++++++++++++---------------- src/kee_frame/interceptors.cljc | 27 --------------------------- src/kee_frame/log.cljc | 9 +-------- src/kee_frame/router.cljc | 5 ++++- src/kee_frame/spec.cljc | 6 +++--- src/kee_frame/state.cljc | 5 ----- 9 files changed, 42 insertions(+), 72 deletions(-) delete mode 100644 src/kee_frame/interceptors.cljc diff --git a/project.clj b/project.clj index e133653..bb43289 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject kee-frame "0.4.1-SNAPSHOT" +(defproject kee-frame "1.0.0-re-frame-1.0.0" :description "A micro-framework on top of re-frame" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} @@ -6,15 +6,15 @@ :min-lein-version "2.0.0" :dependencies [[org.clojure/clojure "1.10.1"] [org.clojure/clojurescript "1.10.597"] - [re-frame "0.12.0"] + [re-frame "1.0.0"] [re-chain "1.1"] - [metosin/reitit-core "0.4.2"] + [metosin/reitit-core "0.5.6"] [day8.re-frame/http-fx "v0.2.0"] [cljs-ajax "0.8.0"] [com.taoensso/timbre "4.10.0"] [venantius/accountant "0.2.5"] [org.clojure/core.match "1.0.0"] - [expound "0.8.4"] + [expound "0.8.5"] [day8.re-frame/test "0.1.5"] [breaking-point "0.1.2"] [pez/clerk "1.0.0"]] diff --git a/src/kee_frame/controller.cljc b/src/kee_frame/controller.cljc index d1a59da..7da4e6f 100644 --- a/src/kee_frame/controller.cljc +++ b/src/kee_frame/controller.cljc @@ -19,8 +19,11 @@ (defn validate-and-dispatch! [dispatch] (when dispatch + (log/debug "Dispatch returned from function " dispatch) (if (map? dispatch) - [::fsm/start dispatch] + (do + (log/debug "Starting fsm from controller " dispatch) + [::fsm/start dispatch]) (do (when-not (s/valid? ::spec/event-vector dispatch) (e/expound ::spec/event-vector dispatch) @@ -28,14 +31,18 @@ (s/explain-data ::spec/event-vector dispatch)))) dispatch)))) -(defn stop-controller [ctx {:keys [id stop]}] - (log/debug "Stopping controller: " id) +(defn stop-controller [ctx {:keys [stop] :as controller}] + (log/debug {:type :controller-stop + :controller controller + :ctx ctx}) (cond (vector? stop) stop (ifn? stop) (validate-and-dispatch! (stop ctx)))) -(defn start-controller [ctx {:keys [id last-params start]}] - (log/debug "Starting controller: " id) +(defn start-controller [ctx {:keys [last-params start] :as controller}] + (log/debug {:type :controller-start + :controller controller + :ctx ctx}) (when start (cond (vector? start) (conj start last-params) diff --git a/src/kee_frame/core.cljc b/src/kee_frame/core.cljc index 028aaa4..8442d08 100644 --- a/src/kee_frame/core.cljc +++ b/src/kee_frame/core.cljc @@ -4,14 +4,13 @@ [re-chain.core :as chain] [re-frame.core :as rf :refer [console]] [kee-frame.log :as log] - [kee-frame.spec :as spec :refer [spec-interceptor]] + [kee-frame.spec :as spec] [kee-frame.event-logger :as event-logger] - [kee-frame.interceptors :as i] [clojure.spec.alpha :as s] [expound.alpha :as e])) ;; Interceptors used by all chains and events registered through kee-frame -(def kee-frame-interceptors [i/add-global-interceptors (spec-interceptor state/app-db-spec) event-logger/interceptor rf/trim-v]) +(def kee-frame-interceptors [event-logger/interceptor rf/trim-v]) (def valid-option-key? #{:router :hash-routing? :routes :process-route :debug? :debug-config :chain-links :app-db-spec :root-component :initial-db diff --git a/src/kee_frame/fsm/alpha.cljc b/src/kee_frame/fsm/alpha.cljc index aca27f8..741ddf9 100644 --- a/src/kee_frame/fsm/alpha.cljc +++ b/src/kee_frame/fsm/alpha.cljc @@ -2,16 +2,13 @@ (:require [clojure.spec.alpha :as s] [re-frame.core :as f] [kee-frame.interop :as interop] - [kee-frame.interceptors :as i] [taoensso.timbre :as log] #?(:cljs [reagent.core :as r]))) (defn reg-no-op "Convenience function for declaring no-op events." [id] - (f/reg-event-fx id - [i/add-global-interceptors] - (constantly nil))) + (f/reg-event-fx id (constantly nil))) (defn find-transition "Try to find a transition that matches some subset of the received event" @@ -25,13 +22,16 @@ there is a valid transition, `nil` otherwise. Event transition `:when` clause is optionally applied." [transitions event] - (when-let [transition (find-transition transitions event)] + (if-let [transition (find-transition transitions event)] (let [{next-state :to dispatch :dispatch} transition] (when dispatch (doseq [e dispatch] (f/dispatch e))) - next-state))) + next-state) + (log/trace {:type :fsm-transition-missed + :transitions transitions + :event event}))) (defn foreign-event? [fsm-id [event-id _ event-fsm-id]] (and (#{::on-enter ::timeout} event-id) @@ -103,19 +103,19 @@ (f/dispatch [::stop fsm])) (assoc-in db [id state-attr] (or next-state current-state start)))) - (f/reg-fx ::start - (fn [{:keys [id] :as fsm}] - (let [timeouts* (atom nil) - state->timeouts (compile-timeouts fsm)] - (->> (partial advance fsm timeouts* state->timeouts) - f/enrich - (i/reg-global-interceptor id))))) + (fn [{:keys [id] :as fsm}] + (let [timeouts* (atom nil) + state->timeouts (compile-timeouts fsm)] + (-> (partial advance fsm timeouts* state->timeouts) + f/enrich + (assoc :id id) + (f/reg-global-interceptor))))) (f/reg-fx ::stop - (fn [{:keys [id]}] - (when id - (i/clear-global-interceptor id)))) + (fn [{:keys [id]}] + (when id + (f/clear-global-interceptor id)))) (f/reg-event-fx ::start ;; Starts the interceptor for the given fsm. diff --git a/src/kee_frame/interceptors.cljc b/src/kee_frame/interceptors.cljc deleted file mode 100644 index 4f67725..0000000 --- a/src/kee_frame/interceptors.cljc +++ /dev/null @@ -1,27 +0,0 @@ -(ns kee-frame.interceptors - (:require [re-frame.core :as f] - [re-frame.registrar :as reg])) - -(defn reg-global-interceptor - [id interceptor] - (reg/register-handler :global-interceptor id interceptor)) - -(defn clear-global-interceptor - [id] - (swap! reg/kind->id->handler - update-in - [:global-interceptor] - dissoc id)) - -(def add-global-interceptors - "Adds global interceptors to the beginning of queue." - (letfn [(cut-in-queue [queue xs] - (let [q (empty queue)] - (into q (concat xs queue)))) - (add-global-interceptors* [context] - (let [globals (vals (reg/get-handler :global-interceptor))] - (update context :queue cut-in-queue globals)))] - (f/->interceptor - :id :add-global-interceptors - :before add-global-interceptors*))) - diff --git a/src/kee_frame/log.cljc b/src/kee_frame/log.cljc index 725b291..b78a221 100644 --- a/src/kee_frame/log.cljc +++ b/src/kee_frame/log.cljc @@ -1,6 +1,5 @@ (ns kee-frame.log (:require [re-frame.core :as rf] - [re-frame.loggers :as rfl] [taoensso.timbre :as log])) (def config @@ -15,10 +14,4 @@ (rf/reg-fx :log (fn [[level & vargs :as arg]] (when arg - (log/log level vargs)))) - -#?(:cljs - (rfl/set-loggers! - {:warn (fn [& args] - (when-not (re-find #"^re-frame: overwriting" (first args)) - (apply js/console.warn args)))})) \ No newline at end of file + (log/log level vargs)))) \ No newline at end of file diff --git a/src/kee_frame/router.cljc b/src/kee_frame/router.cljc index 92f4dbd..458a0f8 100644 --- a/src/kee_frame/router.cljc +++ b/src/kee_frame/router.cljc @@ -5,6 +5,7 @@ [kee-frame.event-logger :as event-logger] [kee-frame.api :as api :refer [dispatch-current! navigate! url->data data->url]] [kee-frame.interop :as interop] + [kee-frame.spec :as spec] [kee-frame.state :as state] [kee-frame.scroll :as scroll] [kee-frame.controller :as controller] @@ -12,6 +13,7 @@ [clojure.string :as str] [clojure.spec.alpha :as s] [expound.alpha :as e] + [re-frame.core :as f] [clojure.set :as set])) (def default-chain-links [{:effect-present? (fn [effects] (:http-xhrio effects)) @@ -119,7 +121,8 @@ :or {scroll true} :as config}] (deprecations config) - (reset! state/app-db-spec app-db-spec) + (when app-db-spec + (f/reg-global-interceptor (spec/spec-interceptor app-db-spec))) (chain/configure! (concat default-chain-links chain-links)) diff --git a/src/kee_frame/spec.cljc b/src/kee_frame/spec.cljc index 651430e..9e4826f 100644 --- a/src/kee_frame/spec.cljc +++ b/src/kee_frame/spec.cljc @@ -45,11 +45,11 @@ (log-spec-error new-db db-spec) (assoc-effect context :db (get-coeffect context :db)))) -(defn spec-interceptor [db-spec-atom] +(defn spec-interceptor [db-spec] (->interceptor :id :spec :after (fn [context] (let [new-db (get-effect context :db)] - (if (and @db-spec-atom new-db (not (s/valid? @db-spec-atom new-db))) - (rollback context new-db @db-spec-atom) + (if (and new-db (not (s/valid? db-spec new-db))) + (rollback context new-db db-spec) context))))) \ No newline at end of file diff --git a/src/kee_frame/state.cljc b/src/kee_frame/state.cljc index da9a083..6f59c21 100644 --- a/src/kee_frame/state.cljc +++ b/src/kee_frame/state.cljc @@ -6,19 +6,14 @@ (def navigator (atom nil)) -(def app-db-spec (atom nil)) - (def debug? (atom false)) (def debug-config (atom nil)) -(def fsm-interceptors (atom {})) - (def breakpoints-initialized? (atom false)) ;; Test utility (defn reset-state! [] (reset! controllers {}) - (reset! fsm-interceptors {}) (reset! router nil) (reset! navigator nil)) \ No newline at end of file