From 3c9b194a61c5df61b5bbba833c327f6a54b9265d Mon Sep 17 00:00:00 2001 From: Konstantinos Kaloutas Date: Wed, 20 Nov 2024 18:45:15 +0200 Subject: [PATCH] refactor --- src/renderer/app/effects.cljs | 42 ++++++++++++++---------------- src/renderer/document/events.cljs | 2 -- src/renderer/history/effects.cljs | 18 ++++++------- src/renderer/history/handlers.cljs | 8 +++--- src/renderer/snap/events.cljs | 17 +++++------- src/renderer/tool/effects.cljs | 7 ++--- 6 files changed, 42 insertions(+), 52 deletions(-) diff --git a/src/renderer/app/effects.cljs b/src/renderer/app/effects.cljs index d6fb1b4d..c585346b 100644 --- a/src/renderer/app/effects.cljs +++ b/src/renderer/app/effects.cljs @@ -12,21 +12,15 @@ (rf.storage/reg-co-fx! config/app-key {:cofx :store}) -(defn persist! - "This is a modified version of akiroz.re-frame.storage/persist-db-keys - The before key is removed and we are dropping the rest of the history states - to get performance to an acceptable level and minimize resource allocation." - [db] - (let [db (cond-> db (:active-document db) history.h/drop-rest)] - (rf.storage/->store config/app-key (select-keys db db/persistent-keys)))) - (def persist (rf/->interceptor :id ::persist :after (fn [context] - (when-let [db (get-in context [:effects :db])] - (persist! db)) - context))) + (let [db (rf/get-effect context :db) + fx (rf/get-effect context :fx)] + (cond-> context + db + (rf/assoc-effect :fx (conj (or fx []) [::persist db]))))))) (rf/reg-cofx ::guid @@ -41,7 +35,8 @@ (rf/reg-fx ::persist (fn [db] - (persist! db))) + (let [db (cond-> db (:active-document db) history.h/drop-rest)] + (rf.storage/->store config/app-key (select-keys db db/persistent-keys))))) (rf/reg-fx ::local-storage-clear @@ -95,20 +90,21 @@ (fn [[k v]] (.setAttribute js/window.document.documentElement k v))) -(defn check-and-throw - "Throws an exception if `db` doesn't match the Spec" - [db event] - (when (not (db/valid? db)) - (js/console.error (str "Event: " (first event))) - (throw (js/Error. (str "Spec check failed: " (db/explain db)))))) +(rf/reg-fx + ::validate-db + (fn [[db event]] + (when (not (db/valid? db)) + (js/console.error (str "Event: " (first event))) + (throw (js/Error. (str "Spec check failed: " (db/explain db))))))) (def schema-validator (rf/->interceptor :id ::schema-validator :after (fn [context] - (let [db (if (contains? (rf/get-effect context) :db) - (rf/get-effect context :db) - (rf/get-coeffect context :db)) + (let [db (or (rf/get-effect context :db) + (rf/get-coeffect context :db)) + fx (rf/get-effect context :fx) event (rf/get-coeffect context :event)] - (check-and-throw db event) - context)))) + (cond-> context + db + (rf/assoc-effect :fx (conj (or fx []) [::validate-db [db event]]))))))) diff --git a/src/renderer/document/events.cljs b/src/renderer/document/events.cljs index 4f4801c6..26951123 100644 --- a/src/renderer/document/events.cljs +++ b/src/renderer/document/events.cljs @@ -93,7 +93,6 @@ (rf/reg-event-fx ::close-active - [persist] (fn [{:keys [db]} [_]] {:dispatch [::close (:active-document db) true]})) @@ -308,4 +307,3 @@ (-> db (h/set-active id) (h/center)))) - diff --git a/src/renderer/history/effects.cljs b/src/renderer/history/effects.cljs index 822dbe28..d67488b8 100644 --- a/src/renderer/history/effects.cljs +++ b/src/renderer/history/effects.cljs @@ -9,16 +9,14 @@ "Persists the current state on history position changes." (rf/->interceptor :id ::auto-persist - :before (fn [context] - (when-let [db (rf/get-coeffect context :db)] - (cond-> context - (:active-document db) - (rf/assoc-coeffect :history-position (history.h/position db))))) :after (fn [context] - (when-let [db (rf/get-effect context :db)] - (when-let [history-position (rf/get-coeffect context :history-position)] - (when (not= (history.h/position db) history-position) - (app.fx/persist! db)))) - context))) + (let [db (rf/get-effect context :db) + fx (rf/get-effect context :fx) + prev-position (when-let [db (rf/get-coeffect context :db)] + (when (:active-document db) + (history.h/position db)))] + (cond-> context + (and db (not= (history.h/position db) prev-position)) + (rf/assoc-effect :fx (conj (or fx []) [::app.fx/persist db]))))))) (rf/reg-global-interceptor auto-persist) diff --git a/src/renderer/history/handlers.cljs b/src/renderer/history/handlers.cljs index 442a2311..093ba5e1 100644 --- a/src/renderer/history/handlers.cljs +++ b/src/renderer/history/handlers.cljs @@ -155,12 +155,12 @@ [db [x y]] (assoc-in db (path db :translate) [x y])) -(m/=> create-state [:-> App int? uuid? string? HistoryState]) +(m/=> create-state [:-> App uuid? string? HistoryState]) (defn create-state - [db now id explanation] + [db id explanation] (let [new-state {:explanation explanation :elements (element.h/entities db) - :timestamp now + :timestamp (.now js/Date) ; REVIEW; Sideffect :index (state-count db) :id id :children []}] @@ -210,7 +210,7 @@ (-> db (assoc-in (path db :position) id) (assoc-in (path db :states id) - (create-state db (.now js/Date) id explanation)) + (create-state db id explanation)) (cond-> current-position (-> (update-in (path db :states current-position :children) conj id) diff --git a/src/renderer/snap/events.cljs b/src/renderer/snap/events.cljs index bbd4978a..cdcae80d 100644 --- a/src/renderer/snap/events.cljs +++ b/src/renderer/snap/events.cljs @@ -24,23 +24,20 @@ "Updates the kdtree when the selection changes, by adding/removing points." (rf/->interceptor :id ::auto-rebuild-tree - :before (fn [context] - (let [db (rf/get-coeffect context :db)] - (cond-> context - (:active-document db) - (rf/assoc-coeffect :non-selected-ids (element.h/non-selected-ids db))))) :after (fn [context] (let [db (rf/get-effect context :db)] (if (:active-document db) - (let [previous-non-selected-ids (rf/get-coeffect context :non-selected-ids) - non-selected-ids (element.h/non-selected-ids db)] + (let [non-selected-ids (element.h/non-selected-ids db) + prev-non-selected-ids (let [db (rf/get-coeffect context :db)] + (when (:active-document db) + (element.h/non-selected-ids db)))] (cond-> context - (not= non-selected-ids previous-non-selected-ids) + (not= non-selected-ids prev-non-selected-ids) (rf/assoc-effect :db (-> db - (h/insert-to-tree (set/difference non-selected-ids previous-non-selected-ids)) - (h/delete-from-tree (set/difference previous-non-selected-ids non-selected-ids)))))) + (h/insert-to-tree (set/difference non-selected-ids prev-non-selected-ids)) + (h/delete-from-tree (set/difference prev-non-selected-ids non-selected-ids)))))) context))))) (rf/reg-global-interceptor auto-rebuild-tree) diff --git a/src/renderer/tool/effects.cljs b/src/renderer/tool/effects.cljs index a801ed04..e5e1efcd 100644 --- a/src/renderer/tool/effects.cljs +++ b/src/renderer/tool/effects.cljs @@ -18,10 +18,11 @@ (rf/->interceptor :id ::custom-fx :after (fn [context] - (let [db (rf/get-effect context :db ::not-found)] + (let [db (rf/get-effect context :db) + fx (rf/get-effect context :fx)] (cond-> context - (not= db ::not-found) - (-> (rf/assoc-effect :fx (apply conj (or (:fx (rf/get-effect context)) []) (:fx db))) + db + (-> (rf/assoc-effect :fx (apply conj (or fx []) (:fx db))) (rf/assoc-effect :db (assoc db :fx [])))))))) (rf/reg-global-interceptor custom-fx)