From 91fb68f906c2984be66219356de56cb4a19e6e16 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 28 Nov 2016 23:47:50 +0100 Subject: [PATCH] Refactor page changes watching mechanism. --- frontend/src/uxbox/main/data/history.cljs | 29 +++++----- frontend/src/uxbox/main/data/pages.cljs | 53 +++++++++++-------- frontend/src/uxbox/main/data/undo.cljs | 26 +++++---- frontend/src/uxbox/main/ui/workspace.cljs | 42 +++++---------- .../main/ui/workspace/sidebar/options.cljs | 1 - 5 files changed, 76 insertions(+), 75 deletions(-) diff --git a/frontend/src/uxbox/main/data/history.cljs b/frontend/src/uxbox/main/data/history.cljs index 8f60697f4..a07060c68 100644 --- a/frontend/src/uxbox/main/data/history.cljs +++ b/frontend/src/uxbox/main/data/history.cljs @@ -25,20 +25,23 @@ (declare fetch-page-history) (declare fetch-pinned-page-history) +(deftype WatchPageChanges [id] + ptk/WatchEvent + (watch [_ state stream] + (println "history:watch-page-changes" id) + (let [stopper (->> stream + (rx/filter #(= % ::udp/stop-page-watcher)) + (rx/take 1))] + (->> stream + (rx/take-until stopper) + (rx/filter udp/page-persisted?) + (rx/debounce 500) + (rx/flat-map #(rx/of (fetch-page-history id) + (fetch-pinned-page-history id))))))) + (defn watch-page-changes - "A function that starts watching for `IPageUpdate` - events emited to the global event stream and just - reacts on them emiting an other event that just - persists the state of the page in an undo stack." - [] - (letfn [(on-value [id] - (st/emit! (fetch-page-history id) - (fetch-pinned-page-history id)))] - (as-> st/store $ - (rx/filter udp/page-persisted? $) - (rx/debounce 500 $) - (rx/map (comp :id :data) $) - (rx/on-value $ on-value)))) + [id] + (WatchPageChanges. id)) ;; --- Pinned Page History Fetched diff --git a/frontend/src/uxbox/main/data/pages.cljs b/frontend/src/uxbox/main/data/pages.cljs index 47584eb5f..2a65a1288 100644 --- a/frontend/src/uxbox/main/data/pages.cljs +++ b/frontend/src/uxbox/main/data/pages.cljs @@ -47,6 +47,10 @@ "A marker protocol for mark events that alters the page and is subject to perform a backend synchronization.") +(defprotocol IMetadataUpdate + "A marker protocol for mark events that alters the + page and is subject to perform a backend synchronization.") + ;; --- Helpers (defn pack-page @@ -200,21 +204,6 @@ [id] (PersistPage. id)) -(defn watch-page-changes - "A function that starts watching for `IPageUpdate` - events emited to the global event stream and just - reacts emiting an other event in order to perform - the page persistence. - - The main behavior debounces the posible emmited - events with 1sec of delay allowing batch updates - on fastly performed events." - [id] - (as-> st/store $ - (rx/filter #(satisfies? IPageUpdate %) $) - (rx/debounce 1000 $) - (rx/on-next $ #(st/emit! (persist-page id))))) - ;; --- Page Metadata Persisted (defrecord MetadataPersisted [id data] @@ -253,13 +242,10 @@ ;; --- Update Page Options (defrecord UpdateMetadata [id metadata] + IMetadataUpdate ptk/UpdateEvent - (update [_ state] - (assoc-in state [:pages id :metadata] metadata)) - - ptk/WatchEvent - (watch [this state s] - (rx/of (persist-metadata id)))) + (update [this state] + (assoc-in state [:pages id :metadata] metadata))) (defn update-metadata [id metadata] @@ -271,7 +257,6 @@ (defrecord UpdatePage [id name width height layout] ptk/UpdateEvent (update [this state] - (println "update-page" this) (-> state (assoc-in [:pages id :name] name) (assoc-in [:pages id :metadata :width] width) @@ -305,3 +290,27 @@ (defn delete-page ([id] (DeletePage. id (constantly nil))) ([id callback] (DeletePage. id callback))) + +;; --- Watch Page Changes + +(deftype WatchPageChanges [id] + ptk/WatchEvent + (watch [_ state stream] + (let [stopper (->> stream + (rx/filter #(= % ::stop-page-watcher)) + (rx/take 1))] + (rx/merge + (->> stream + (rx/take-until stopper) + (rx/filter #(satisfies? IPageUpdate %)) + (rx/debounce 1000) + (rx/map #(persist-page id))) + (->> stream + (rx/take-until stopper) + (rx/filter #(satisfies? IMetadataUpdate %)) + (rx/debounce 1000) + (rx/map #(persist-metadata id))))))) + +(defn watch-page-changes + [id] + (WatchPageChanges. id)) diff --git a/frontend/src/uxbox/main/data/undo.cljs b/frontend/src/uxbox/main/data/undo.cljs index 1b87c5c42..a3d21f47c 100644 --- a/frontend/src/uxbox/main/data/undo.cljs +++ b/frontend/src/uxbox/main/data/undo.cljs @@ -19,19 +19,23 @@ (declare redo?) (declare initialize-undo-for-page) +(deftype WatchPageChanges [id] + ptk/WatchEvent + (watch [_ state stream] + (let [stopper (->> stream + (rx/filter #(= % ::udp/stop-page-watcher)) + (rx/take 1))] + (->> stream + (rx/take-until stopper) + (rx/filter #(satisfies? udp/IPageUpdate %)) + (rx/filter #(not (undo? %))) + (rx/filter #(not (redo? %))) + (rx/debounce 500) + (rx/map #(save-undo-entry id)))))) + (defn watch-page-changes - "A function that starts watching for `IPageUpdate` - events emited to the global event stream and just - reacts on them emiting an other event that just - persists the state of the page in an undo stack." [id] - (st/emit! (initialize-undo-for-page id)) - (as-> st/store $ - (rx/filter #(satisfies? udp/IPageUpdate %) $) - (rx/filter #(not (undo? %)) $) - (rx/filter #(not (redo? %)) $) - (rx/debounce 500 $) - (rx/on-next $ #(st/emit! (save-undo-entry id))))) + (WatchPageChanges. id)) ;; -- Save Undo Entry diff --git a/frontend/src/uxbox/main/ui/workspace.cljs b/frontend/src/uxbox/main/ui/workspace.cljs index 7bc3c45ef..73b280e02 100644 --- a/frontend/src/uxbox/main/ui/workspace.cljs +++ b/frontend/src/uxbox/main/ui/workspace.cljs @@ -47,49 +47,35 @@ [own] (let [[projectid pageid] (:rum/args own) sub1 (scroll/watch-scroll-interactions own) - sub2 (udp/watch-page-changes pageid) - sub3 (udu/watch-page-changes pageid) - sub4 (udh/watch-page-changes) dom (mx/ref-node own "workspace-canvas")] + (st/emit! (udp/watch-page-changes pageid) + (udu/watch-page-changes pageid) + (udh/watch-page-changes pageid)) + ;; Set initial scroll position (set! (.-scrollLeft dom) (* c/canvas-start-scroll-x @wb/zoom-ref)) (set! (.-scrollTop dom) (* c/canvas-start-scroll-y @wb/zoom-ref)) - (assoc own - ::sub1 sub1 - ::sub2 sub2 - ::sub3 sub3 - ::sub4 sub4))) + (assoc own ::sub1 sub1))) (defn- workspace-will-unmount [own] - ;; Close subscriptions + (st/emit! ::udp/stop-page-watcher) (.close (::sub1 own)) - (.close (::sub2 own)) - (.close (::sub3 own)) - (.close (::sub4 own)) - (dissoc own ::sub1 ::sub2 ::sub3 ::sub4)) + (dissoc own ::sub1)) (defn- workspace-did-remount [old-state state] (let [[projectid pageid] (:rum/args state) [oldprojectid oldpageid] (:rum/args old-state)] - (if (not= pageid oldpageid) - (do - (st/emit! (dw/initialize projectid pageid)) - (.close (::sub2 old-state)) - (.close (::sub3 old-state)) - (assoc state - ::sub1 (::sub1 old-state) - ::sub2 (udp/watch-page-changes pageid) - ::sub3 (udu/watch-page-changes pageid) - ::sub4 (::sub4 old-state))) - (assoc state - ::sub1 (::sub1 old-state) - ::sub2 (::sub2 old-state) - ::sub3 (::sub3 old-state) - ::sub4 (::sub4 old-state))))) + (when (not= pageid oldpageid) + (st/emit! (dw/initialize projectid pageid) + ::udp/stop-page-watcher + (udp/watch-page-changes pageid) + (udu/watch-page-changes pageid) + (udh/watch-page-changes pageid))) + state)) (defn- on-scroll [event] diff --git a/frontend/src/uxbox/main/ui/workspace/sidebar/options.cljs b/frontend/src/uxbox/main/ui/workspace/sidebar/options.cljs index 8bedc0ddd..de4f704bd 100644 --- a/frontend/src/uxbox/main/ui/workspace/sidebar/options.cljs +++ b/frontend/src/uxbox/main/ui/workspace/sidebar/options.cljs @@ -107,7 +107,6 @@ (let [menus (get +menus-map+ (:type shape ::page)) contained-in? (into #{} menus) active (:menu @local (first menus))] - (println "options" active) [:div [:ul.element-icons (for [menu-id (get +menus-map+ (:type shape ::page))