From 7e7fa96f16401b4ff1a447ce7971efc874207bd9 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sun, 27 Mar 2016 21:01:25 +0300 Subject: [PATCH] Move page changes watching and persistence out of ui. --- src/uxbox/data/pages.cljs | 22 +++++++++++++++++++--- src/uxbox/state/project.cljs | 14 ++++---------- src/uxbox/ui/workspace.cljs | 33 ++------------------------------- 3 files changed, 25 insertions(+), 44 deletions(-) diff --git a/src/uxbox/data/pages.cljs b/src/uxbox/data/pages.cljs index 7fbe6b68c..d5b5fd034 100644 --- a/src/uxbox/data/pages.cljs +++ b/src/uxbox/data/pages.cljs @@ -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] diff --git a/src/uxbox/state/project.cljs b/src/uxbox/state/project.cljs index 646da0f28..725d67f36 100644 --- a/src/uxbox/state/project.cljs +++ b/src/uxbox/state/project.cljs @@ -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." diff --git a/src/uxbox/ui/workspace.cljs b/src/uxbox/ui/workspace.cljs index d068e4127..fbf46b510 100644 --- a/src/uxbox/ui/workspace.cljs +++ b/src/uxbox/ui/workspace.cljs @@ -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)))))