Skip to content

Commit 208e3df

Browse files
committed
fix(rtc): lost first block's rtc-ops when paste multiple blocks
1 parent c9f1920 commit 208e3df

2 files changed

Lines changed: 87 additions & 8 deletions

File tree

src/main/frontend/worker/rtc/gen_client_op.cljs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,52 @@
66
[logseq.db :as ldb]
77
[logseq.db.frontend.property :as db-property]))
88

9+
(defn remove-conflict-same-block-datoms
10+
"remove conflict entity-datoms for same-block(same block/uuid) in same-entity-datoms-coll.
11+
merge
12+
[[[182 :block/uuid block-uuid1 1 false], ...]
13+
[[183 :block/uuid block-uuid1 1 true], ...]]
14+
into
15+
[[[183 :block/uuid block-uuid1 1 true], ...]]
16+
"
17+
[same-entity-datoms-coll]
18+
(let [entity-info (map (fn [datoms]
19+
(let [first-datom (first datoms)
20+
e (nth first-datom 0)
21+
t (nth first-datom 3)
22+
uuid (some (fn [d]
23+
(when (keyword-identical? :block/uuid (nth d 1))
24+
(nth d 2)))
25+
datoms)]
26+
{:e e :t t :uuid uuid :datoms datoms}))
27+
same-entity-datoms-coll)
28+
uuid-groups (group-by :uuid (filter :uuid entity-info))
29+
loser-eids (reduce
30+
(fn [acc [_uuid infos]]
31+
(let [t-groups (group-by :t infos)]
32+
(reduce
33+
(fn [acc* [_t infos*]]
34+
(if (> (count infos*) 1)
35+
(let [sorted-infos (sort-by :e > infos*)
36+
losers (rest sorted-infos)]
37+
(into acc* (map :e losers)))
38+
acc*))
39+
acc
40+
t-groups)))
41+
#{}
42+
uuid-groups)]
43+
(if (seq loser-eids)
44+
(map :datoms (remove #(contains? loser-eids (:e %)) entity-info))
45+
same-entity-datoms-coll)))
46+
947
(defn group-datoms-by-entity
1048
"Groups transaction datoms by entity and returns a map of entity-id to datoms."
1149
[tx-data]
1250
(let [datom-vec-coll (map vec tx-data)
1351
id->same-entity-datoms (group-by first datom-vec-coll)
1452
id-order (distinct (map first datom-vec-coll))
15-
same-entity-datoms-coll (map id->same-entity-datoms id-order)]
53+
same-entity-datoms-coll (map id->same-entity-datoms id-order)
54+
same-entity-datoms-coll (remove-conflict-same-block-datoms same-entity-datoms-coll)]
1655
{:same-entity-datoms-coll same-entity-datoms-coll
1756
:id->same-entity-datoms id->same-entity-datoms}))
1857

src/test/frontend/worker/rtc/gen_client_op_test.cljs

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@
167167
:block/tags :block/title :db/cardinality}]
168168
#_{:clj-kondo/ignore [:unresolved-symbol :invalid-arity]}
169169
(is (->> (me/find (subject/generate-rtc-ops-from-property-entities [ent])
170-
([:move _ {:block-uuid ?block-uuid}]
171-
[:update-page _ {:block-uuid ?block-uuid}]
172-
[:update _ {:block-uuid ?block-uuid :av-coll ([!av-coll-attrs . _ ...] ...)}])
173-
!av-coll-attrs)
170+
([:move _ {:block-uuid ?block-uuid}]
171+
[:update-page _ {:block-uuid ?block-uuid}]
172+
[:update _ {:block-uuid ?block-uuid :av-coll ([!av-coll-attrs . _ ...] ...)}])
173+
!av-coll-attrs)
174174
set
175175
(set/difference av-coll-attrs)
176176
empty?))))
@@ -183,9 +183,49 @@
183183
:block/tags :block/title}]
184184
#_{:clj-kondo/ignore [:unresolved-symbol :invalid-arity]}
185185
(is (->> (me/find (subject/generate-rtc-ops-from-class-entities [ent])
186-
([:update-page _ {:block-uuid ?block-uuid}]
187-
[:update _ {:block-uuid ?block-uuid :av-coll ([!av-coll-attrs . _ ...] ...)}])
188-
!av-coll-attrs)
186+
([:update-page _ {:block-uuid ?block-uuid}]
187+
[:update _ {:block-uuid ?block-uuid :av-coll ([!av-coll-attrs . _ ...] ...)}])
188+
!av-coll-attrs)
189189
set
190190
(set/difference av-coll-attrs)
191191
empty?))))
192+
193+
(deftest remove-conflict-same-block-datoms-test
194+
(testing "remove conflict entity-datoms for same-block"
195+
(let [block-uuid #uuid "693ec519-e73e-4f2c-b517-7e75ca2c64da"
196+
datoms-182 [[182 :logseq.property/created-by-ref 161 536870976 false]
197+
[182 :block/created-at 1765721369994 536870976 false]
198+
[182 :block/parent 162 536870976 false]
199+
[182 :block/order "aF" 536870976 false]
200+
[182 :block/tx-id 536870972 536870976 false]
201+
[182 :block/page 162 536870976 false]
202+
[182 :block/uuid block-uuid 536870976 false]
203+
[182 :block/title "" 536870976 false]
204+
[182 :block/updated-at 1765721369994 536870976 false]]
205+
datoms-185 [[185 :block/parent 162 536870976 true]
206+
[185 :logseq.property/created-by-ref 161 536870976 true]
207+
[185 :block/title "111" 536870976 true]
208+
[185 :logseq.property.embedding/hnsw-label-updated-at 0 536870976 true]
209+
[185 :block/order "aG" 536870976 true]
210+
[185 :block/page 162 536870976 true]
211+
[185 :block/created-at 1765721370449 536870976 true]
212+
[185 :block/updated-at 1765721370449 536870976 true]
213+
[185 :block/uuid block-uuid 536870976 true]
214+
[185 :block/tx-id 536870976 536870977 true]]
215+
same-entity-datoms-coll [datoms-182 datoms-185]
216+
result (subject/remove-conflict-same-block-datoms same-entity-datoms-coll)]
217+
(is (= 1 (count result)))
218+
(is (= 185 (nth (ffirst result) 0)))
219+
(is (= datoms-185 (first result)))))
220+
221+
(testing "remove conflict entity-datoms should preserve order"
222+
(let [block-uuid1 #uuid "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
223+
block-uuid2 #uuid "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
224+
datoms-1 [[100 :block/uuid block-uuid1 1 true]]
225+
datoms-2 [[101 :block/uuid block-uuid2 2 true]]
226+
datoms-3 [[102 :block/uuid block-uuid2 2 true]] ;; Conflict with datoms-2, wins (higher ID)
227+
same-entity-datoms-coll [datoms-1 datoms-2 datoms-3]
228+
result (subject/remove-conflict-same-block-datoms same-entity-datoms-coll)]
229+
(is (= 2 (count result)))
230+
(is (= datoms-1 (first result)))
231+
(is (= datoms-3 (second result))))))

0 commit comments

Comments
 (0)