2023-12-05 03:57:41 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use loro_internal::{
|
|
|
|
event::Diff,
|
2024-03-08 08:40:06 +00:00
|
|
|
handler::{Handler, ValueOrHandler},
|
2024-03-30 03:38:24 +00:00
|
|
|
ListHandler, LoroDoc, MapHandler, TextHandler, ToJson, TreeHandler,
|
2023-12-05 03:57:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
2024-05-20 22:14:49 +00:00
|
|
|
let doc = LoroDoc::new();
|
2023-12-05 03:57:41 +00:00
|
|
|
doc.start_auto_commit();
|
|
|
|
let list = doc.get_list("list");
|
2024-10-03 07:14:21 +00:00
|
|
|
let _g = doc.subscribe_root(Arc::new(|e| {
|
2024-02-08 06:03:15 +00:00
|
|
|
for container_diff in e.events {
|
|
|
|
match &container_diff.diff {
|
|
|
|
Diff::List(list) => {
|
|
|
|
for item in list.iter() {
|
2024-04-24 05:53:26 +00:00
|
|
|
if let loro_delta::DeltaItem::Replace { value, .. } = item {
|
|
|
|
for v in value.iter() {
|
2024-02-08 06:03:15 +00:00
|
|
|
match v {
|
2024-03-08 08:40:06 +00:00
|
|
|
ValueOrHandler::Handler(h) => {
|
2024-02-08 06:03:15 +00:00
|
|
|
// You can directly obtain the handler and perform some operations.
|
|
|
|
if matches!(h, Handler::Map(_)) {
|
|
|
|
let text = h
|
|
|
|
.as_map()
|
|
|
|
.unwrap()
|
2024-03-30 03:38:24 +00:00
|
|
|
.insert_container(
|
|
|
|
"text",
|
|
|
|
TextHandler::new_detached(),
|
|
|
|
)
|
2024-02-08 06:03:15 +00:00
|
|
|
.unwrap();
|
2024-03-30 03:38:24 +00:00
|
|
|
text.insert(0, "created from event").unwrap();
|
2024-02-08 06:03:15 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-08 08:40:06 +00:00
|
|
|
ValueOrHandler::Value(value) => {
|
2024-02-08 06:03:15 +00:00
|
|
|
println!("insert value {:?}", value);
|
|
|
|
}
|
2023-12-05 03:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 06:03:15 +00:00
|
|
|
Diff::Map(map) => {
|
|
|
|
println!("map container updates {:?}", map.updated);
|
|
|
|
}
|
|
|
|
_ => {}
|
2023-12-05 03:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
list.insert(0, "abc").unwrap();
|
2024-03-30 03:38:24 +00:00
|
|
|
list.insert_container(1, ListHandler::new_detached())
|
|
|
|
.unwrap();
|
|
|
|
list.insert_container(2, MapHandler::new_detached())
|
|
|
|
.unwrap();
|
|
|
|
list.insert_container(3, TextHandler::new_detached())
|
|
|
|
.unwrap();
|
|
|
|
list.insert_container(4, TreeHandler::new_detached())
|
|
|
|
.unwrap();
|
2023-12-05 03:57:41 +00:00
|
|
|
doc.commit_then_renew();
|
|
|
|
assert_eq!(
|
|
|
|
doc.get_deep_value().to_json(),
|
|
|
|
r#"{"list":["abc",[],{"text":"created from event"},"",[]]}"#
|
|
|
|
);
|
|
|
|
}
|