0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-12 07:41:43 -05:00

Move page changes watching and persistence out of ui.

This commit is contained in:
Andrey Antukh 2016-03-27 21:01:25 +03:00
parent 0912d6ec4f
commit 7e7fa96f16
3 changed files with 25 additions and 44 deletions

View file

@ -9,6 +9,7 @@
(:require [cuerdas.core :as str]
[promesa.core :as p]
[beicon.core :as rx]
[lentes.core :as l]
[uxbox.rstore :as rs]
[uxbox.router :as r]
[uxbox.repo :as rp]
@ -55,7 +56,7 @@
rs/WatchEvent
(-apply-watch [this state s]
(letfn [(on-created [{page :payload}]
#(stpr/assoc-page % page))
#(stpr/unpack-page % page))
(on-failed [page]
(uum/error (tr "errors.auth"))
(rx/empty))]
@ -101,7 +102,7 @@
(rx/map on-success)
(rx/catch on-failure)))))
(def ^:static +update-page-schema+
(def ^:const +update-page-schema+
{:name [sc/required sc/string]
:width [sc/required sc/integer]
:height [sc/required sc/integer]
@ -112,6 +113,22 @@
(sc/validate! +update-page-schema+ data)
(map->UpdatePage data))
(defn watch-page-changes
[id]
(letfn [(on-page-change [buffer]
#_(println "on-page-change" buffer)
(let [page (second buffer)]
(rs/emit! (update-page page))))]
(let [lens (l/getter #(stpr/pack-page % id))]
(as-> (l/focus-atom lens st/state) $
(rx/from-atom $)
(rx/debounce 1000 $)
(rx/scan (fn [acc page]
(if (>= (:version page) (:version acc)) page acc)) $)
(rx/dedupe #(dissoc % :version) $)
(rx/buffer 2 1 $)
(rx/subscribe $ on-page-change #(throw %))))))
;; --- Update Page Metadata
;; This is a simplified version of `UpdatePage` event
@ -146,7 +163,6 @@
(sc/validate! +update-page-schema+ data)
(map->UpdatePageMetadata (dissoc data :data)))
;; --- Delete Page (by id)
(defrecord DeletePage [id]

View file

@ -14,13 +14,6 @@
[state id]
(update-in state [:projects-by-id] dissoc id))
(defn assoc-page
"A reduce function for assoc the page
to the state map."
[state page]
(let [uuid (:id page)]
(update-in state [:pages-by-id] assoc uuid page)))
(defn dissoc-page-shapes
[state id]
(let [shapes (get-in state [:shapes-by-id])]
@ -47,14 +40,15 @@
"Unpacks packed page object and assocs it to the
provided state."
[state page]
(let [shapes (get-in page [:data :shapes])
shapes-by-id (get-in page [:data :shapes-by-id])
(let [data (:data page)
shapes (:shapes data)
shapes-by-id (:shapes-by-id data)
page (-> page
(dissoc page :data)
(assoc :shapes shapes))]
(-> state
(update :shapes-by-id merge shapes-by-id)
(assoc-page page))))
(update-in [:pages-by-id] assoc (:id page) page))))
(defn dissoc-page
"Remove page and all related stuff from the state."

View file

@ -28,35 +28,6 @@
;; Workspace
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- focus-page
[id]
(as-> (ul/getter #(stpr/pack-page % id)) $
(l/focus-atom $ st/state)))
;; TODO: move out of UI.
(defn- on-page-change
[buffer]
#_(println "on-page-change" buffer)
(let [page (second buffer)]
(rs/emit! (udp/update-page page))))
(defn- subscribe-to-page-changes
[pageid]
(as-> (focus-page pageid) $
(rx/from-atom $)
(rx/debounce 1000 $)
(rx/scan (fn [acc page]
#_(println "change:" (:version acc) "->" (:version page))
(let [result (if (>= (:version page) (:version acc))
page
acc)]
;; (println "!!!" (:version result))
result)) $)
(rx/dedupe #(dissoc % :version) $)
(rx/buffer 2 1 $)
(rx/subscribe $ on-page-change #(throw %))))
(defn- workspace-will-mount
[own]
(let [[projectid pageid] (:rum/props own)]
@ -97,7 +68,7 @@
(rx/dedupe $)
(rx/filter #(= :scroll/viewport %) $)
(rx/on-value $ handle-scroll-interaction))
sub2 (subscribe-to-page-changes pageid)]
sub2 (udp/watch-page-changes pageid)]
(set! (.-scrollLeft el) uuwb/canvas-start-scroll-x)
(set! (.-scrollTop el) uuwb/canvas-start-scroll-y)
(assoc own ::sub1 sub1 ::sub2 sub2))))
@ -120,7 +91,7 @@
(.close (::sub2 old-state))
(assoc state
::sub1 (::sub1 old-state)
::sub2 (subscribe-to-page-changes pageid)))
::sub2 (udp/watch-page-changes pageid)))
(assoc state
::sub1 (::sub1 old-state)
::sub2 (::sub2 old-state)))))