♻️ Refactor state locality

The main purpose of this refactor is reduce
a custom state from different pages and unify
them under common access patterns
This commit is contained in:
Andrey Antukh
2025-01-16 11:00:14 +01:00
parent f62aecb383
commit 85746e7cb2
110 changed files with 2254 additions and 2125 deletions

View File

@@ -116,7 +116,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
blue1' (cths/get-shape file' :blue1)
copied-blue1' (find-copied-shape blue1' page' uuid/zero)]
@@ -155,7 +155,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b1' (cths/get-shape file' :frame-b1)
blue1' (cths/get-shape file' :blue1)
@@ -193,7 +193,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
yellow' (cths/get-shape file' :frame-yellow)
blue1' (cths/get-shape file' :blue1)
@@ -232,7 +232,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b2' (cths/get-shape file' :frame-b2)
blue1' (cths/get-shape file' :blue1)
@@ -272,7 +272,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
copied-blue1' (find-copied-shape blue1 page' uuid/zero)]
@@ -309,7 +309,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
yellow' (cths/get-shape file' :frame-yellow)
copied-blue1' (find-copied-shape blue1 page' (:id yellow'))]
@@ -346,7 +346,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b2' (cths/get-shape file' :frame-b2)
copied-blue1' (find-copied-shape blue1 page' (:id b2'))]
@@ -380,7 +380,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
copied-yellow' (find-copied-shape yellow page' uuid/zero)
blue1' (cths/get-shape file' :blue1)
@@ -419,7 +419,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b1' (cths/get-shape file' :frame-b1)
copied-yellow' (find-copied-shape yellow page' (:id b1'))
@@ -459,7 +459,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b2' (cths/get-shape file' :frame-b2)
copied-yellow' (find-copied-shape yellow page' (:id b2'))
@@ -500,7 +500,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
copied-yellow' (find-copied-shape yellow page' uuid/zero)
copied-blue1' (find-copied-shape blue1 page' (:id copied-yellow'))]
@@ -537,7 +537,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b1' (cths/get-shape file' :frame-b1)
copied-yellow' (find-copied-shape yellow page' (:id b1'))
@@ -575,7 +575,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b2' (cths/get-shape file' :frame-b2)
copied-yellow' (find-copied-shape yellow page' (:id b2'))
@@ -609,7 +609,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
blue2' (cths/get-shape file' :blue-copy-in-green-copy)
copied-green' (find-copied-shape green page' uuid/zero)
@@ -648,7 +648,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b1' (cths/get-shape file' :frame-b1)
blue2' (cths/get-shape file' :blue-copy-in-green-copy)
@@ -688,7 +688,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b2' (cths/get-shape file' :frame-b2)
blue2' (cths/get-shape file' :blue-copy-in-green-copy)
@@ -731,7 +731,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
copied-green' (find-copied-shape green page' uuid/zero)
copied-blue1' (find-copied-shape blue2 page' (:id copied-green'))]
@@ -768,7 +768,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b1' (cths/get-shape file' :frame-b1)
copied-green' (find-copied-shape green page' (:id b1'))
@@ -806,7 +806,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
b2' (cths/get-shape file' :frame-b2)
copied-green' (find-copied-shape green page' (:id b2'))
@@ -855,7 +855,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
green' (cths/get-shape file' :frame-green)
blue1' (cths/get-shape file' :blue1)

View File

@@ -14,10 +14,10 @@
[app.common.test-helpers.shapes :as cths]
[app.common.test-helpers.tokens :as ctht]
[app.common.types.tokens-lib :as ctob]
[app.main.data.helpers :as dsh]
[app.main.data.tokens :as dt]
[app.main.data.workspace.libraries :as dwl]
[app.main.data.workspace.selection :as dws]
[app.main.data.workspace.state-helpers :as wsh]
[app.main.ui.workspace.tokens.changes :as wtch]
[app.main.ui.workspace.tokens.update :as wtu]
[cljs.test :as t :include-macros true]
@@ -79,7 +79,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
frame1' (cths/get-shape file' :frame1)
tokens-frame1' (:applied-tokens frame1')]
@@ -111,8 +111,8 @@
store done events
(fn [new-state]
(let [;; ==== Get
selected (wsh/lookup-selected new-state)
c-frame1' (wsh/lookup-shape new-state (first selected))
selected (dsh/lookup-selected new-state)
c-frame1' (dsh/lookup-shape new-state (first selected))
tokens-frame1' (:applied-tokens c-frame1')]
;; ==== Check
@@ -145,7 +145,7 @@
store done events2
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
c-frame1' (cths/get-shape file' :c-frame1)
tokens-frame1' (:applied-tokens c-frame1')]
@@ -181,7 +181,7 @@
store done events2
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
c-frame1' (cths/get-shape file' :c-frame1)
tokens-frame1' (:applied-tokens c-frame1')]
@@ -215,7 +215,7 @@
store done events2
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
c-frame1' (cths/get-shape file' :c-frame1)
tokens-frame1' (:applied-tokens c-frame1')]
@@ -256,7 +256,7 @@
store done events2
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
c-frame1' (cths/get-shape file' :c-frame1)
tokens-frame1' (:applied-tokens c-frame1')]
@@ -296,7 +296,7 @@
store done events2
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
c-frame1' (cths/get-shape file' :c-frame1)
tokens-frame1' (:applied-tokens c-frame1')]
@@ -390,7 +390,7 @@
store done events2
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
frame1' (cths/get-shape file' :frame1)
c-frame1' (cths/get-shape file' :c-frame1)
tokens-frame1' (:applied-tokens c-frame1')]
@@ -423,4 +423,4 @@
(t/is (empty? (:touched c-frame1'))))))))]
(tohs/run-store-async
store step2 events identity))))
store step2 events identity))))

