Refactor page changes watching mechanism.

This commit is contained in:
Andrey Antukh 2016-11-28 23:47:50 +01:00
parent 3accc4b66e
commit 91fb68f906
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
5 changed files with 76 additions and 75 deletions

View file

@ -25,20 +25,23 @@
(declare fetch-page-history) (declare fetch-page-history)
(declare fetch-pinned-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 (defn watch-page-changes
"A function that starts watching for `IPageUpdate` [id]
events emited to the global event stream and just (WatchPageChanges. id))
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))))
;; --- Pinned Page History Fetched ;; --- Pinned Page History Fetched

View file

@ -47,6 +47,10 @@
"A marker protocol for mark events that alters the "A marker protocol for mark events that alters the
page and is subject to perform a backend synchronization.") 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 ;; --- Helpers
(defn pack-page (defn pack-page
@ -200,21 +204,6 @@
[id] [id]
(PersistPage. 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 ;; --- Page Metadata Persisted
(defrecord MetadataPersisted [id data] (defrecord MetadataPersisted [id data]
@ -253,13 +242,10 @@
;; --- Update Page Options ;; --- Update Page Options
(defrecord UpdateMetadata [id metadata] (defrecord UpdateMetadata [id metadata]
IMetadataUpdate
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [this state]
(assoc-in state [:pages id :metadata] metadata)) (assoc-in state [:pages id :metadata] metadata)))
ptk/WatchEvent
(watch [this state s]
(rx/of (persist-metadata id))))
(defn update-metadata (defn update-metadata
[id metadata] [id metadata]
@ -271,7 +257,6 @@
(defrecord UpdatePage [id name width height layout] (defrecord UpdatePage [id name width height layout]
ptk/UpdateEvent ptk/UpdateEvent
(update [this state] (update [this state]
(println "update-page" this)
(-> state (-> state
(assoc-in [:pages id :name] name) (assoc-in [:pages id :name] name)
(assoc-in [:pages id :metadata :width] width) (assoc-in [:pages id :metadata :width] width)
@ -305,3 +290,27 @@
(defn delete-page (defn delete-page
([id] (DeletePage. id (constantly nil))) ([id] (DeletePage. id (constantly nil)))
([id callback] (DeletePage. id callback))) ([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))

View file

@ -19,19 +19,23 @@
(declare redo?) (declare redo?)
(declare initialize-undo-for-page) (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 (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] [id]
(st/emit! (initialize-undo-for-page id)) (WatchPageChanges. 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)))))
;; -- Save Undo Entry ;; -- Save Undo Entry

View file

@ -47,49 +47,35 @@
[own] [own]
(let [[projectid pageid] (:rum/args own) (let [[projectid pageid] (:rum/args own)
sub1 (scroll/watch-scroll-interactions 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")] 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 initial scroll position
(set! (.-scrollLeft dom) (* c/canvas-start-scroll-x @wb/zoom-ref)) (set! (.-scrollLeft dom) (* c/canvas-start-scroll-x @wb/zoom-ref))
(set! (.-scrollTop dom) (* c/canvas-start-scroll-y @wb/zoom-ref)) (set! (.-scrollTop dom) (* c/canvas-start-scroll-y @wb/zoom-ref))
(assoc own (assoc own ::sub1 sub1)))
::sub1 sub1
::sub2 sub2
::sub3 sub3
::sub4 sub4)))
(defn- workspace-will-unmount (defn- workspace-will-unmount
[own] [own]
;; Close subscriptions (st/emit! ::udp/stop-page-watcher)
(.close (::sub1 own)) (.close (::sub1 own))
(.close (::sub2 own)) (dissoc own ::sub1))
(.close (::sub3 own))
(.close (::sub4 own))
(dissoc own ::sub1 ::sub2 ::sub3 ::sub4))
(defn- workspace-did-remount (defn- workspace-did-remount
[old-state state] [old-state state]
(let [[projectid pageid] (:rum/args state) (let [[projectid pageid] (:rum/args state)
[oldprojectid oldpageid] (:rum/args old-state)] [oldprojectid oldpageid] (:rum/args old-state)]
(if (not= pageid oldpageid) (when (not= pageid oldpageid)
(do (st/emit! (dw/initialize projectid pageid)
(st/emit! (dw/initialize projectid pageid)) ::udp/stop-page-watcher
(.close (::sub2 old-state)) (udp/watch-page-changes pageid)
(.close (::sub3 old-state)) (udu/watch-page-changes pageid)
(assoc state (udh/watch-page-changes pageid)))
::sub1 (::sub1 old-state) 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)))))
(defn- on-scroll (defn- on-scroll
[event] [event]

View file

@ -107,7 +107,6 @@
(let [menus (get +menus-map+ (:type shape ::page)) (let [menus (get +menus-map+ (:type shape ::page))
contained-in? (into #{} menus) contained-in? (into #{} menus)
active (:menu @local (first menus))] active (:menu @local (first menus))]
(println "options" active)
[:div [:div
[:ul.element-icons [:ul.element-icons
(for [menu-id (get +menus-map+ (:type shape ::page)) (for [menu-id (get +menus-map+ (:type shape ::page))