batch_tx.cljs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. (ns frontend.worker.batch-tx
  2. "Batch process multiple transactions.
  3. When batch-processing, don't refresh ui."
  4. (:require [frontend.worker.state :as worker-state]
  5. [frontend.schema-register :include-macros true :as sr]))
  6. (sr/defkeyword :batch/txs
  7. "store all tx-data when batch-processing")
  8. (sr/defkeyword :batch/db-before
  9. "store db before batch-tx.
  10. It can be used to judge if it is batch-processing.")
  11. (sr/defkeyword :batch/opts
  12. "Opts for with-batch-tx-mode")
  13. (defn get-batch-txs
  14. []
  15. (->> (:batch/txs @worker-state/*state)
  16. (sort-by :tx)))
  17. (defn set-batch-db-before!
  18. [db]
  19. (swap! worker-state/*state assoc :batch/db-before db))
  20. (defn get-batch-db-before
  21. []
  22. (:batch/db-before @worker-state/*state))
  23. (defn set-batch-opts
  24. [opts]
  25. (swap! worker-state/*state assoc :batch/opts opts))
  26. (defn get-batch-opts
  27. []
  28. (:batch/opts @worker-state/*state))
  29. (defn conj-batch-txs!
  30. [tx-data]
  31. (swap! worker-state/*state update :batch/txs (fn [data] (into data tx-data))))
  32. (defn exit-batch-txs-mode!
  33. []
  34. (swap! worker-state/*state assoc :batch/txs nil)
  35. (swap! worker-state/*state assoc :batch/db-before nil)
  36. (swap! worker-state/*state assoc :batch/opts nil))