feat: add from_checkout mark to event (#164)

This commit is contained in:
Zixuan Chen 2023-11-08 16:15:11 +08:00 committed by GitHub
parent 116190c817
commit 08e4ed9d40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 2 deletions

View file

@ -1,4 +1,5 @@
use enum_as_inner::EnumAsInner;
use fxhash::FxHasher64;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
@ -10,7 +11,10 @@ use crate::{
InternalString, LoroValue,
};
use std::borrow::Cow;
use std::{
borrow::Cow,
hash::{Hash, Hasher},
};
use loro_common::{ContainerID, TreeID};
@ -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,
@ -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>,
}
@ -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()),
}

View file

@ -404,6 +404,7 @@ impl LoroDoc {
origin,
local: false,
diff: (diff).into(),
from_checkout: false,
new_version: Cow::Owned(oplog.frontiers().clone()),
});
}
@ -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();

View file

@ -429,6 +429,7 @@ impl DocState {
self.record_diff(InternalDocDiff {
origin: Default::default(),
local: false,
from_checkout: false,
diff,
new_version: Cow::Borrowed(&frontiers),
});
@ -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() {
@ -714,6 +716,7 @@ impl DocState {
from,
to,
origin,
from_checkout,
local,
diff,
}

View file

@ -321,6 +321,7 @@ impl Transaction {
})
.collect(),
),
from_checkout: false,
new_version: Cow::Borrowed(oplog.frontiers()),
}),
);

View file

@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex};
use std::sync::{atomic::AtomicBool, Arc, Mutex};
use loro_common::{ContainerID, ContainerType, LoroValue, ID};
use loro_internal::{
@ -6,6 +6,28 @@ use loro_internal::{
};
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();