mirror of
https://github.com/penpot/penpot.git
synced 2025-03-15 01:01:30 -05:00
Normalize state initialization into uxbox.state ns.
This commit is contained in:
parent
5f5fa2a2d7
commit
abd68ad2e9
3 changed files with 23 additions and 100 deletions
|
@ -7,35 +7,15 @@
|
||||||
|
|
||||||
(ns uxbox.core
|
(ns uxbox.core
|
||||||
(:require-macros [uxbox.util.syntax :refer [define-once]])
|
(:require-macros [uxbox.util.syntax :refer [define-once]])
|
||||||
(:require [beicon.core :as rx]
|
(:require [uxbox.state :as st]
|
||||||
[lentes.core :as l]
|
|
||||||
[uxbox.state :as st]
|
|
||||||
[uxbox.router :as rt]
|
[uxbox.router :as rt]
|
||||||
[uxbox.rstore :as rs]
|
[uxbox.rstore :as rs]
|
||||||
[uxbox.ui :as ui]
|
[uxbox.ui :as ui]))
|
||||||
[uxbox.data.load :as dl]))
|
|
||||||
|
|
||||||
(enable-console-print!)
|
(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
|
(define-once :setup
|
||||||
(println "bootstrap")
|
(println "bootstrap")
|
||||||
(st/init)
|
(st/init)
|
||||||
(rt/init)
|
(rt/init)
|
||||||
(ui/init)
|
(ui/init))
|
||||||
|
|
||||||
(rs/emit! (dl/load-data))
|
|
||||||
|
|
||||||
;; During development, you can comment the
|
|
||||||
;; following call for disable temprary the
|
|
||||||
;; local persistence.
|
|
||||||
(main))
|
|
||||||
|
|
|
@ -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)))))
|
|
||||||
|
|
|
@ -6,16 +6,22 @@
|
||||||
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
||||||
|
|
||||||
(ns uxbox.state
|
(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]))
|
[uxbox.rstore :as rs]))
|
||||||
|
|
||||||
|
(def +storage+ local-storage)
|
||||||
|
(def ^:const ^:private +persistent-keys+
|
||||||
|
[:auth])
|
||||||
|
|
||||||
(defonce state (atom {}))
|
(defonce state (atom {}))
|
||||||
|
|
||||||
(defonce stream
|
(defonce stream
|
||||||
(rs/init {:dashboard {:project-order :name
|
(rs/init {:dashboard {:project-order :name
|
||||||
:project-filter ""}
|
:project-filter ""}
|
||||||
:route nil
|
:route nil
|
||||||
:auth {}
|
:auth (::auth +storage+)
|
||||||
:workspace nil
|
:workspace nil
|
||||||
:shapes-by-id {}
|
:shapes-by-id {}
|
||||||
:elements-by-id {}
|
:elements-by-id {}
|
||||||
|
@ -24,8 +30,18 @@
|
||||||
:projects-by-id {}
|
:projects-by-id {}
|
||||||
:pages-by-id {}}))
|
:pages-by-id {}}))
|
||||||
|
|
||||||
|
(defn- persist-state!
|
||||||
|
[state]
|
||||||
|
(assoc! +storage+ ::auth (:auth state)))
|
||||||
|
|
||||||
(defn init
|
(defn init
|
||||||
"Initialize the state materialization."
|
"Initialize the state materialization."
|
||||||
[]
|
[]
|
||||||
(as-> stream $
|
(rx/to-atom stream state)
|
||||||
(rx/to-atom $ 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! %))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue