Просмотр исходного кода

enhance(rtc): add more context when apply-ops return :not-found-target-ops

rcmerci 1 месяц назад
Родитель
Сommit
75a1312387

+ 21 - 6
src/main/frontend/worker/rtc/client.cljs

@@ -9,6 +9,7 @@
             [frontend.worker.rtc.client-op :as client-op]
             [frontend.worker.rtc.const :as rtc-const]
             [frontend.worker.rtc.exception :as r.ex]
+            [frontend.worker.rtc.gen-client-op :as gen-client-op]
             [frontend.worker.rtc.log-and-state :as rtc-log-and-state]
             [frontend.worker.rtc.malli-schema :as rtc-schema]
             [frontend.worker.rtc.remote-update :as r.remote-update]
@@ -16,6 +17,7 @@
             [frontend.worker.rtc.throttle :as r.throttle]
             [frontend.worker.rtc.ws :as ws]
             [frontend.worker.rtc.ws-util :as ws-util]
+            [lambdaisland.glogi :as log]
             [logseq.db :as ldb]
             [logseq.db.frontend.schema :as db-schema]
             [missionary.core :as m]
@@ -524,12 +526,25 @@
                   ;; else
                   (do (rollback repo block-ops-map-coll update-kv-value-ops-map-coll rename-db-ident-ops-map-coll)
                       (throw (ex-info "Unavailable1" {:remote-ex remote-ex})))))
-
-            (do (assert (and (pos? (:t r)) (pos? (:t-query-end r))) r)
-                (m/?
-                 (r.remote-update/task--apply-remote-update
-                  graph-uuid repo conn date-formatter {:type :remote-update :value r} aes-key add-log-fn))
-                (add-log-fn :rtc.log/push-local-update {:remote-t (:t r) :remote-t-query-end (:t-query-end r)}))))))))
+            (if-let [not-found-target-ops (seq (:not-found-target-ops r))]
+              (do (rollback repo block-ops-map-coll update-kv-value-ops-map-coll rename-db-ident-ops-map-coll)
+                  ;; add more ents into ops for remote
+                  (let [ents (mapv
+                              (fn [op]
+                                (let [block-uuid (:block-uuid (second op))]
+                                  (assert block-uuid)
+                                  (d/entity @conn [:block/uuid block-uuid])))
+                              not-found-target-ops)
+                        extra-ops (gen-client-op/generate-rtc-ops-from-entities+parents ents)]
+                    (log/error :not-found-target-ops not-found-target-ops
+                               :extra-ops extra-ops)
+                    (client-op/add-ops! repo extra-ops)))
+
+              (do (assert (and (pos? (:t r)) (pos? (:t-query-end r))) r)
+                  (m/?
+                   (r.remote-update/task--apply-remote-update
+                    graph-uuid repo conn date-formatter {:type :remote-update :value r} aes-key add-log-fn))
+                  (add-log-fn :rtc.log/push-local-update {:remote-t (:t r) :remote-t-query-end (:t-query-end r)})))))))))
 
 (defn new-task--pull-remote-data
   [repo conn graph-uuid major-schema-version date-formatter get-ws-create-task aes-key add-log-fn]

+ 22 - 10
src/main/frontend/worker/rtc/gen_client_op.cljs

@@ -146,16 +146,28 @@
 
 (defn- generate-rtc-ops-from-entities
   [ents]
-  (let [db (d/entity-db (first ents))
-        id->same-entity-datoms
-        (into {}
-              (map (fn [ent]
-                     (let [e (:db/id ent)
-                           datoms (d/datoms db :eavt e)]
-                       [e datoms])))
-              ents)
-        e->a->add?->v->t (update-vals id->same-entity-datoms entity-datoms=>a->add?->v->t)]
-    (generate-rtc-ops db db (vals id->same-entity-datoms) e->a->add?->v->t)))
+  (when (seq ents)
+    (let [db (d/entity-db (first ents))
+          id->same-entity-datoms
+          (into {}
+                (map (fn [ent]
+                       (let [e (:db/id ent)
+                             datoms (d/datoms db :eavt e)]
+                         [e datoms])))
+                ents)
+          e->a->add?->v->t (update-vals id->same-entity-datoms entity-datoms=>a->add?->v->t)]
+      (generate-rtc-ops db db (vals id->same-entity-datoms) e->a->add?->v->t))))
+
+(defn generate-rtc-ops-from-entities+parents
+  "generate ents and their parents as rtc-ops"
+  [ents]
+  (let [ents*
+        (set
+         (mapcat
+          (fn [ent]
+            (take 20 (take-while some? (iterate :block/parent ent))))
+          ents))]
+    (generate-rtc-ops-from-entities ents*)))
 
 (defn generate-rtc-ops-from-property-entities
   [property-ents]

+ 3 - 1
src/main/frontend/worker/rtc/malli_schema.cljs

@@ -187,7 +187,9 @@
        [:block/order {:optional true} db-malli-schema/block-order]
        [:block/parent {:optional true} :uuid]
        [::m/default extra-attr-map-schema]]]]]
-   [:failed-ops {:optional true}
+   [:failed-ops {:optional true}        ;DEPRECATED
+    [:maybe [:sequential to-ws-op-schema]]]
+   [:not-found-target-ops {:optional true}
     [:maybe [:sequential to-ws-op-schema]]]])
 
 (def data-from-ws-schema