0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-13 16:21:57 -05:00

Normalize state initialization into uxbox.state ns.

This commit is contained in:
Andrey Antukh 2016-03-15 20:51:51 +02:00
parent 5f5fa2a2d7
commit abd68ad2e9
3 changed files with 23 additions and 100 deletions

View file

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

View file

@ -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 <niwi@niwi.nz>
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
(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)))))

View file

@ -6,16 +6,22 @@
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
(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! %))))