🐛 Fix problem when forcing persistence on screen change

This commit is contained in:
alonso.torres 2023-01-05 16:18:48 +01:00
parent c459c56f37
commit 672cfa4ecc

View file

@ -93,29 +93,32 @@
(->> (rx/from-atom commits) (->> (rx/from-atom commits)
(rx/filter (complement empty?)) (rx/filter (complement empty?))
(rx/sample-when (rx/sample-when
(->> (rx/merge (rx/merge
(rx/interval 5000) (rx/filter #(= ::force-persist %) stream)
(rx/filter #(= ::force-persist %) stream) (->> (rx/merge
(->> (rx/from-atom commits) (rx/interval 5000)
(rx/filter (complement empty?)) (->> (rx/from-atom commits)
(rx/debounce 2000))) (rx/filter (complement empty?))
;; Not sample while saving so there are no race conditions (rx/debounce 2000)))
(rx/filter #(not @saving?)))) ;; Not sample while saving so there are no race conditions
(rx/filter #(not @saving?)))))
(rx/tap #(reset! commits [])) (rx/tap #(reset! commits []))
(rx/tap on-saving) (rx/tap on-saving)
(rx/mapcat (fn [changes] (rx/mapcat (fn [changes]
;; NOTE: this is needed for don't start the ;; NOTE: this is needed for don't start the
;; next persistence before this one is ;; next persistence before this one is
;; finished. ;; finished.
(rx/merge (if-let [file-revn (dm/get-in @st/state [:workspace-file :revn])]
(->> (rx/of (persist-changes file-id changes commits)) (rx/merge
(rx/observe-on :async)) (->> (rx/of (persist-changes file-id file-revn changes commits))
(->> stream (rx/observe-on :async))
;; We wait for every change to be persisted (->> stream
(rx/filter (ptk/type? ::shapes-changes-persisted-finished)) ;; We wait for every change to be persisted
(rx/take 1) (rx/filter (ptk/type? ::shapes-changes-persisted-finished))
(rx/tap on-saved) (rx/take 1)
(rx/ignore))))) (rx/tap on-saved)
(rx/ignore)))
(rx/empty))))
(rx/take-until (rx/delay 100 stoper)) (rx/take-until (rx/delay 100 stoper))
(rx/finalize (fn [] (rx/finalize (fn []
(log/debug :hint "finalize persistence: save loop")))) (log/debug :hint "finalize persistence: save loop"))))
@ -132,7 +135,7 @@
(log/debug :hint "finalize persistence: synchronous save loop"))))))))) (log/debug :hint "finalize persistence: synchronous save loop")))))))))
(defn persist-changes (defn persist-changes
[file-id changes pending-commits] [file-id file-revn changes pending-commits]
(log/debug :hint "persist changes" :changes (count changes)) (log/debug :hint "persist changes" :changes (count changes))
(us/verify ::us/uuid file-id) (us/verify ::us/uuid file-id)
(ptk/reify ::persist-changes (ptk/reify ::persist-changes
@ -146,48 +149,46 @@
(features/active-feature? state :components-v2) (features/active-feature? state :components-v2)
(conj "components/v2")) (conj "components/v2"))
sid (:session-id state) sid (:session-id state)
file (get state :workspace-file) params {:id file-id
params {:id (:id file) :revn file-revn
:revn (:revn file)
:session-id sid :session-id sid
:changes-with-metadata (into [] changes) :changes-with-metadata (into [] changes)
:features features}] :features features}]
(when (= file-id (:id params)) (->> (rp/cmd! :update-file params)
(->> (rp/cmd! :update-file params) (rx/mapcat (fn [lagged]
(rx/mapcat (fn [lagged] (log/debug :hint "changes persisted" :lagged (count lagged))
(log/debug :hint "changes persisted" :lagged (count lagged)) (let [frame-updates
(let [frame-updates (-> (group-by :page-id changes)
(-> (group-by :page-id changes) (update-vals #(into #{} (mapcat :frames) %)))
(update-vals #(into #{} (mapcat :frames) %)))
commits commits
(->> @pending-commits (->> @pending-commits
(map #(assoc % :revn (:revn file))))] (map #(assoc % :revn file-revn)))]
(rx/concat (rx/concat
(rx/merge (rx/merge
(->> (rx/from frame-updates) (->> (rx/from frame-updates)
(rx/mapcat (fn [[page-id frames]] (rx/mapcat (fn [[page-id frames]]
(->> frames (map #(vector page-id %))))) (->> frames (map #(vector page-id %)))))
(rx/map (fn [[page-id frame-id]] (dwt/update-thumbnail (:id file) page-id frame-id)))) (rx/map (fn [[page-id frame-id]] (dwt/update-thumbnail file-id page-id frame-id))))
(->> (rx/from (concat lagged commits)) (->> (rx/from (concat lagged commits))
(rx/merge-map (rx/merge-map
(fn [{:keys [changes] :as entry}] (fn [{:keys [changes] :as entry}]
(rx/merge (rx/merge
(rx/from (rx/from
(for [[page-id changes] (group-by :page-id changes)] (for [[page-id changes] (group-by :page-id changes)]
(dch/update-indices page-id changes))) (dch/update-indices page-id changes)))
(rx/of (shapes-changes-persisted file-id entry))))))) (rx/of (shapes-changes-persisted file-id entry)))))))
(rx/of (shapes-changes-persisted-finished)))))) (rx/of (shapes-changes-persisted-finished))))))
(rx/catch (fn [cause] (rx/catch (fn [cause]
(rx/concat (rx/concat
(if (= :authentication (:type cause)) (if (= :authentication (:type cause))
(rx/empty) (rx/empty)
(rx/of (rt/assign-exception cause))) (rx/of (rt/assign-exception cause)))
(rx/throw cause)))))))))) (rx/throw cause)))))))))
;; Event to be thrown after the changes have been persisted ;; Event to be thrown after the changes have been persisted
(defn shapes-changes-persisted-finished (defn shapes-changes-persisted-finished