0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-23 23:35:58 -05:00
penpot/frontend/src/app/main/store.cljs

75 lines
1.8 KiB
Text
Raw Normal View History

;; 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) 2020 UXBOX Labs SL
(ns app.main.store
(:require
[beicon.core :as rx]
[okulary.core :as l]
[potok.core :as ptk]
[app.common.uuid :as uuid]
[app.util.storage :refer [storage]]
[app.util.debug :refer [debug? logjs]]))
(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}))
(defonce stream (ptk/input-stream store))
(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))))
(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)) $)
(rx/subscribe $ (fn [event]
(println "[stream]: " (repr-event event)))))))
(defn emit!
2020-04-02 17:08:24 +02:00
([] nil)
([event]
(ptk/emit! store event)
nil)
([event & events]
(apply ptk/emit! store (cons event events))
nil))
(defn emitf
[& events]
#(apply ptk/emit! store events))
2019-07-01 19:40:01 +02:00
(def initial-state
{:session-id (uuid/next)
:profile (:profile storage)})
(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]))))