♻️ Simplifies RPC pattern for token themes (#6052)

* ♻️ Add set token theme method schema

* ♻️ Add `:set-token-theme` dispatcher for `process-change` multimenthod

* ♻️ Add `set-token-theme` to the changes builder

* ♻️ Use new method on the frontend

* ♻️ Remove unused token theme methods

* ♻️ Add tests

* ♻️ Add library data to changes

* ♻️ Add new test case

* ♻️ Remove unused binding
This commit is contained in:
Andrei Fëdorov 2025-03-12 09:29:03 +01:00 committed by GitHub
parent 474cd1e55a
commit b9df8ad038
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 179 additions and 93 deletions

View file

@ -376,24 +376,6 @@
[:type [:= :update-active-token-themes]] [:type [:= :update-active-token-themes]]
[:theme-ids [:set :string]]]] [:theme-ids [:set :string]]]]
[:add-token-theme
[:map {:title "AddTokenThemeChange"}
[:type [:= :add-token-theme]]
[:token-theme ::ctot/token-theme]]]
[:mod-token-theme
[:map {:title "ModTokenThemeChange"}
[:type [:= :mod-token-theme]]
[:group :string]
[:name :string]
[:token-theme ::ctot/token-theme]]]
[:del-token-theme
[:map {:title "DelTokenThemeChange"}
[:type [:= :del-token-theme]]
[:group :string]
[:name :string]]]
[:add-token-sets [:add-token-sets
[:map {:title "AddTokenSetsChange"} [:map {:title "AddTokenSetsChange"}
[:type [:= :add-token-sets]] [:type [:= :add-token-sets]]
@ -422,6 +404,13 @@
[:before-path [:maybe [:vector :string]]] [:before-path [:maybe [:vector :string]]]
[:before-group? [:maybe :boolean]]]] [:before-group? [:maybe :boolean]]]]
[:set-token-theme
[:map {:title "SetTokenThemeChange"}
[:type [:= :set-token-theme]]
[:theme-name :string]
[:group :string]
[:theme [:maybe ::ctot/token-theme]]]]
[:set-tokens-lib [:set-tokens-lib
[:map {:title "SetTokensLib"} [:map {:title "SetTokensLib"}
[:type [:= :set-tokens-lib]] [:type [:= :set-tokens-lib]]
@ -1038,32 +1027,29 @@
(ctob/update-set lib' set-name (fn [prev-token-set] (ctob/update-set lib' set-name (fn [prev-token-set]
(ctob/make-token-set (merge prev-token-set token-set))))))))) (ctob/make-token-set (merge prev-token-set token-set)))))))))
(defmethod process-change :set-token-theme
[data {:keys [group theme-name theme]}]
(update data :tokens-lib
(fn [lib]
(let [lib' (ctob/ensure-tokens-lib lib)]
(cond
(not theme)
(ctob/delete-theme lib' group theme-name)
(not (ctob/get-theme lib' group theme-name))
(ctob/add-theme lib' (ctob/make-token-theme theme))
:else
(ctob/update-theme lib'
group theme-name
(fn [prev-token-theme]
(ctob/make-token-theme (merge prev-token-theme theme)))))))))
(defmethod process-change :update-active-token-themes (defmethod process-change :update-active-token-themes
[data {:keys [theme-ids]}] [data {:keys [theme-ids]}]
(update data :tokens-lib #(-> % (ctob/ensure-tokens-lib) (update data :tokens-lib #(-> % (ctob/ensure-tokens-lib)
(ctob/set-active-themes theme-ids)))) (ctob/set-active-themes theme-ids))))
(defmethod process-change :add-token-theme
[data {:keys [token-theme]}]
(update data :tokens-lib #(-> %
(ctob/ensure-tokens-lib)
(ctob/add-theme (-> token-theme
(ctob/make-token-theme))))))
(defmethod process-change :mod-token-theme
[data {:keys [name group token-theme]}]
(update data :tokens-lib #(-> %
(ctob/ensure-tokens-lib)
(ctob/update-theme group name
(fn [prev-theme]
(merge prev-theme token-theme))))))
(defmethod process-change :del-token-theme
[data {:keys [group name]}]
(update data :tokens-lib #(-> %
(ctob/ensure-tokens-lib)
(ctob/delete-theme group name))))
(defmethod process-change :add-token-sets (defmethod process-change :add-token-sets
[data {:keys [token-sets]}] [data {:keys [token-sets]}]
(update data :tokens-lib #(-> % (update data :tokens-lib #(-> %

View file

@ -774,33 +774,30 @@
(update :undo-changes conj {:type :update-active-token-themes :theme-ids prev-token-active-theme-ids}) (update :undo-changes conj {:type :update-active-token-themes :theme-ids prev-token-active-theme-ids})
(apply-changes-local))) (apply-changes-local)))
(defn add-token-theme (defn set-token-theme [changes group theme-name theme]
[changes token-theme]
(-> changes
(update :redo-changes conj {:type :add-token-theme :token-theme token-theme})
(update :undo-changes conj {:type :del-token-theme :group (:group token-theme) :name (:name token-theme)})
(apply-changes-local)))
(defn update-token-theme
[changes token-theme prev-token-theme]
(let [name (or (:name prev-token-theme)
(:name token-theme))
group (or (:group prev-token-theme)
(:group token-theme))]
(-> changes
(update :redo-changes conj {:type :mod-token-theme :group group :name name :token-theme token-theme})
(update :undo-changes conj {:type :mod-token-theme :group group :name name :token-theme (or prev-token-theme token-theme)})
(apply-changes-local))))
(defn delete-token-theme
[changes group name]
(assert-library! changes) (assert-library! changes)
(let [library-data (::library-data (meta changes)) (let [library-data (::library-data (meta changes))
prev-token-theme (some-> (get library-data :tokens-lib) prev-theme (some-> (get library-data :tokens-lib)
(ctob/get-theme group name))] (ctob/get-theme group theme-name))]
(-> changes (-> changes
(update :redo-changes conj {:type :del-token-theme :group group :name name}) (update :redo-changes conj {:type :set-token-theme
(update :undo-changes conj {:type :add-token-theme :token-theme prev-token-theme}) :theme-name theme-name
:group group
:theme theme})
(update :undo-changes conj (if prev-theme
{:type :set-token-theme
:group group
:theme-name (or
;; Undo of edit
(:name theme)
;; Undo of delete
theme-name)
:theme prev-theme}
;; Undo of create
{:type :set-token-theme
:group group
:theme-name theme-name
:theme nil}))
(apply-changes-local)))) (apply-changes-local))))
(defn rename-token-set-group (defn rename-token-set-group

View file

@ -13,13 +13,12 @@
prev-hidden-token-theme (ctob/get-hidden-theme tokens-lib) prev-hidden-token-theme (ctob/get-hidden-theme tokens-lib)
hidden-token-theme (-> (some-> prev-hidden-token-theme (ctob/set-sets active-token-set-names)) hidden-token-theme (-> (some-> prev-hidden-token-theme (ctob/set-sets active-token-set-names))
(update-theme-fn)) (update-theme-fn))]
(-> changes
changes (-> changes (pcb/update-active-token-themes #{ctob/hidden-token-theme-path} prev-active-token-themes)
(pcb/update-active-token-themes #{ctob/hidden-token-theme-path} prev-active-token-themes)) (pcb/set-token-theme (:group prev-hidden-token-theme)
(:name prev-hidden-token-theme)
changes (pcb/update-token-theme changes hidden-token-theme prev-hidden-token-theme)] hidden-token-theme))))
changes))
(defn generate-toggle-token-set (defn generate-toggle-token-set
"Toggle a token set at `set-name` in `tokens-lib` without modifying a user theme." "Toggle a token set at `set-name` in `tokens-lib` without modifying a user theme."

View file

@ -22,7 +22,9 @@
(ctob/add-theme (ctob/make-token-theme :name "theme" (ctob/add-theme (ctob/make-token-theme :name "theme"
:sets #{"foo/bar"})) :sets #{"foo/bar"}))
(ctob/set-active-themes #{"/theme"}))) (ctob/set-active-themes #{"/theme"})))
changes (clt/generate-toggle-token-set (pcb/empty-changes) (tht/get-tokens-lib file) "foo/bar") changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(clt/generate-toggle-token-set (tht/get-tokens-lib file) "foo/bar"))
redo (thf/apply-changes file changes) redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo) redo-lib (tht/get-tokens-lib redo)
@ -40,7 +42,9 @@
(ctob/add-theme (ctob/make-token-theme :name "theme" (ctob/add-theme (ctob/make-token-theme :name "theme"
:sets #{"foo/bar"})) :sets #{"foo/bar"}))
(ctob/set-active-themes #{"/theme"}))) (ctob/set-active-themes #{"/theme"})))
changes (clt/generate-toggle-token-set (pcb/empty-changes) (tht/get-tokens-lib file) "foo/bar") changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(clt/generate-toggle-token-set (tht/get-tokens-lib file) "foo/bar"))
redo (thf/apply-changes file changes) redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo) redo-lib (tht/get-tokens-lib redo)
@ -58,7 +62,9 @@
(ctob/add-theme (ctob/make-hidden-token-theme)) (ctob/add-theme (ctob/make-hidden-token-theme))
(ctob/set-active-themes #{ctob/hidden-token-theme-path}))) (ctob/set-active-themes #{ctob/hidden-token-theme-path})))
changes (clt/generate-toggle-token-set-group (pcb/empty-changes) (tht/get-tokens-lib file) ["foo"]) changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(clt/generate-toggle-token-set-group (tht/get-tokens-lib file) ["foo"]))
redo (thf/apply-changes file changes) redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo) redo-lib (tht/get-tokens-lib redo)
@ -68,6 +74,87 @@
(t/is (= #{"foo/bar"} (:sets (ctob/get-hidden-theme redo-lib))))))) (t/is (= #{"foo/bar"} (:sets (ctob/get-hidden-theme redo-lib)))))))
(t/deftest set-token-theme-test
(t/testing "delete token theme"
(let [theme-name "foo"
group "main"
file (setup-file #(-> %
(ctob/add-theme (ctob/make-token-theme :name theme-name
:group group))))
changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(pcb/set-token-theme group theme-name nil))
redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo)
undo (thf/apply-undo-changes redo changes)
undo-lib (tht/get-tokens-lib undo)]
;; Redo
(t/is (nil? (ctob/get-theme redo-lib group theme-name)))
;; Undo
(t/is (some? (ctob/get-theme undo-lib group theme-name)))))
(t/testing "add token theme"
(let [theme-name "foo"
group "main"
theme (ctob/make-token-theme :name theme-name
:group group)
file (setup-file identity)
changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(pcb/set-token-theme group theme-name theme))
redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo)
undo (thf/apply-undo-changes redo changes)
undo-lib (tht/get-tokens-lib undo)]
;; Redo
(t/is (some? (ctob/get-theme redo-lib group theme-name)))
;; Undo
(t/is (nil? (ctob/get-theme undo-lib group theme-name)))))
(t/testing "update token theme"
(let [theme-name "foo"
group "main"
prev-theme (ctob/make-token-theme :name theme-name
:group group)
file (setup-file #(ctob/add-theme % prev-theme))
new-theme-name "foo1"
changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(pcb/set-token-theme group new-theme-name prev-theme))
redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo)
undo (thf/apply-undo-changes redo changes)
undo-lib (tht/get-tokens-lib undo)]
;; Redo
(t/is (some? (ctob/get-theme redo-lib group theme-name)))
(t/is (nil? (ctob/get-theme redo-lib group new-theme-name)))
;; Undo
(t/is (some? (ctob/get-theme undo-lib group theme-name)))
(t/is (nil? (ctob/get-theme undo-lib group new-theme-name)))))
(t/testing "toggling token theme updates using changes history"
(let [theme-name "foo-theme"
group "main"
set-name "bar-set"
token-set (ctob/make-token-set :name set-name)
theme (ctob/make-token-theme :name theme-name
:group group)
file (setup-file #(-> %
(ctob/add-theme theme)
(ctob/add-set token-set)))
theme' (assoc theme :sets #{set-name})
changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(pcb/set-token-theme group theme-name theme'))
changed-file (-> file
(thf/apply-changes changes)
(thf/apply-undo-changes changes)
(thf/apply-changes changes))
changed-lib (tht/get-tokens-lib changed-file)]
(t/is (= #{set-name}
(-> changed-lib (ctob/get-theme group theme-name) :sets))))))
(t/deftest set-token-test (t/deftest set-token-test
(t/testing "delete token" (t/testing "delete token"
(let [set-name "foo" (let [set-name "foo"
@ -198,7 +285,9 @@
(ctob/add-set (ctob/make-token-set :name "foo/bar/baz/baz-child")) (ctob/add-set (ctob/make-token-set :name "foo/bar/baz/baz-child"))
(ctob/add-theme (ctob/make-token-theme :name "theme")) (ctob/add-theme (ctob/make-token-theme :name "theme"))
(ctob/set-active-themes #{"/theme"}))) (ctob/set-active-themes #{"/theme"})))
changes (clt/generate-toggle-token-set-group (pcb/empty-changes) (tht/get-tokens-lib file) ["foo" "bar"]) changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(clt/generate-toggle-token-set-group (tht/get-tokens-lib file) ["foo" "bar"]))
redo (thf/apply-changes file changes) redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo) redo-lib (tht/get-tokens-lib redo)
@ -219,7 +308,9 @@
:sets #{"foo/bar/baz"})) :sets #{"foo/bar/baz"}))
(ctob/set-active-themes #{"/theme"}))) (ctob/set-active-themes #{"/theme"})))
changes (clt/generate-toggle-token-set-group (pcb/empty-changes) (tht/get-tokens-lib file) ["foo" "bar"]) changes (-> (pcb/empty-changes)
(pcb/with-library-data (:data file))
(clt/generate-toggle-token-set-group (tht/get-tokens-lib file) ["foo" "bar"]))
redo (thf/apply-changes file changes) redo (thf/apply-changes file changes)
redo-lib (tht/get-tokens-lib redo) redo-lib (tht/get-tokens-lib redo)

View file

@ -70,9 +70,13 @@
(let [new-token-theme token-theme] (let [new-token-theme token-theme]
(ptk/reify ::create-token-theme (ptk/reify ::create-token-theme
ptk/WatchEvent ptk/WatchEvent
(watch [it _ _] (watch [it state _]
(let [changes (-> (pcb/empty-changes it) (let [data (dsh/lookup-file-data state)
(pcb/add-token-theme new-token-theme))] changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/set-token-theme (:group new-token-theme)
(:name new-token-theme)
new-token-theme))]
(rx/of (rx/of
(dch/commit-changes changes))))))) (dch/commit-changes changes)))))))
@ -80,9 +84,14 @@
(ptk/reify ::update-token-theme (ptk/reify ::update-token-theme
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(let [tokens-lib (get-tokens-lib state) (let [tokens-lib (get-tokens-lib state)
data (dsh/lookup-file-data state)
prev-token-theme (some-> tokens-lib (ctob/get-theme group name)) prev-token-theme (some-> tokens-lib (ctob/get-theme group name))
changes (pcb/update-token-theme (pcb/empty-changes it) token-theme prev-token-theme)] changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/set-token-theme (:group prev-token-theme)
(:name prev-token-theme)
token-theme))]
(rx/of (rx/of
(dch/commit-changes changes)))))) (dch/commit-changes changes))))))
@ -105,15 +114,14 @@
(dch/commit-changes changes) (dch/commit-changes changes)
(wtu/update-workspace-tokens)))))) (wtu/update-workspace-tokens))))))
(defn delete-token-theme [group name] (defn delete-token-theme [group theme-name]
(ptk/reify ::delete-token-theme (ptk/reify ::delete-token-theme
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(let [data (dsh/lookup-file-data state) (let [data (dsh/lookup-file-data state)
changes (-> (pcb/empty-changes it) changes (-> (pcb/empty-changes it)
(pcb/with-library-data data) (pcb/with-library-data data)
(pcb/delete-token-theme group name))] (pcb/set-token-theme group theme-name nil))]
(rx/of (rx/of
(dch/commit-changes changes) (dch/commit-changes changes)
(wtu/update-workspace-tokens)))))) (wtu/update-workspace-tokens))))))
@ -167,8 +175,10 @@
(ptk/reify ::toggle-token-set (ptk/reify ::toggle-token-set
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ state _]
(let [tlib (get-tokens-lib state) (let [data (dsh/lookup-file-data state)
tlib (get-tokens-lib state)
changes (-> (pcb/empty-changes) changes (-> (pcb/empty-changes)
(pcb/with-library-data data)
(clt/generate-toggle-token-set tlib name))] (clt/generate-toggle-token-set tlib name))]
(rx/of (dch/commit-changes changes) (rx/of (dch/commit-changes changes)
@ -178,7 +188,10 @@
(ptk/reify ::toggle-token-set-group (ptk/reify ::toggle-token-set-group
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ state _]
(let [changes (clt/generate-toggle-token-set-group (pcb/empty-changes) (get-tokens-lib state) group-path)] (let [data (dsh/lookup-file-data state)
changes (-> (pcb/empty-changes)
(pcb/with-library-data data)
(clt/generate-toggle-token-set-group (get-tokens-lib state) group-path))]
(rx/of (rx/of
(dch/commit-changes changes) (dch/commit-changes changes)
(wtu/update-workspace-tokens)))))) (wtu/update-workspace-tokens))))))
@ -252,9 +265,11 @@
(ptk/reify ::create-token-and-set (ptk/reify ::create-token-and-set
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ state _]
(let [set-name "Global" (let [data
(dsh/lookup-file-data state)
data (dsh/lookup-file-data state) set-name
"Global"
token-set token-set
(-> (ctob/make-token-set :name set-name) (-> (ctob/make-token-set :name set-name)
@ -269,13 +284,11 @@
changes changes
(-> (pcb/empty-changes) (-> (pcb/empty-changes)
(pcb/with-library-data data) (pcb/with-library-data data)
(pcb/set-token-set set-name false token-set)) (pcb/set-token-set set-name false token-set)
(pcb/set-token-theme (:group hidden-theme)
changes (:name hidden-theme)
(-> changes hidden-theme-with-set)
(pcb/update-token-theme hidden-theme-with-set hidden-theme)
(pcb/update-active-token-themes #{ctob/hidden-token-theme-path} #{}))] (pcb/update-active-token-themes #{ctob/hidden-token-theme-path} #{}))]
(rx/of (dch/commit-changes changes) (rx/of (dch/commit-changes changes)
(set-selected-token-set-name set-name)))))) (set-selected-token-set-name set-name))))))