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-10-05 18:20:39 +02:00
|
|
|
(:require-macros [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-09-28 15:29:54 +02:00
|
|
|
[cuerdas.core :as str]
|
|
|
|
[app.common.data :as d]
|
2020-12-10 13:56:33 +01:00
|
|
|
[app.common.pages :as cp]
|
2021-01-19 18:44:32 +01:00
|
|
|
[app.common.pages.helpers :as helpers]
|
2020-08-18 19:26:37 +02:00
|
|
|
[app.common.uuid :as uuid]
|
|
|
|
[app.util.storage :refer [storage]]
|
2020-12-04 17:35:13 +01:00
|
|
|
[app.util.debug :refer [debug? debug-exclude-events 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-04-14 17:00:52 +02:00
|
|
|
(defonce loader (l/atom false))
|
2020-12-21 09:47:50 +01:00
|
|
|
(defonce state (ptk/store {:resolve ptk/resolve}))
|
|
|
|
(defonce stream (ptk/input-stream state))
|
2016-11-27 21:53:12 +01:00
|
|
|
|
2021-01-22 14:33:18 +01:00
|
|
|
(defn ^boolean is-logged?
|
|
|
|
[pdata]
|
|
|
|
(and (some? pdata)
|
|
|
|
(uuid? (:id pdata))
|
|
|
|
(not= uuid/zero (:id pdata))))
|
|
|
|
|
2020-03-10 11:44:57 +01:00
|
|
|
(when *assert*
|
|
|
|
(defonce debug-subscription
|
2020-11-10 17:52:23 +01:00
|
|
|
(->> stream
|
|
|
|
(rx/filter ptk/event?)
|
2020-12-04 17:35:13 +01:00
|
|
|
(rx/filter (fn [s] (and (debug? :events)
|
|
|
|
(not (debug-exclude-events (ptk/type s))))))
|
2020-12-21 11:35:13 +01:00
|
|
|
(rx/subs #(println "[stream]: " (ptk/repr-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]
|
2020-12-21 09:47:50 +01:00
|
|
|
(ptk/emit! state event)
|
2019-08-08 16:27:37 +02:00
|
|
|
nil)
|
2016-11-27 21:53:12 +01:00
|
|
|
([event & events]
|
2020-12-21 09:47:50 +01:00
|
|
|
(apply ptk/emit! state (cons event events))
|
2019-08-08 16:27:37 +02:00
|
|
|
nil))
|
2016-11-27 21:53:12 +01:00
|
|
|
|
2020-09-25 14:51:21 +02:00
|
|
|
(defn emitf
|
|
|
|
[& events]
|
2020-12-21 09:47:50 +01:00
|
|
|
#(apply ptk/emit! state events))
|
2020-09-25 14:51:21 +02:00
|
|
|
|
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]
|
2020-12-21 09:47:50 +01:00
|
|
|
(emit! #(merge % initial-state props))))
|
2020-04-29 10:03:38 +02:00
|
|
|
|
|
|
|
(defn ^:export dump-state []
|
|
|
|
(logjs "state" @state))
|
|
|
|
|
2020-11-10 17:52:23 +01:00
|
|
|
(defn ^:export get-state [str-path]
|
|
|
|
(let [path (->> (str/split str-path " ")
|
|
|
|
(map d/read-string))]
|
|
|
|
(clj->js (get-in @state path))))
|
|
|
|
|
2020-04-29 10:03:38 +02:00
|
|
|
(defn ^:export dump-objects []
|
2020-10-26 17:24:09 +01:00
|
|
|
(let [page-id (get @state :current-page-id)]
|
|
|
|
(logjs "state" (get-in @state [:workspace-data :pages-index page-id :objects]))))
|
2020-09-28 15:29:54 +02:00
|
|
|
|
2020-10-23 15:41:06 +02:00
|
|
|
(defn ^:export dump-object [name]
|
|
|
|
(let [page-id (get @state :current-page-id)]
|
|
|
|
(let [objects (get-in @state [:workspace-data :pages-index page-id :objects])
|
2021-02-25 09:41:57 +01:00
|
|
|
target (or (d/seek (fn [[id shape]] (= name (:name shape))) objects)
|
|
|
|
(get objects (uuid name)))]
|
2020-10-23 15:41:06 +02:00
|
|
|
(->> target
|
|
|
|
(logjs "state")))))
|
|
|
|
|
2020-09-30 13:46:45 +02:00
|
|
|
(defn ^:export dump-tree
|
2020-10-22 16:14:46 +02:00
|
|
|
([] (dump-tree false false))
|
|
|
|
([show-ids] (dump-tree show-ids false))
|
|
|
|
([show-ids show-touched]
|
2020-09-30 13:46:45 +02:00
|
|
|
(let [page-id (get @state :current-page-id)
|
|
|
|
objects (get-in @state [:workspace-data :pages-index page-id :objects])
|
|
|
|
components (get-in @state [:workspace-data :components])
|
|
|
|
libraries (get-in @state [:workspace-libraries])
|
|
|
|
root (d/seek #(nil? (:parent-id %)) (vals objects))]
|
|
|
|
|
|
|
|
(letfn [(show-shape [shape-id level objects]
|
|
|
|
(let [shape (get objects shape-id)]
|
|
|
|
(println (str/pad (str (str/repeat " " level)
|
2020-10-05 11:01:14 +02:00
|
|
|
(:name shape)
|
2020-10-15 11:15:35 +02:00
|
|
|
(when (seq (:touched shape)) "*")
|
2020-10-22 16:14:46 +02:00
|
|
|
(when show-ids (str/format " <%s>" (:id shape))))
|
2020-09-30 13:46:45 +02:00
|
|
|
{:length 20
|
|
|
|
:type :right})
|
|
|
|
(show-component shape objects))
|
2021-01-13 15:11:47 +01:00
|
|
|
(when show-touched
|
|
|
|
(when (seq (:touched shape))
|
|
|
|
(println (str (str/repeat " " level)
|
2020-09-30 13:46:45 +02:00
|
|
|
" "
|
|
|
|
(str (:touched shape)))))
|
2021-01-13 15:11:47 +01:00
|
|
|
(when (:remote-synced? shape)
|
|
|
|
(println (str (str/repeat " " level)
|
|
|
|
" (remote-synced)"))))
|
2020-09-30 13:46:45 +02:00
|
|
|
(when (:shapes shape)
|
|
|
|
(dorun (for [shape-id (:shapes shape)]
|
2020-10-22 16:14:46 +02:00
|
|
|
(show-shape shape-id (inc level) objects))))))
|
2020-09-30 13:46:45 +02:00
|
|
|
|
|
|
|
(show-component [shape objects]
|
2020-10-05 11:01:14 +02:00
|
|
|
(if (nil? (:shape-ref shape))
|
|
|
|
""
|
2021-01-20 17:12:39 +01:00
|
|
|
(let [root-shape (cp/get-component-shape shape objects)
|
2020-10-05 11:01:14 +02:00
|
|
|
component-id (when root-shape (:component-id root-shape))
|
|
|
|
component-file-id (when root-shape (:component-file root-shape))
|
2020-12-04 15:31:03 +01:00
|
|
|
component-file (when component-file-id (get libraries component-file-id nil))
|
2020-10-05 11:01:14 +02:00
|
|
|
component (when component-id
|
|
|
|
(if component-file
|
|
|
|
(get-in component-file [:data :components component-id])
|
|
|
|
(get components component-id)))
|
|
|
|
component-shape (when (and component (:shape-ref shape))
|
|
|
|
(get-in component [:objects (:shape-ref shape)]))]
|
|
|
|
(str/format " %s--> %s%s%s"
|
2020-10-06 15:44:42 +02:00
|
|
|
(cond (:component-root? shape) "#"
|
|
|
|
(:component-id shape) "@"
|
|
|
|
:else "-")
|
2020-09-30 13:46:45 +02:00
|
|
|
(when component-file (str/format "<%s> " (:name component-file)))
|
2021-01-20 17:12:39 +01:00
|
|
|
(or (:name component-shape) "?")
|
2020-10-05 11:01:14 +02:00
|
|
|
(if (or (:component-root? shape)
|
2020-10-22 16:14:46 +02:00
|
|
|
(nil? (:component-id shape))
|
|
|
|
true)
|
2020-10-05 11:01:14 +02:00
|
|
|
""
|
|
|
|
(let [component-id (:component-id shape)
|
|
|
|
component-file-id (:component-file shape)
|
2020-12-04 15:31:03 +01:00
|
|
|
component-file (when component-file-id (get libraries component-file-id nil))
|
2020-10-05 11:01:14 +02:00
|
|
|
component (if component-file
|
|
|
|
(get-in component-file [:data :components component-id])
|
|
|
|
(get components component-id))]
|
|
|
|
(str/format " (%s%s)"
|
|
|
|
(when component-file (str/format "<%s> " (:name component-file)))
|
|
|
|
(:name component))))))))]
|
2020-09-30 13:46:45 +02:00
|
|
|
|
2020-12-03 14:18:03 +01:00
|
|
|
(println "[Page]")
|
2020-09-30 13:46:45 +02:00
|
|
|
(show-shape (:id root) 0 objects)
|
|
|
|
|
|
|
|
(dorun (for [component (vals components)]
|
|
|
|
(do
|
|
|
|
(println)
|
|
|
|
(println (str/format "[%s]" (:name component)))
|
|
|
|
(show-shape (:id component) 0 (:objects component)))))))))
|
2020-09-28 15:29:54 +02:00
|
|
|
|