🎉 Notify shared files clients for update

This commit is contained in:
Andrés Moya 2020-09-17 15:34:31 +02:00 committed by Andrey Antukh
parent 494cd1e96c
commit 3b516aa139
6 changed files with 80 additions and 31 deletions

View file

@ -345,6 +345,16 @@
qparams {:page-id (first (get-in file [:data :pages]))}]
(st/emit! (rt/nav-new-window :workspace pparams qparams))))))
(defn ext-library-changed
[file-id changes]
(us/assert ::us/uuid file-id)
(us/assert ::cp/changes changes)
(ptk/reify ::ext-library-changed
ptk/UpdateEvent
(update [_ state]
(d/update-in-when state [:workspace-libraries file-id :data]
cp/process-changes changes))))
(declare generate-sync-file)
(declare generate-sync-page)
(declare generate-sync-shape-and-children)
@ -407,12 +417,12 @@
[new-shape new-shapes _]
(cph/clone-object root-shape nil objects update-new-shape)
rchanges [{:type :update-component
rchanges [{:type :mod-component
:id component-id
:name (:name new-shape)
:shapes new-shapes}]
uchanges [{:type :update-component
uchanges [{:type :mod-component
:id component-id
:name (:name component-obj)
:shapes (vals component-objs)}]]
@ -420,7 +430,7 @@
(rx/of (dwc/commit-changes rchanges uchanges {:commit-local? true}))))))
(defn sync-file
[{:keys [file-id] :as params}]
[file-id]
(us/assert (s/nilable ::us/uuid) file-id)
(ptk/reify ::sync-file
ptk/WatchEvent
@ -434,22 +444,25 @@
(if (nil? file-id)
(get-in state [:workspace-data :components])
(get-in state [:workspace-libraries file-id :data :components]))]
(loop [pages (seq (vals (get-in state [:workspace-data :pages-index])))
rchanges []
uchanges []]
(let [page (first pages)]
(if (nil? page)
[rchanges uchanges]
(let [[page-rchanges page-uchanges]
(generate-sync-page page components)]
(recur (next pages)
(concat rchanges page-rchanges)
(concat uchanges page-uchanges))))))))
(when (some? components)
(loop [pages (seq (vals (get-in state [:workspace-data :pages-index])))
rchanges []
uchanges []]
(let [page (first pages)]
(if (nil? page)
[rchanges uchanges]
(let [[page-rchanges page-uchanges]
(generate-sync-page file-id page components)]
(recur (next pages)
(concat rchanges page-rchanges)
(concat uchanges page-uchanges)))))))))
(defn- generate-sync-page
[page components]
[file-id page components]
(let [linked-shapes
(cph/select-objects #(some? (:component-id %)) page)]
(cph/select-objects #(and (some? (:component-id %))
(= (:component-file %) file-id))
page)]
(loop [shapes (seq linked-shapes)
rchanges []
uchanges []]

View file

@ -15,6 +15,7 @@
[app.common.spec :as us]
[app.main.data.workspace.common :as dwc]
[app.main.data.workspace.persistence :as dwp]
[app.main.data.workspace.libraries :as dwl]
[app.main.repo :as rp]
[app.main.store :as st]
[app.main.streams :as ms]
@ -31,6 +32,7 @@
(declare handle-presence)
(declare handle-pointer-update)
(declare handle-file-change)
(declare handle-library-change)
(declare handle-pointer-send)
(declare send-keepalive)
@ -73,6 +75,7 @@
:presence (handle-presence msg)
:pointer-update (handle-pointer-update msg)
:file-change (handle-file-change msg)
:library-change (handle-library-change msg)
::unknown))
(defn- send-keepalive
@ -197,3 +200,17 @@
(rx/of (dwp/shapes-changes-persisted file-id msg))
(when (seq page-ids)
(rx/from (map dwc/update-indices page-ids))))))))
(s/def ::library-change-event
(s/keys :req-un [::type ::profile-id ::file-id ::session-id ::revn ::changes]))
(defn handle-library-change
[{:keys [file-id changes] :as msg}]
(us/assert ::library-change-event msg)
(ptk/reify ::handle-library-change
ptk/WatchEvent
(watch [_ state stream]
(when (contains? (:workspace-libraries state) file-id)
(rx/of (dwl/ext-library-changed file-id changes)
(dwl/sync-file file-id))))))

View file

@ -67,7 +67,7 @@
do-reset-component #(st/emit! (dwl/reset-component id))
do-update-component #(do
(st/emit! (dwl/update-component id))
(st/emit! (dwl/sync-file {:file-id nil})))
(st/emit! (dwl/sync-file nil)))
do-navigate-component-file #(st/emit! (dwl/nav-to-component-file
(:component-file root-shape)))]
[:*

View file

@ -51,7 +51,7 @@
(mf/deps state)
(fn []
(st/emit! (dwl/delete-component {:id (:component-id @state)}))
(st/emit! (dwl/sync-file {:file-id nil}))))
(st/emit! (dwl/sync-file nil))))
on-context-menu
(mf/use-callback