From b7eb20dc446e27c78f92839ca664236bdadcf6f4 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 25 Aug 2023 16:32:56 +0200 Subject: [PATCH] :zap: Reduce unnecesary lookups on get-frame-by-position fn --- common/src/app/common/types/container.cljc | 10 +++--- common/src/app/common/types/shape_tree.cljc | 34 +++++++++++-------- frontend/src/app/main/data/comments.cljs | 2 +- .../src/app/main/data/workspace/comments.cljs | 2 +- .../app/main/data/workspace/interactions.cljs | 2 +- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/common/src/app/common/types/container.cljc b/common/src/app/common/types/container.cljc index 4d71bbac3..057e86d8d 100644 --- a/common/src/app/common/types/container.cljc +++ b/common/src/app/common/types/container.cljc @@ -239,10 +239,10 @@ unames (volatile! (cfh/get-used-names objects)) frame-id (or force-frame-id - (ctst/frame-id-by-position objects - (gpt/add orig-pos delta) - {:skip-components? true - :bottom-frames? true})) + (ctst/get-frame-id-by-position objects + (gpt/add orig-pos delta) + {:skip-components? true + :bottom-frames? true})) frame-ids-map (volatile! {}) update-new-shape @@ -315,4 +315,4 @@ parent (get objects parent-id)] (if (= parent-id uuid/zero) current-top - (get-top-instance objects parent current-top)))) \ No newline at end of file + (get-top-instance objects parent current-top)))) diff --git a/common/src/app/common/types/shape_tree.cljc b/common/src/app/common/types/shape_tree.cljc index 61380f3b4..c5bc32dc1 100644 --- a/common/src/app/common/types/shape_tree.cljc +++ b/common/src/app/common/types/shape_tree.cljc @@ -256,23 +256,27 @@ -1)))) items))) -(defn frame-id-by-position - ([objects position] (frame-id-by-position objects position nil)) - ([objects position options] - (assert (gpt/point? position)) - (let [sort-with-options (fn [objects ids] - (sort-z-index objects ids options)) - top-frame - (->> (get-frames-ids objects options) - (sort-with-options objects) - (d/seek #(and position (gsh/has-point? (get objects %) position))))] - (or top-frame uuid/zero)))) +(defn get-frame-by-position + ([objects position] + (get-frame-by-position objects position nil)) -(defn frame-by-position - ([objects position] (frame-by-position objects position nil)) ([objects position options] - (let [frame-id (frame-id-by-position objects position options)] - (get objects frame-id)))) + (dm/assert! + "expected a point" + (gpt/point? position)) + + (let [frames (get-frames objects options) + frames (sort-z-index-objects objects frames options)] + (or (d/seek #(and ^boolean (some? position) + ^boolean (gsh/has-point? % position)) + frames) + (get objects uuid/zero))))) + +(defn get-frame-id-by-position + ([objects position] (get-frame-id-by-position objects position nil)) + ([objects position options] + (when-let [frame (get-frame-by-position objects position options)] + (dm/get-prop frame :id)))) (defn get-frames-by-position ([objects position] (get-frames-by-position objects position nil)) diff --git a/frontend/src/app/main/data/comments.cljs b/frontend/src/app/main/data/comments.cljs index 4ea5796be..b73f4e9c8 100644 --- a/frontend/src/app/main/data/comments.cljs +++ b/frontend/src/app/main/data/comments.cljs @@ -83,7 +83,7 @@ (watch [_ state _] (let [page-id (:current-page-id state) objects (wsh/lookup-page-objects state page-id) - frame-id (ctst/frame-id-by-position objects (:position params)) + frame-id (ctst/get-frame-id-by-position objects (:position params)) params (assoc params :frame-id frame-id)] (->> (rp/cmd! :create-comment-thread params) (rx/mapcat #(rp/cmd! :get-comment-thread {:file-id (:file-id %) :id (:id %)})) diff --git a/frontend/src/app/main/data/workspace/comments.cljs b/frontend/src/app/main/data/workspace/comments.cljs index c7d9eb118..ea1b3e12f 100644 --- a/frontend/src/app/main/data/workspace/comments.cljs +++ b/frontend/src/app/main/data/workspace/comments.cljs @@ -127,7 +127,7 @@ page-id (:id page) objects (wsh/lookup-page-objects state page-id) new-frame-id (if (nil? frame-id) - (ctst/frame-id-by-position objects (gpt/point new-x new-y)) + (ctst/get-frame-id-by-position objects (gpt/point new-x new-y)) (:frame-id thread)) thread (assoc thread :position (gpt/point new-x new-y) diff --git a/frontend/src/app/main/data/workspace/interactions.cljs b/frontend/src/app/main/data/workspace/interactions.cljs index 6d72f5e8d..dabf0b18e 100644 --- a/frontend/src/app/main/data/workspace/interactions.cljs +++ b/frontend/src/app/main/data/workspace/interactions.cljs @@ -206,7 +206,7 @@ from-frame-id (if (cph/frame-shape? from-shape) from-id (:frame-id from-shape)) - target-frame (ctst/frame-by-position objects position)] + target-frame (ctst/get-frame-by-position objects position)] (when (and (not= (:id target-frame) uuid/zero) (not= (:id target-frame) from-frame-id))