0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-04-15 08:21:40 -05:00

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-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

View file

@ -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))

View file

@ -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

View file

@ -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]

View file

@ -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))