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/.
|
|
|
|
;;
|
2020-04-11 19:22:24 +02:00
|
|
|
;; Copyright (c) 2020 UXBOX Labs SL
|
2016-11-27 21:53:12 +01:00
|
|
|
|
2020-08-18 19:26:37 +02:00
|
|
|
(ns app.main.store
|
2020-04-11 19:22:24 +02:00
|
|
|
(:require
|
|
|
|
[beicon.core :as rx]
|
|
|
|
[okulary.core :as l]
|
|
|
|
[potok.core :as ptk]
|
2020-08-18 19:26:37 +02:00
|
|
|
[app.common.uuid :as uuid]
|
|
|
|
[app.util.storage :refer [storage]]
|
|
|
|
[app.util.debug :refer [debug? logjs]]))
|
2019-07-03 09:30:59 +02:00
|
|
|
|
2016-11-27 21:53:12 +01:00
|
|
|
(enable-console-print!)
|
|
|
|
|
|
|
|
(def ^:dynamic *on-error* identity)
|
|
|
|
|
2020-08-19 13:05:38 +02:00
|
|
|
(defonce state (l/atom {}))
|
2020-04-14 17:00:52 +02:00
|
|
|
(defonce loader (l/atom false))
|
2020-08-19 13:05:38 +02:00
|
|
|
(defonce store (ptk/store {:resolve ptk/resolve}))
|
2017-01-12 17:39:26 +01:00
|
|
|
(defonce stream (ptk/input-stream store))
|
2016-11-27 21:53:12 +01:00
|
|
|
|
2020-03-10 11:44:57 +01:00
|
|
|
(defn- repr-event
|
2019-12-04 20:13:35 +01:00
|
|
|
[event]
|
|
|
|
(cond
|
|
|
|
(satisfies? ptk/Event event)
|
|
|
|
(str "typ: " (pr-str (ptk/type event)))
|
2019-09-20 17:30:03 +02:00
|
|
|
|
2019-12-04 20:13:35 +01:00
|
|
|
(and (fn? event)
|
|
|
|
(pos? (count (.-name event))))
|
|
|
|
(str "fn: " (demunge (.-name event)))
|
2019-09-20 17:30:03 +02:00
|
|
|
|
2019-12-04 20:13:35 +01:00
|
|
|
:else
|
|
|
|
(str "unk: " (pr-str event))))
|
2019-09-20 17:30:03 +02:00
|
|
|
|
2020-03-10 11:44:57 +01:00
|
|
|
(when *assert*
|
|
|
|
(defonce debug-subscription
|
|
|
|
(as-> stream $
|
2020-04-29 10:03:38 +02:00
|
|
|
#_(rx/filter ptk/event? $)
|
|
|
|
(rx/filter (fn [s] (debug? :events)) $)
|
2020-03-10 11:44:57 +01:00
|
|
|
(rx/subscribe $ (fn [event]
|
|
|
|
(println "[stream]: " (repr-event event)))))))
|
2016-11-27 21:53:12 +01:00
|
|
|
(defn emit!
|
2020-04-02 17:08:24 +02:00
|
|
|
([] nil)
|
2016-11-27 21:53:12 +01:00
|
|
|
([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
|
|
|
|
2020-09-25 14:51:21 +02:00
|
|
|
(defn emitf
|
|
|
|
[& events]
|
|
|
|
#(apply ptk/emit! store events))
|
|
|
|
|
2019-07-01 19:40:01 +02:00
|
|
|
(def initial-state
|
2020-02-20 09:41:30 +01:00
|
|
|
{:session-id (uuid/next)
|
|
|
|
:profile (:profile storage)})
|
2017-01-13 22:01:13 +01:00
|
|
|
|
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)))
|
2020-04-29 10:03:38 +02:00
|
|
|
|
|
|
|
(defn ^:export dump-state []
|
|
|
|
(logjs "state" @state))
|
|
|
|
|
|
|
|
(defn ^:export dump-objects []
|
|
|
|
(let [page-id (get @state :current-page-id)]
|
2020-09-01 15:09:57 +02:00
|
|
|
(logjs "state" (get-in @state [:workspace-data :pages-index page-id :objects]))))
|