From 742da4a0f7b3c4ea4b9a7ff825fb889d45838351 Mon Sep 17 00:00:00 2001 From: Konstantinos Kaloutas Date: Wed, 20 Nov 2024 18:57:54 +0200 Subject: [PATCH] refactor --- src/dev.cljs | 6 ++--- src/renderer/app/effects.cljs | 22 ----------------- src/renderer/app/events.cljs | 24 +++++++++++++++++- src/renderer/core.cljs | 2 +- src/renderer/document/events.cljs | 4 +-- src/renderer/frame/events.cljs | 3 +-- src/renderer/history/effects.cljs | 22 ----------------- src/renderer/history/events.cljs | 14 +++++++++++ src/renderer/ruler/events.cljs | 2 +- src/renderer/snap/events.cljs | 41 ++++++++++++++----------------- src/renderer/theme/events.cljs | 3 ++- src/renderer/tool/effects.cljs | 13 ---------- src/renderer/tool/events.cljs | 11 +++++++++ test/benchmark_test.cljs | 9 +++---- test/core_test.cljs | 7 +++--- 15 files changed, 83 insertions(+), 100 deletions(-) delete mode 100644 src/renderer/history/effects.cljs diff --git a/src/dev.cljs b/src/dev.cljs index e1ecba09..d8085c78 100644 --- a/src/dev.cljs +++ b/src/dev.cljs @@ -5,12 +5,12 @@ [clojure.string :as str] [malli.dev.cljs :as dev] [re-frame.core :as rf] - [renderer.app.effects :as app.fx])) + [renderer.app.events :as app.e])) (comment ;; Enable full db validation - (rf/reg-global-interceptor app.fx/schema-validator) - (rf/clear-global-interceptor ::app.fx/schema-validator) + (rf/reg-global-interceptor app.e/schema-validator) + (rf/clear-global-interceptor ::app.e/schema-validator) ;; Enable function instrumentation ;; https://github.com/metosin/malli/blob/master/docs/clojurescript-function-instrumentation.md diff --git a/src/renderer/app/effects.cljs b/src/renderer/app/effects.cljs index c585346b..b20d4aae 100644 --- a/src/renderer/app/effects.cljs +++ b/src/renderer/app/effects.cljs @@ -12,16 +12,6 @@ (rf.storage/reg-co-fx! config/app-key {:cofx :store}) -(def persist - (rf/->interceptor - :id ::persist - :after (fn [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 (fn [coeffects _] @@ -96,15 +86,3 @@ (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 (or (rf/get-effect context :db) - (rf/get-coeffect context :db)) - fx (rf/get-effect context :fx) - event (rf/get-coeffect context :event)] - (cond-> context - db - (rf/assoc-effect :fx (conj (or fx []) [::validate-db [db event]]))))))) diff --git a/src/renderer/app/events.cljs b/src/renderer/app/events.cljs index 63bbc01b..9ea4dbed 100644 --- a/src/renderer/app/events.cljs +++ b/src/renderer/app/events.cljs @@ -4,13 +4,23 @@ [malli.error :as me] [re-frame.core :as rf] [renderer.app.db :as db] - [renderer.app.effects :as fx :refer [persist]] + [renderer.app.effects :as-alias fx] [renderer.notification.events :as-alias notification.e] [renderer.notification.handlers :as notification.h] [renderer.notification.views :as notification.v] [renderer.utils.i18n :as i18n] [renderer.window.effects :as-alias window.fx])) +(def persist + (rf/->interceptor + :id ::persist + :after (fn [context] + (let [db (rf/get-effect context :db) + fx (rf/get-effect context :fx)] + (cond-> context + db + (rf/assoc-effect :fx (conj (or fx []) [::fx/persist db]))))))) + (rf/reg-event-db ::initialize-db (fn [_ _] @@ -100,3 +110,15 @@ ::file-open (fn [_ [_ options]] {::fx/file-open options})) + +(def schema-validator + (rf/->interceptor + :id ::schema-validator + :after (fn [context] + (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)] + (cond-> context + db + (rf/assoc-effect :fx (conj (or fx []) [::fx/validate-db [db event]]))))))) diff --git a/src/renderer/core.cljs b/src/renderer/core.cljs index e4231c3c..e97824c6 100644 --- a/src/renderer/core.cljs +++ b/src/renderer/core.cljs @@ -5,6 +5,7 @@ [re-frame.core :as rf] [re-pressed.core :as rp] [reagent.dom :as ra.dom] + [renderer.app.effects] [renderer.app.events :as app.e] [renderer.app.subs] [renderer.app.views :as app.v] @@ -23,7 +24,6 @@ [renderer.element.subs] [renderer.frame.events] [renderer.frame.subs] - [renderer.history.effects] [renderer.history.events] [renderer.history.subs] [renderer.menubar.events] diff --git a/src/renderer/document/events.cljs b/src/renderer/document/events.cljs index 26951123..4cddece8 100644 --- a/src/renderer/document/events.cljs +++ b/src/renderer/document/events.cljs @@ -5,8 +5,8 @@ [malli.core :as m] [re-frame.core :as rf] [renderer.app.db :refer [App]] - [renderer.app.effects :as-alias app.fx :refer [persist]] - [renderer.app.events :as-alias app.e] + [renderer.app.effects :as-alias app.fx] + [renderer.app.events :as-alias app.e :refer [persist]] [renderer.dialog.events :as-alias dialog.e] [renderer.dialog.handlers :as dialog.h] [renderer.dialog.views :as dialog.v] diff --git a/src/renderer/frame/events.cljs b/src/renderer/frame/events.cljs index 9c3f7003..8eaeeacc 100644 --- a/src/renderer/frame/events.cljs +++ b/src/renderer/frame/events.cljs @@ -2,8 +2,7 @@ (:require [clojure.core.matrix :as mat] [re-frame.core :as rf] - [renderer.app.effects :as-alias app.fx :refer [persist]] - [renderer.app.events] + [renderer.app.events :refer [persist]] [renderer.document.events :as-alias document.e] [renderer.element.handlers :as element.h] [renderer.frame.handlers :as h] diff --git a/src/renderer/history/effects.cljs b/src/renderer/history/effects.cljs deleted file mode 100644 index d67488b8..00000000 --- a/src/renderer/history/effects.cljs +++ /dev/null @@ -1,22 +0,0 @@ -(ns renderer.history.effects - (:require - [re-frame.core :as rf] - [renderer.app.effects :as app.fx] - [renderer.app.events :as-alias e] - [renderer.history.handlers :as history.h])) - -(def auto-persist - "Persists the current state on history position changes." - (rf/->interceptor - :id ::auto-persist - :after (fn [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/events.cljs b/src/renderer/history/events.cljs index de253e31..f17c7357 100644 --- a/src/renderer/history/events.cljs +++ b/src/renderer/history/events.cljs @@ -1,6 +1,7 @@ (ns renderer.history.events (:require [re-frame.core :as rf] + [renderer.app.effects :as app.fx] [renderer.element.events :as-alias element.e] [renderer.history.handlers :as h])) @@ -53,3 +54,16 @@ translate (h/set-translate translate)))) + +(rf/reg-global-interceptor + (rf/->interceptor + :id ::auto-persist + :after (fn [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) + (h/position db)))] + (cond-> context + (and db (not= (h/position db) prev-position)) + (rf/assoc-effect :fx (conj (or fx []) [::app.fx/persist db]))))))) diff --git a/src/renderer/ruler/events.cljs b/src/renderer/ruler/events.cljs index 87757710..549586bf 100644 --- a/src/renderer/ruler/events.cljs +++ b/src/renderer/ruler/events.cljs @@ -1,7 +1,7 @@ (ns renderer.ruler.events (:require [re-frame.core :as rf] - [renderer.app.effects :as fx :refer [persist]])) + [renderer.app.events :refer [persist]])) (rf/reg-event-db ::toggle-visible diff --git a/src/renderer/snap/events.cljs b/src/renderer/snap/events.cljs index cdcae80d..738e53ba 100644 --- a/src/renderer/snap/events.cljs +++ b/src/renderer/snap/events.cljs @@ -2,7 +2,7 @@ (:require [clojure.set :as set] [re-frame.core :as rf] - [renderer.app.effects :refer [persist]] + [renderer.app.events :refer [persist]] [renderer.element.handlers :as element.h] [renderer.snap.handlers :as h])) @@ -20,24 +20,21 @@ (-> (h/toggle-option db option) (h/rebuild-tree)))) -(def auto-rebuild-tree - "Updates the kdtree when the selection changes, by adding/removing points." - (rf/->interceptor - :id ::auto-rebuild-tree - :after (fn [context] - (let [db (rf/get-effect context :db)] - (if (:active-document 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 prev-non-selected-ids) - (rf/assoc-effect - :db - (-> db - (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) +(rf/reg-global-interceptor + (rf/->interceptor + :id ::auto-rebuild-tree + :after (fn [context] + (let [db (rf/get-effect context :db)] + (if (:active-document 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 prev-non-selected-ids) + (rf/assoc-effect + :db + (-> db + (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))))) diff --git a/src/renderer/theme/events.cljs b/src/renderer/theme/events.cljs index c482a427..a27089c0 100644 --- a/src/renderer/theme/events.cljs +++ b/src/renderer/theme/events.cljs @@ -1,7 +1,8 @@ (ns renderer.theme.events (:require [re-frame.core :as rf] - [renderer.app.effects :as-alias app.fx :refer [persist]] + [renderer.app.effects :as-alias app.fx] + [renderer.app.events :refer [persist]] [renderer.theme.effects :as-alias fx])) (rf/reg-event-fx diff --git a/src/renderer/tool/effects.cljs b/src/renderer/tool/effects.cljs index e5e1efcd..b34494e1 100644 --- a/src/renderer/tool/effects.cljs +++ b/src/renderer/tool/effects.cljs @@ -14,19 +14,6 @@ (fn [pointer-id] (.releasePointerCapture (dom/canvas-element!) pointer-id))) -(def custom-fx - (rf/->interceptor - :id ::custom-fx - :after (fn [context] - (let [db (rf/get-effect context :db) - fx (rf/get-effect context :fx)] - (cond-> context - db - (-> (rf/assoc-effect :fx (apply conj (or fx []) (:fx db))) - (rf/assoc-effect :db (assoc db :fx [])))))))) - -(rf/reg-global-interceptor custom-fx) - (rf/reg-fx ::data-transfer (fn [[position data-transfer]] diff --git a/src/renderer/tool/events.cljs b/src/renderer/tool/events.cljs index b8069db0..5b1f3d83 100644 --- a/src/renderer/tool/events.cljs +++ b/src/renderer/tool/events.cljs @@ -42,3 +42,14 @@ (= (:state db) :idle)) {:dispatch [::element.e/deselect-all]} {:db (h/cancel db)}))) + +(rf/reg-global-interceptor + (rf/->interceptor + :id ::custom-fx + :after (fn [context] + (let [db (rf/get-effect context :db) + fx (rf/get-effect context :fx)] + (cond-> context + db + (-> (rf/assoc-effect :fx (apply conj (or fx []) (:fx db))) + (rf/assoc-effect :db (assoc db :fx [])))))))) diff --git a/test/benchmark_test.cljs b/test/benchmark_test.cljs index 10419f64..3602beb6 100644 --- a/test/benchmark_test.cljs +++ b/test/benchmark_test.cljs @@ -5,8 +5,7 @@ [day8.re-frame.test :as rf-test] [malli.instrument :as mi] [re-frame.core :as rf] - [renderer.app.effects :as app.fx] - [renderer.app.events :as-alias app.e] + [renderer.app.events :as app.e] [renderer.document.events :as-alias document.e] [renderer.element.events :as-alias element.e])) @@ -27,7 +26,7 @@ ;; Istrumentation and db validation affects performance, so we disable it. (mi/unstrument!) - (rf/clear-global-interceptor ::app.fx/schema-validator) + (rf/clear-global-interceptor ::app.e/schema-validator) (testing "creating elements" (let [points (str/join " " (repeatedly 100 #(rand-int 1000)))] @@ -48,6 +47,4 @@ (is (> 100 (bench [::element.e/scale [100 100]])))) (mi/instrument!) - (rf/reg-global-interceptor app.fx/schema-validator))) - - + (rf/reg-global-interceptor app.e/schema-validator))) diff --git a/test/core_test.cljs b/test/core_test.cljs index 3f32e124..7b3f5dcb 100644 --- a/test/core_test.cljs +++ b/test/core_test.cljs @@ -3,8 +3,8 @@ [malli.instrument :as mi] [re-frame.core :as rf] [re-frame.subs :as rf.subs] - [renderer.app.effects :as app.fx] - [renderer.app.events] + [renderer.app.effects] + [renderer.app.events :as app.e] [renderer.app.subs] [renderer.dialog.events] [renderer.document.effects] @@ -16,7 +16,6 @@ [renderer.element.subs] [renderer.frame.events] [renderer.frame.subs] - [renderer.history.effects] [renderer.history.events] [renderer.history.subs] [renderer.notification.events] @@ -36,4 +35,4 @@ (set! rf.subs/warn-when-not-reactive (constantly nil)) (mi/instrument!) -(rf/reg-global-interceptor app.fx/schema-validator) +(rf/reg-global-interceptor app.e/schema-validator)