2016-11-27 21:53:12 +01:00
|
|
|
;; 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/.
|
|
|
|
;;
|
2017-01-12 17:39:26 +01:00
|
|
|
;; Copyright (c) 2015-2017 Andrey Antukh <niwi@niwi.nz>
|
2016-11-27 21:53:12 +01:00
|
|
|
|
2017-01-13 22:01:13 +01:00
|
|
|
(ns uxbox.main.store
|
2016-11-27 21:53:12 +01:00
|
|
|
(:require [beicon.core :as rx]
|
|
|
|
[lentes.core :as l]
|
2017-01-13 22:01:13 +01:00
|
|
|
[potok.core :as ptk]
|
|
|
|
[uxbox.builtins.colors :as colors]
|
|
|
|
[uxbox.util.storage :refer [storage]]))
|
2019-07-03 09:30:59 +02:00
|
|
|
|
2016-11-27 21:53:12 +01:00
|
|
|
(enable-console-print!)
|
|
|
|
|
|
|
|
(def ^:dynamic *on-error* identity)
|
|
|
|
|
|
|
|
(defonce state (atom {}))
|
|
|
|
(defonce loader (atom false))
|
|
|
|
(defonce store (ptk/store {:on-error #(*on-error* %)}))
|
2017-01-12 17:39:26 +01:00
|
|
|
(defonce stream (ptk/input-stream store))
|
2016-11-27 21:53:12 +01:00
|
|
|
|
2019-09-20 17:30:03 +02:00
|
|
|
;; (defn repr-event
|
|
|
|
;; [event]
|
|
|
|
;; (cond
|
|
|
|
;; (satisfies? ptk/Event event)
|
|
|
|
;; (str "typ: " (pr-str (ptk/type event)))
|
|
|
|
|
|
|
|
;; (and (fn? event)
|
|
|
|
;; (pos? (count (.-name event))))
|
|
|
|
;; (str "fn: " (demunge (.-name event)))
|
|
|
|
|
|
|
|
;; :else
|
|
|
|
;; (str "unk: " (pr-str event))))
|
|
|
|
|
|
|
|
;; (defonce debug (as-> stream $
|
|
|
|
;; (rx/filter ptk/event? $)
|
|
|
|
;; (rx/subscribe $ (fn [event]
|
|
|
|
;; (println "[stream]: " (repr-event event))))))
|
|
|
|
|
2016-11-27 21:53:12 +01:00
|
|
|
(def auth-ref
|
|
|
|
(-> (l/key :auth)
|
|
|
|
(l/derive state)))
|
|
|
|
|
|
|
|
(defn emit!
|
|
|
|
([event]
|
2019-08-08 16:27:37 +02:00
|
|
|
(ptk/emit! store event)
|
|
|
|
nil)
|
2016-11-27 21:53:12 +01:00
|
|
|
([event & events]
|
2019-08-08 16:27:37 +02:00
|
|
|
(apply ptk/emit! store (cons event events))
|
|
|
|
nil))
|
2016-11-27 21:53:12 +01:00
|
|
|
|
2019-07-01 19:40:01 +02:00
|
|
|
(def initial-state
|
2017-01-13 22:01:13 +01:00
|
|
|
{:dashboard {:project-order :name
|
|
|
|
:project-filter ""
|
|
|
|
:images-order :name
|
|
|
|
:images-filter ""}
|
|
|
|
:route nil
|
2019-07-01 19:40:01 +02:00
|
|
|
:router nil
|
2019-07-03 09:30:59 +02:00
|
|
|
:auth (:auth storage)
|
|
|
|
:profile (:profile storage)
|
2017-01-13 22:01:13 +01:00
|
|
|
:clipboard #queue []
|
|
|
|
:undo {}
|
|
|
|
:workspace nil
|
|
|
|
:images-collections nil
|
|
|
|
:images nil
|
2019-06-19 16:52:42 +02:00
|
|
|
:icons-collections nil
|
2017-01-13 22:01:13 +01:00
|
|
|
:icons nil
|
|
|
|
:colors-collections colors/collections
|
|
|
|
:shapes nil
|
|
|
|
:projects nil
|
|
|
|
:pages nil})
|
|
|
|
|
2016-11-27 21:53:12 +01:00
|
|
|
(defn init
|
|
|
|
"Initialize the state materialization."
|
2019-07-01 19:40:01 +02:00
|
|
|
([] (init {}))
|
|
|
|
([props]
|
|
|
|
(emit! #(merge % initial-state props))
|
|
|
|
(rx/to-atom store state)))
|