Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc committed Nov 20, 2024
1 parent 3c9b194 commit 742da4a
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 100 deletions.
6 changes: 3 additions & 3 deletions src/dev.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 0 additions & 22 deletions src/renderer/app/effects.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _]
Expand Down Expand Up @@ -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]])))))))
24 changes: 23 additions & 1 deletion src/renderer/app/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [_ _]
Expand Down Expand Up @@ -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]])))))))
2 changes: 1 addition & 1 deletion src/renderer/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/document/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/frame/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 0 additions & 22 deletions src/renderer/history/effects.cljs

This file was deleted.

14 changes: 14 additions & 0 deletions src/renderer/history/events.cljs
Original file line number Diff line number Diff line change
@@ -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]))

Expand Down Expand Up @@ -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])))))))
2 changes: 1 addition & 1 deletion src/renderer/ruler/events.cljs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
41 changes: 19 additions & 22 deletions src/renderer/snap/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand All @@ -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)))))
3 changes: 2 additions & 1 deletion src/renderer/theme/events.cljs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 0 additions & 13 deletions src/renderer/tool/effects.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
11 changes: 11 additions & 0 deletions src/renderer/tool/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 []))))))))
9 changes: 3 additions & 6 deletions test/benchmark_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand All @@ -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)))]
Expand All @@ -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)))
7 changes: 3 additions & 4 deletions test/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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)

0 comments on commit 742da4a

Please sign in to comment.