mirror of
https://github.com/penpot/penpot.git
synced 2025-05-04 19:25:55 +02:00
✨ Don't put timeout on tokens changes transaction
This commit is contained in:
parent
f7f1598e71
commit
22f789e77c
2 changed files with 24 additions and 25 deletions
|
@ -22,7 +22,8 @@
|
||||||
;; Change this to :info :debug or :trace to debug this module
|
;; Change this to :info :debug or :trace to debug this module
|
||||||
(log/set-level! :warn)
|
(log/set-level! :warn)
|
||||||
|
|
||||||
(def discard-transaction-time-millis (* 20 1000))
|
(def ^:private
|
||||||
|
discard-transaction-time-millis (* 20 1000))
|
||||||
|
|
||||||
(def ^:private
|
(def ^:private
|
||||||
schema:undo-entry
|
schema:undo-entry
|
||||||
|
@ -30,7 +31,7 @@
|
||||||
[:undo-changes [:vector ::cpc/change]]
|
[:undo-changes [:vector ::cpc/change]]
|
||||||
[:redo-changes [:vector ::cpc/change]]])
|
[:redo-changes [:vector ::cpc/change]]])
|
||||||
|
|
||||||
(def check-undo-entry!
|
(def check-undo-entry
|
||||||
(sm/check-fn schema:undo-entry))
|
(sm/check-fn schema:undo-entry))
|
||||||
|
|
||||||
(def MAX-UNDO-SIZE 50)
|
(def MAX-UNDO-SIZE 50)
|
||||||
|
@ -48,8 +49,7 @@
|
||||||
(ptk/reify ::materialize-undo
|
(ptk/reify ::materialize-undo
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
(-> state
|
(update state :workspace-undo assoc :index index))))
|
||||||
(assoc-in [:workspace-undo :index] index)))))
|
|
||||||
|
|
||||||
(defn- add-undo-entry
|
(defn- add-undo-entry
|
||||||
[state entry]
|
[state entry]
|
||||||
|
@ -88,12 +88,9 @@
|
||||||
|
|
||||||
(defn append-undo
|
(defn append-undo
|
||||||
[entry stack?]
|
[entry stack?]
|
||||||
(dm/assert!
|
|
||||||
"expected valid undo entry"
|
|
||||||
(check-undo-entry! entry))
|
|
||||||
|
|
||||||
(dm/assert!
|
(assert (check-undo-entry entry))
|
||||||
(boolean? stack?))
|
(assert (boolean? stack?))
|
||||||
|
|
||||||
(ptk/reify ::append-undo
|
(ptk/reify ::append-undo
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
|
@ -118,17 +115,11 @@
|
||||||
|
|
||||||
(defn start-undo-transaction
|
(defn start-undo-transaction
|
||||||
"Start a transaction, so that every changes inside are added together in a single undo entry."
|
"Start a transaction, so that every changes inside are added together in a single undo entry."
|
||||||
[id]
|
[id & {:keys [timeout] :or {timeout discard-transaction-time-millis}}]
|
||||||
(ptk/reify ::start-undo-transaction
|
(ptk/reify ::start-undo-transaction
|
||||||
ptk/WatchEvent
|
|
||||||
(watch [_ _ _]
|
|
||||||
(->> (rx/of (check-open-transactions))
|
|
||||||
;; Wait the configured time
|
|
||||||
(rx/delay discard-transaction-time-millis)))
|
|
||||||
|
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
(log/info :msg "start-undo-transaction")
|
(log/info :hint "start-undo-transaction")
|
||||||
;; We commit the old transaction before starting the new one
|
;; We commit the old transaction before starting the new one
|
||||||
(let [current-tx (get-in state [:workspace-undo :transaction])
|
(let [current-tx (get-in state [:workspace-undo :transaction])
|
||||||
pending-tx (get-in state [:workspace-undo :transactions-pending])]
|
pending-tx (get-in state [:workspace-undo :transactions-pending])]
|
||||||
|
@ -136,20 +127,28 @@
|
||||||
(nil? current-tx) (assoc-in [:workspace-undo :transaction] empty-tx)
|
(nil? current-tx) (assoc-in [:workspace-undo :transaction] empty-tx)
|
||||||
(nil? pending-tx) (assoc-in [:workspace-undo :transactions-pending] #{id})
|
(nil? pending-tx) (assoc-in [:workspace-undo :transactions-pending] #{id})
|
||||||
(some? pending-tx) (update-in [:workspace-undo :transactions-pending] conj id)
|
(some? pending-tx) (update-in [:workspace-undo :transactions-pending] conj id)
|
||||||
:always (update-in [:workspace-undo :transactions-pending-ts] assoc id (dt/now)))))))
|
:always (update-in [:workspace-undo :transactions-pending-ts] assoc id (dt/now)))))
|
||||||
|
|
||||||
|
ptk/WatchEvent
|
||||||
|
(watch [_ _ _]
|
||||||
|
(when (and timeout (pos? timeout))
|
||||||
|
(->> (rx/of (check-open-transactions timeout))
|
||||||
|
;; Wait the configured time
|
||||||
|
(rx/delay timeout))))))
|
||||||
|
|
||||||
|
|
||||||
(defn discard-undo-transaction []
|
(defn discard-undo-transaction []
|
||||||
(ptk/reify ::discard-undo-transaction
|
(ptk/reify ::discard-undo-transaction
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
(log/info :msg "discard-undo-transaction")
|
(log/info :hint "discard-undo-transaction")
|
||||||
(update state :workspace-undo dissoc :transaction :transactions-pending :transactions-pending-ts))))
|
(update state :workspace-undo dissoc :transaction :transactions-pending :transactions-pending-ts))))
|
||||||
|
|
||||||
(defn commit-undo-transaction [id]
|
(defn commit-undo-transaction [id]
|
||||||
(ptk/reify ::commit-undo-transaction
|
(ptk/reify ::commit-undo-transaction
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
(log/info :msg "commit-undo-transaction")
|
(log/info :hint "commit-undo-transaction")
|
||||||
(let [state (-> state
|
(let [state (-> state
|
||||||
(update-in [:workspace-undo :transactions-pending] disj id)
|
(update-in [:workspace-undo :transactions-pending] disj id)
|
||||||
(update-in [:workspace-undo :transactions-pending-ts] dissoc id))]
|
(update-in [:workspace-undo :transactions-pending-ts] dissoc id))]
|
||||||
|
@ -166,15 +165,15 @@
|
||||||
(assoc state :workspace-undo {}))))
|
(assoc state :workspace-undo {}))))
|
||||||
|
|
||||||
(defn check-open-transactions
|
(defn check-open-transactions
|
||||||
[]
|
[timeout]
|
||||||
(ptk/reify ::check-open-transactions
|
(ptk/reify ::check-open-transactions
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
(watch [_ state _]
|
(watch [_ state _]
|
||||||
(log/info :msg "check-open-transactions")
|
(log/info :hint "check-open-transactions" :timeout timeout)
|
||||||
(let [pending-ts (-> (dm/get-in state [:workspace-undo :transactions-pending-ts])
|
(let [pending-ts (-> (dm/get-in state [:workspace-undo :transactions-pending-ts])
|
||||||
(update-vals #(.toMillis (dt/diff (dt/now) %))))]
|
(update-vals #(inst-ms (dt/diff (dt/now) %))))]
|
||||||
(->> pending-ts
|
(->> pending-ts
|
||||||
(filter (fn [[_ ts]] (>= ts discard-transaction-time-millis)))
|
(filter (fn [[_ ts]] (>= ts timeout)))
|
||||||
(rx/from)
|
(rx/from)
|
||||||
(rx/tap #(js/console.warn (dm/str "FORCE COMMIT TRANSACTION AFTER " (second %) "MS")))
|
(rx/tap #(js/console.warn (dm/str "FORCE COMMIT TRANSACTION AFTER " (second %) "MS")))
|
||||||
(rx/map first)
|
(rx/map first)
|
||||||
|
|
|
@ -164,6 +164,6 @@
|
||||||
(rx/mapcat (fn [sd-tokens]
|
(rx/mapcat (fn [sd-tokens]
|
||||||
(let [undo-id (js/Symbol)]
|
(let [undo-id (js/Symbol)]
|
||||||
(rx/concat
|
(rx/concat
|
||||||
(rx/of (dwu/start-undo-transaction undo-id))
|
(rx/of (dwu/start-undo-transaction undo-id :timeout false))
|
||||||
(update-tokens state sd-tokens)
|
(update-tokens state sd-tokens)
|
||||||
(rx/of (dwu/commit-undo-transaction undo-id)))))))))))
|
(rx/of (dwu/commit-undo-transaction undo-id)))))))))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue