Skip to content

Commit

Permalink
feat: add from_checkout mark to event (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n authored Nov 8, 2023
1 parent 116190c commit 08e4ed9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
20 changes: 19 additions & 1 deletion crates/loro-internal/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use enum_as_inner::EnumAsInner;
use fxhash::FxHasher64;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;

Expand All @@ -10,7 +11,10 @@ use crate::{
InternalString, LoroValue,
};

use std::borrow::Cow;
use std::{
borrow::Cow,
hash::{Hash, Hasher},
};

use loro_common::{ContainerID, TreeID};

Expand Down Expand Up @@ -43,9 +47,21 @@ pub struct DocDiff {
pub to: Frontiers,
pub origin: InternalString,
pub local: bool,
/// Whether the diff is created from the checkout operation.
pub from_checkout: bool,
pub diff: Vec<ContainerDiff>,
}

impl DocDiff {
/// Get the unique id of the diff.
pub fn id(&self) -> u64 {
let mut hasher = FxHasher64::default();
self.from.hash(&mut hasher);
self.to.hash(&mut hasher);
hasher.finish()
}
}

#[derive(Debug, Clone)]
pub(crate) struct InternalContainerDiff {
pub(crate) idx: ContainerIdx,
Expand All @@ -71,6 +87,7 @@ pub(crate) enum DiffVariant {
pub(crate) struct InternalDocDiff<'a> {
pub(crate) origin: InternalString,
pub(crate) local: bool,
pub(crate) from_checkout: bool,
pub(crate) diff: Cow<'a, [InternalContainerDiff]>,
pub(crate) new_version: Cow<'a, Frontiers>,
}
Expand All @@ -80,6 +97,7 @@ impl<'a> InternalDocDiff<'a> {
InternalDocDiff {
origin: self.origin,
local: self.local,
from_checkout: self.from_checkout,
diff: Cow::Owned((*self.diff).to_owned()),
new_version: Cow::Owned((*self.new_version).to_owned()),
}
Expand Down
2 changes: 2 additions & 0 deletions crates/loro-internal/src/loro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ impl LoroDoc {
origin,
local: false,
diff: (diff).into(),
from_checkout: false,
new_version: Cow::Owned(oplog.frontiers().clone()),
});
}
Expand Down Expand Up @@ -627,6 +628,7 @@ impl LoroDoc {
origin: "checkout".into(),
local: true,
diff: Cow::Owned(diff),
from_checkout: true,
new_version: Cow::Owned(frontiers.clone()),
});
let events = state.take_events();
Expand Down
3 changes: 3 additions & 0 deletions crates/loro-internal/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ impl DocState {
self.record_diff(InternalDocDiff {
origin: Default::default(),
local: false,
from_checkout: false,
diff,
new_version: Cow::Borrowed(&frontiers),
});
Expand Down Expand Up @@ -664,6 +665,7 @@ impl DocState {
let to = (*diffs.last().unwrap().new_version).to_owned();
let origin = diffs[0].origin.clone();
let local = diffs[0].local;
let from_checkout = diffs[0].from_checkout;
for diff in diffs {
#[allow(clippy::unnecessary_to_owned)]
for container_diff in diff.diff.into_owned() {
Expand Down Expand Up @@ -714,6 +716,7 @@ impl DocState {
from,
to,
origin,
from_checkout,
local,
diff,
}
Expand Down
1 change: 1 addition & 0 deletions crates/loro-internal/src/txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ impl Transaction {
})
.collect(),
),
from_checkout: false,
new_version: Cow::Borrowed(oplog.frontiers()),
}),
);
Expand Down
24 changes: 23 additions & 1 deletion crates/loro-internal/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
use std::sync::{Arc, Mutex};
use std::sync::{atomic::AtomicBool, Arc, Mutex};

use loro_common::{ContainerID, ContainerType, LoroValue, ID};
use loro_internal::{
container::richtext::TextStyleInfoFlag, version::Frontiers, ApplyDiff, LoroDoc, ToJson,
};
use serde_json::json;

#[test]
fn event_from_checkout() {
let mut a = LoroDoc::new_auto_commit();
let sub_id = a.subscribe_root(Arc::new(|event| {
assert!(!event.doc.from_checkout);
}));
a.get_text("text").insert_(0, "hello").unwrap();
a.commit_then_renew();
let version = a.oplog_frontiers();
a.get_text("text").insert_(0, "hello").unwrap();
a.commit_then_renew();
a.unsubscribe(sub_id);
let ran = Arc::new(AtomicBool::new(false));
let ran_cloned = ran.clone();
a.subscribe_root(Arc::new(move |event| {
assert!(event.doc.from_checkout);
ran.store(true, std::sync::atomic::Ordering::Relaxed);
}));
a.checkout(&version).unwrap();
assert!(ran_cloned.load(std::sync::atomic::Ordering::Relaxed));
}

#[test]
fn out_of_bound_test() {
let a = LoroDoc::new_auto_commit();
Expand Down

0 comments on commit 08e4ed9

Please sign in to comment.