mirror of
https://github.com/loro-dev/loro.git
synced 2025-01-22 04:45:46 +00:00
Fix-better-event-order (#595)
The event will now be ordered by (depth, container counter). Therefore, two container creation events within the same layer will be sorted based on the containers’ counter values. This approach can prevent the issue where child tree node events are received before the parent tree node events
This commit is contained in:
parent
d552955ec6
commit
df81aece51
3 changed files with 32 additions and 1 deletions
5
.changeset/tricky-bees-peel.md
Normal file
5
.changeset/tricky-bees-peel.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"loro-crdt": patch
|
||||
---
|
||||
|
||||
Better event ordering
|
|
@ -1302,7 +1302,16 @@ impl DocState {
|
|||
|
||||
// Sort by path length, so caller can apply the diff from the root to the leaf.
|
||||
// Otherwise, the caller may use a wrong path to apply the diff.
|
||||
diff.sort_by_key(|x| x.path.len());
|
||||
|
||||
diff.sort_by_key(|x| {
|
||||
(
|
||||
x.path.len(),
|
||||
match &x.id {
|
||||
ContainerID::Root { .. } => 0,
|
||||
ContainerID::Normal { counter, .. } => *counter + 1,
|
||||
},
|
||||
)
|
||||
});
|
||||
DocDiff {
|
||||
from,
|
||||
to,
|
||||
|
|
|
@ -2350,6 +2350,23 @@ fn test_detach_and_attach() {
|
|||
assert!(!doc.is_detached());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_event_order() {
|
||||
let doc = LoroDoc::new();
|
||||
let _sub = doc.subscribe_root(Arc::new(|e| {
|
||||
let e0 = &e.events[0].diff;
|
||||
assert!(e0.is_map());
|
||||
let e1 = &e.events[1].diff;
|
||||
assert!(e1.is_list());
|
||||
let e2 = &e.events[2].diff;
|
||||
assert!(e2.is_tree());
|
||||
}));
|
||||
doc.get_map("map").insert("key", "value").unwrap();
|
||||
doc.get_list("list").insert(0, "item").unwrap();
|
||||
doc.get_tree("tree").create(None).unwrap();
|
||||
doc.commit();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rust_get_value_by_path() {
|
||||
let doc = LoroDoc::new();
|
||||
|
|
Loading…
Reference in a new issue