From 5d3eadc6dbee72a1e33df994548a2716d1d92b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Mon, 12 Sep 2022 11:24:11 +0200 Subject: [PATCH] :bug: Avoid loops in main instance sync --- common/src/app/common/pages/changes.cljc | 2 +- .../app/main/data/workspace/libraries.cljs | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/common/src/app/common/pages/changes.cljc b/common/src/app/common/pages/changes.cljc index 1c7f580c5..feedf70bd 100644 --- a/common/src/app/common/pages/changes.cljc +++ b/common/src/app/common/pages/changes.cljc @@ -472,7 +472,7 @@ (defmulti components-changed (fn [_ change] (:type change))) (defmethod components-changed :mod-obj - [file-data {:keys [id page-id _component-id operations]}] + [file-data {:keys [id page-id operations]}] (when page-id (let [page (ctpl/get-page file-data page-id) shape-and-parents (map #(ctn/get-shape page %) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 79b0ad4cf..5a8dadce2 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -721,7 +721,13 @@ (when sync-typographies? (dwlh/generate-sync-file it file-id :typographies asset-id library-id state))]) - changes (pcb/concat-changes library-changes file-changes)] + changes (pcb/concat-changes library-changes file-changes) + + changes (cond-> changes + ; Store the origin component to avoid sync loops + (= asset-type :components) + (update :redo-changes + #(vary-meta % assoc :origin-component-id asset-id)))] (log/debug :msg "SYNC-FILE finished" :js/rchanges (log-changes (:redo-changes changes) @@ -842,10 +848,25 @@ check-changes (fn [[event data]] - (let [{:keys [changes save-undo?]} (deref event) + (let [page (wsh/lookup-page state) + + [{:keys [changes save-undo?]} (deref event) components-changed (reduce #(into %1 (ch/components-changed data %2)) #{} - changes)] + changes) + + orig-component-id (get (meta changes) :origin-component-id) + + is-orig (fn [shape-id] + (let [shape (ctn/get-shape page shape-id)] + (= (:component-id shape) orig-component-id))) + + ;; If the changes are derived of the synchronization of a main component, + ;; ignore them to avoid entering loops. + components-changed (if orig-component-id + (set (remove is-orig components-changed)) + components-changed)] + (when (and (d/not-empty? components-changed) save-undo?) (log/info :msg "DETECTED COMPONENTS CHANGED" :ids (map str components-changed)) @@ -890,6 +911,7 @@ (let [state (dissoc state :files)] (assoc state :workspace-shared-files files))))) + (defn fetch-shared-files [{:keys [team-id] :as params}] (us/assert ::us/uuid team-id)