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

96 lines
2.9 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/.
;;
2022-09-20 23:23:22 +02:00
;; Copyright (c) KALEIDOS INC
(ns app.main.store
(:require
[app.common.logging :as log]
[app.util.object :as obj]
[beicon.core :as rx]
[okulary.core :as l]
[potok.core :as ptk]))
(log/set-level! :info)
(enable-console-print!)
2020-04-14 17:00:52 +02:00
(defonce loader (l/atom false))
(defonce on-error (l/atom identity))
(defmethod ptk/resolve :default
[type data]
(ptk/data-event type data))
(def on-event identity)
(def ^:dynamic *debug-events* false)
;; Only created in development build
(when *assert*
(def debug-exclude-events
#{:app.main.data.workspace.notifications/handle-pointer-update
:app.main.data.workspace.notifications/handle-pointer-send
:app.main.data.workspace.persistence/update-persistence-status
:app.main.data.workspace.changes/update-indices
:app.main.data.websocket/send-message
:app.main.data.workspace.selection/change-hover-state})
(set! on-event (fn [e]
(when (and *debug-events*
(ptk/event? e)
(not (debug-exclude-events (ptk/type e))))
(.log js/console (str "[stream]: " (ptk/repr-event e)) )))))
2022-12-30 16:08:27 +01:00
(defonce state
(ptk/store {:resolve ptk/resolve
:on-event on-event
:on-error (fn [cause]
(when cause
#_(log/error :hint "unexpected exception on store" :cause cause)
(@on-error cause)))}))
(defonce stream
(ptk/input-stream state))
(defonce last-events
(let [buffer (atom [])
allowed #{:app.main.data.workspace/initialize-page
:app.main.data.workspace/finalize-page
:app.main.data.workspace/initialize-file
:app.main.data.workspace/finalize-file}]
(->> (rx/merge
(->> stream
(rx/filter (ptk/type? :app.main.data.workspace.changes/commit-changes))
(rx/map #(-> % deref :hint-origin str))
(rx/dedupe))
(->> stream
(rx/map ptk/type)
(rx/filter #(contains? allowed %))
(rx/map str)))
(rx/scan (fn [buffer event]
(cond-> (conj buffer event)
(> (count buffer) 20)
(pop)))
#queue [])
(rx/subs #(reset! buffer (vec %))))
buffer))
(defn emit!
2020-04-02 17:08:24 +02:00
([] nil)
([event]
2020-12-21 09:47:50 +01:00
(ptk/emit! state event)
nil)
([event & events]
2020-12-21 09:47:50 +01:00
(apply ptk/emit! state (cons event events))
nil))
(defonce ongoing-tasks (l/atom #{}))
2020-09-28 15:29:54 +02:00
(add-watch ongoing-tasks ::ongoing-tasks
(fn [_ _ _ events]
(if (empty? events)
(obj/set! js/window "onbeforeunload" nil)
(obj/set! js/window "onbeforeunload" (constantly false)))))