penpot/frontend/src/app/main/store.cljs
Alejandro Alonso 0e0fb68c38 🎉 Add assets exportation in bulk (multiple)
And adapt to the websocket changes on backend and
exporter.
2022-03-22 11:34:32 +01:00

70 lines
2.0 KiB
Clojure

;; 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) UXBOX Labs SL
(ns app.main.store
(:require-macros [app.main.store])
(:require
[app.util.object :as obj]
[beicon.core :as rx]
[okulary.core :as l]
[potok.core :as ptk]))
(enable-console-print!)
(defonce loader (l/atom false))
(defonce on-error (l/atom identity))
(defonce state
(ptk/store {:resolve ptk/resolve
:on-error (fn [e] (@on-error e))}))
(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!
([] nil)
([event]
(ptk/emit! state event)
nil)
([event & events]
(apply ptk/emit! state (cons event events))
nil))
(defn emitf
[& events]
#(apply ptk/emit! state events))
(defonce ongoing-tasks (l/atom #{}))
(add-watch ongoing-tasks ::ongoing-tasks
(fn [_ _ _ events]
(if (empty? events)
(obj/set! js/window "onbeforeunload" nil)
(obj/set! js/window "onbeforeunload" (constantly false)))))