From abd68ad2e9d09fde86dbb708efe4693db77fde9f Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 15 Mar 2016 20:51:51 +0200 Subject: [PATCH] Normalize state initialization into uxbox.state ns. --- src/uxbox/core.cljs | 26 ++------------ src/uxbox/data/load.cljs | 73 ---------------------------------------- src/uxbox/state.cljs | 24 ++++++++++--- 3 files changed, 23 insertions(+), 100 deletions(-) delete mode 100644 src/uxbox/data/load.cljs diff --git a/src/uxbox/core.cljs b/src/uxbox/core.cljs index eb5418266..1deef2cb7 100644 --- a/src/uxbox/core.cljs +++ b/src/uxbox/core.cljs @@ -7,35 +7,15 @@ (ns uxbox.core (:require-macros [uxbox.util.syntax :refer [define-once]]) - (:require [beicon.core :as rx] - [lentes.core :as l] - [uxbox.state :as st] + (:require [uxbox.state :as st] [uxbox.router :as rt] [uxbox.rstore :as rs] - [uxbox.ui :as ui] - [uxbox.data.load :as dl])) + [uxbox.ui :as ui])) (enable-console-print!) -(defn- main - [] - (let [lens (l/select-keys dl/+persistent-keys+) - stream (->> (l/focus-atom lens st/state) - (rx/from-atom) - (rx/dedupe) - (rx/debounce 1000) - (rx/tap #(println "[save]")))] - (rx/on-value stream #(dl/persist-state %)))) - (define-once :setup (println "bootstrap") (st/init) (rt/init) - (ui/init) - - (rs/emit! (dl/load-data)) - - ;; During development, you can comment the - ;; following call for disable temprary the - ;; local persistence. - (main)) + (ui/init)) diff --git a/src/uxbox/data/load.cljs b/src/uxbox/data/load.cljs deleted file mode 100644 index da795596b..000000000 --- a/src/uxbox/data/load.cljs +++ /dev/null @@ -1,73 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) 2015-2016 Andrey Antukh -;; Copyright (c) 2015-2016 Juan de la Cruz - -(ns uxbox.data.load - (:require [hodgepodge.core :refer [local-storage]] - [uxbox.rstore :as rs] - [uxbox.router :as r] - [uxbox.state :as st] - [uxbox.schema :as sc] - [uxbox.state.project :as stpr] - [uxbox.data.projects :as dp] - [bouncer.validators :as v])) - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Helpers -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(defn assoc-color - "A reduce function for assoc the project - to the state map." - [state color-coll] - (let [uuid (:id color-coll)] - (update state :colors-by-id assoc uuid color-coll))) - -(defn assoc-shape - [state shape] - (let [id (:id shape)] - (update state :shapes-by-id assoc id shape))) - -(def ^:const ^:private +persistent-keys+ - [:auth - :pages-by-id - :shapes-by-id - :colors-by-id - :projects-by-id]) - -(defn persist-state - [state] - (let [pages (into #{} (vals (:pages-by-id state))) - projects (into #{} (vals (:projects-by-id state))) - shapes (into #{} (vals (:shapes-by-id state))) - color-colls (into #{} (vals (:colors-by-id state)))] - (assoc! local-storage ::auth (:auth state)) - (assoc! local-storage ::data {:pages pages - :shapes shapes - :projects projects - :color-collections color-colls}))) - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Events -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(defn load-data - "Load data from local storage." - [] - (reify - rs/UpdateEvent - (-apply-update [_ state] - (let [auth (::auth local-storage) - data (::data local-storage) - state (assoc state :auth auth)] - (if data - (as-> state $ - (reduce stpr/assoc-project $ (:projects data)) - (reduce stpr/assoc-page $ (:pages data)) - (reduce assoc-color $ (:color-collections data)) - (reduce assoc-shape $ (:shapes data))) - state))))) - diff --git a/src/uxbox/state.cljs b/src/uxbox/state.cljs index fcb052e8c..8a05fbb56 100644 --- a/src/uxbox/state.cljs +++ b/src/uxbox/state.cljs @@ -6,16 +6,22 @@ ;; Copyright (c) 2015-2016 Juan de la Cruz (ns uxbox.state - (:require [beicon.core :as rx] + (:require [hodgepodge.core :refer [local-storage]] + [beicon.core :as rx] + [lentes.core :as l] [uxbox.rstore :as rs])) +(def +storage+ local-storage) +(def ^:const ^:private +persistent-keys+ + [:auth]) + (defonce state (atom {})) (defonce stream (rs/init {:dashboard {:project-order :name :project-filter ""} :route nil - :auth {} + :auth (::auth +storage+) :workspace nil :shapes-by-id {} :elements-by-id {} @@ -24,8 +30,18 @@ :projects-by-id {} :pages-by-id {}})) +(defn- persist-state! + [state] + (assoc! +storage+ ::auth (:auth state))) + (defn init "Initialize the state materialization." [] - (as-> stream $ - (rx/to-atom $ state))) + (rx/to-atom stream state) + (let [lens (l/select-keys +persistent-keys+) + stream (->> (l/focus-atom lens state) + (rx/from-atom) + (rx/dedupe) + (rx/debounce 1000) + (rx/tap #(println "[save]")))] + (rx/on-value stream #(persist-state! %))))