♻️ Refactor detach-preview-sprite! to use atomic swap

Use idiomatic atomic swap! update instead of non-atomic
read-then-write pattern. The new implementation computes
the decremented refs inside swap! and only removes the
node when the result reaches zero.

AI-assisted-by: mimo-v2.5
This commit is contained in:
Andrey Antukh 2026-08-25 12:01:19 +00:00
parent 3ce1f083e1
commit 3acfc65894

View File

@ -238,12 +238,9 @@
the last open dropdown closes. The cached node and `:ids` stay, so reopening
re-attaches without a refetch or re-parse."
[node]
(let [refs (max 0 (dec (:refs @preview-sprite)))]
(if (pos? refs)
(swap! preview-sprite assoc :refs refs)
(do
(dom/remove! node)
(swap! preview-sprite assoc :refs 0)))))
(let [new-state (swap! preview-sprite update :refs #(max 0 (dec %)))]
(when (zero? (:refs new-state))
(dom/remove! node))))
(defn- add-font-css!
"Creates a style element and attaches it to the dom."