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 3bca685 commit 3c9b194
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 52 deletions.
42 changes: 19 additions & 23 deletions src/renderer/app/effects.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]])))))))
2 changes: 0 additions & 2 deletions src/renderer/document/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@

(rf/reg-event-fx
::close-active
[persist]
(fn [{:keys [db]} [_]]
{:dispatch [::close (:active-document db) true]}))

Expand Down Expand Up @@ -308,4 +307,3 @@
(-> db
(h/set-active id)
(h/center))))

18 changes: 8 additions & 10 deletions src/renderer/history/effects.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 4 additions & 4 deletions src/renderer/history/handlers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 []}]
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 7 additions & 10 deletions src/renderer/snap/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 4 additions & 3 deletions src/renderer/tool/effects.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3c9b194

Please sign in to comment.