View File

@@ -126,31 +126,32 @@
(not (:component-root %))))
(map :id))]
(concat
(apply concat (mapv #(copy-paste-shape % file :target-page-label target-page-label :target-container-id uuid/zero) frame-1-instance-ids))
(apply concat (mapv #(copy-paste-shape % file :target-page-label target-page-label :target-container-id uuid/zero) frame-1-instance-ids)))))
(apply concat
(mapv #(copy-paste-shape % file :target-page-label target-page-label :target-container-id uuid/zero) frame-1-instance-ids))
(apply concat
(mapv #(copy-paste-shape % file :target-page-label target-page-label :target-container-id uuid/zero) frame-1-instance-ids)))))
(t/deftest main-and-first-level-copy-1
(t/async
done
(t/async done
(with-redefs [uuid/next cthi/next-uuid]
(let [;; ==== Setup
file (setup-file)
store (ths/setup-store file)
;; ==== Action
;; ==== Action
;; For each main and first level copy:
;; - Duplicate it two times with copy-paste.
;; For each main and first level copy:
;; - Duplicate it two times with copy-paste.
events
(concat
(duplicate-each-main-and-first-level-copy file)
;; - Change color of Simple1
;; - Change color of Simple1
(set-color-bottom-shape :frame-simple-1 file {:color "#111111"}))]
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)]
(let [file' (ths/get-file-from-state new-state)]
(t/is (= (count-shapes file' "rect-simple-1" "#111111") 18)))))))))
(t/deftest main-and-first-level-copy-2
@@ -176,7 +177,7 @@
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)]
(let [file' (ths/get-file-from-state new-state)]
(t/is (= (count-shapes file' "rect-simple-1" "#222222") 15)))))))))
(t/deftest main-and-first-level-copy-3
@@ -203,7 +204,7 @@
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)]
(let [file' (ths/get-file-from-state new-state)]
(t/is (= (count-shapes file' "rect-simple-1" "#333333") 12)))))))))
@@ -232,7 +233,7 @@
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)]
(let [file' (ths/get-file-from-state new-state)]
(t/is (= (count-shapes file' "rect-simple-1" "#444444") 6)))))))))
(t/deftest copy-nested-in-main-1
@@ -255,7 +256,7 @@
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)]
(let [file' (ths/get-file-from-state new-state)]
;; Check propagation to all copies.
(t/is (= (count-shapes file' "rect-simple-1" "#111111") 28)))))))))
@@ -279,7 +280,7 @@
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)]
(let [file' (ths/get-file-from-state new-state)]
;; Check propagation to duplicated.
(t/is (= (count-shapes file' "rect-simple-1" "#222222") 9)))))))))
@@ -303,7 +304,7 @@
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)]
(let [file' (ths/get-file-from-state new-state)]
;; Check that it's NOT PROPAGATED.
(t/is (= (count-shapes file' "rect-simple-1" "#333333") 2)))))))))
@@ -328,7 +329,7 @@
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)]
(let [file' (ths/get-file-from-state new-state)]
;; Check propagation to all copies.
(t/is (= (count-shapes file' "rect-simple-1" "#111111") 20)))))))))
@@ -357,7 +358,7 @@
(ths/run-store
store done events
(fn [new-state]
(let [file' (ths/get-file-from-store new-state)]
(let [file' (ths/get-file-from-state new-state)]
;; Check that it's NOT PROPAGATED.
(t/is (= (count-shapes file' "rect-simple-1" "#111111") 11))
(t/is (= (count-shapes file' "rect-simple-1" "#222222") 7))
@@ -365,8 +366,7 @@
(t/deftest copy-nested-3
(t/async
done
(t/async done
(with-redefs [uuid/next cthi/next-uuid]
(let [;; ==== Setup
file (setup-file)
@@ -388,9 +388,9 @@
(ths/run-store
store done events
(fn [new-state]
(let [file' (-> (ths/get-file-from-store new-state)
(let [file' (-> (ths/get-file-from-state new-state)
(cthf/switch-to-page :page-2))]
;; Check that it's NOT PROPAGATED.
(t/is (= (count-shapes file' "rect-simple-1" "#111111") 10))
(t/is (= (count-shapes file' "rect-simple-1" "#222222") 4))
(t/is (= (count-shapes file' "rect-simple-1" "#333333") 0)))))))))
(t/is (= (count-shapes file' "rect-simple-1" "#333333") 0)))))))))

View File

@@ -42,7 +42,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
guide' (-> page'

View File

@@ -39,7 +39,7 @@
store done events
(fn [new-state]
(let [;; ==== Get
file' (ths/get-file-from-store new-state)
file' (ths/get-file-from-state new-state)
page' (cthf/current-page file')
group-id (->> (:objects page')
vals