2022-11-24 15:28:36 +00:00
|
|
|
use enum_as_inner::EnumAsInner;
|
2022-11-29 10:31:57 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-03-10 02:50:05 +00:00
|
|
|
use smallvec::SmallVec;
|
2022-11-23 08:26:38 +00:00
|
|
|
|
2023-03-01 13:37:58 +00:00
|
|
|
use crate::{
|
2023-03-05 09:38:26 +00:00
|
|
|
container::ContainerID,
|
2023-07-04 06:31:50 +00:00
|
|
|
delta::{Delta, DeltaType, MapDelta, MapDiff, Meta},
|
2023-07-10 04:06:11 +00:00
|
|
|
text::text_content::SliceRanges,
|
2023-03-20 13:26:36 +00:00
|
|
|
transaction::Origin,
|
2023-03-05 09:38:26 +00:00
|
|
|
version::Frontiers,
|
|
|
|
InternalString, LoroValue,
|
2023-03-01 13:37:58 +00:00
|
|
|
};
|
2022-11-23 17:01:40 +00:00
|
|
|
|
2023-04-04 13:44:41 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct EventDiff {
|
|
|
|
pub id: ContainerID,
|
|
|
|
pub diff: SmallVec<[Diff; 1]>,
|
|
|
|
pub local: bool,
|
|
|
|
}
|
|
|
|
|
2022-11-24 07:10:21 +00:00
|
|
|
#[derive(Debug)]
|
2023-03-21 02:50:18 +00:00
|
|
|
pub(crate) struct RawEvent {
|
2022-11-23 17:01:40 +00:00
|
|
|
pub container_id: ContainerID,
|
|
|
|
pub old_version: Frontiers,
|
|
|
|
pub new_version: Frontiers,
|
2022-11-24 04:15:25 +00:00
|
|
|
pub local: bool,
|
2023-04-04 13:44:41 +00:00
|
|
|
pub diff: Diff,
|
2022-12-08 10:52:43 +00:00
|
|
|
pub abs_path: Path,
|
2023-03-20 13:26:36 +00:00
|
|
|
pub origin: Option<Origin>,
|
2022-11-23 08:26:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 11:09:13 +00:00
|
|
|
#[derive(Debug, Serialize, Clone)]
|
2022-11-23 08:26:38 +00:00
|
|
|
pub struct Event {
|
|
|
|
pub old_version: Frontiers,
|
|
|
|
pub new_version: Frontiers,
|
2022-11-24 15:28:36 +00:00
|
|
|
pub current_target: Option<ContainerID>,
|
2022-11-23 08:26:38 +00:00
|
|
|
pub target: ContainerID,
|
|
|
|
/// the relative path from current_target to target
|
|
|
|
pub relative_path: Path,
|
2022-11-24 15:28:36 +00:00
|
|
|
pub absolute_path: Path,
|
2023-04-04 13:44:41 +00:00
|
|
|
pub diff: Diff,
|
2022-11-24 04:15:25 +00:00
|
|
|
pub local: bool,
|
2023-03-20 13:26:36 +00:00
|
|
|
pub origin: Option<Origin>,
|
2022-11-23 08:26:38 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:50:23 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct PathAndTarget {
|
|
|
|
pub relative_path: Path,
|
|
|
|
pub target: Option<ContainerID>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub(crate) struct EventDispatch {
|
|
|
|
pub sub_ids: Vec<SubscriptionID>,
|
|
|
|
pub rewrite: Option<PathAndTarget>,
|
|
|
|
}
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
pub type Path = SmallVec<[Index; 4]>;
|
2022-11-23 08:26:38 +00:00
|
|
|
|
2022-11-29 10:31:57 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
2022-11-23 08:26:38 +00:00
|
|
|
pub enum Index {
|
|
|
|
Key(InternalString),
|
2022-11-23 10:12:23 +00:00
|
|
|
Seq(usize),
|
2022-11-23 08:26:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:03:07 +00:00
|
|
|
#[repr(transparent)]
|
|
|
|
#[derive(Default, Clone, Copy, Debug, Serialize, PartialEq)]
|
|
|
|
pub struct Utf16Meta {
|
|
|
|
pub utf16_len: Option<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Meta for Utf16Meta {
|
|
|
|
fn empty() -> Self {
|
|
|
|
Utf16Meta { utf16_len: None }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.utf16_len.is_none()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compose(&mut self, _: &Self, _: (DeltaType, DeltaType)) {}
|
|
|
|
|
|
|
|
fn take(&mut self, other: &Self) -> Self {
|
|
|
|
if let Some(utf16_len) = &mut self.utf16_len {
|
|
|
|
let other_len = other.utf16_len.unwrap_or(0);
|
|
|
|
debug_assert!(
|
|
|
|
other_len <= *utf16_len,
|
|
|
|
"other_len: {}, utf16_len: {}",
|
|
|
|
other_len,
|
|
|
|
utf16_len
|
|
|
|
);
|
|
|
|
*utf16_len -= other_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
*other
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_mergeable(&self, _: &Self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge(&mut self, other: &Self) {
|
|
|
|
match (&mut self.utf16_len, &other.utf16_len) {
|
|
|
|
(Some(a), Some(b)) => {
|
|
|
|
*a += *b;
|
|
|
|
}
|
|
|
|
(a, _) => {
|
|
|
|
*a = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Utf16Meta {
|
|
|
|
pub(crate) fn new(utf16_len: usize) -> Self {
|
|
|
|
Utf16Meta {
|
|
|
|
utf16_len: Some(utf16_len),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 13:40:16 +00:00
|
|
|
#[derive(Clone, Debug, EnumAsInner, Serialize)]
|
2022-11-23 08:26:38 +00:00
|
|
|
pub enum Diff {
|
2023-03-05 09:38:26 +00:00
|
|
|
List(Delta<Vec<LoroValue>>),
|
2023-07-07 13:54:47 +00:00
|
|
|
SeqRaw(Delta<SliceRanges>),
|
2023-04-03 12:03:07 +00:00
|
|
|
Text(Delta<String, Utf16Meta>),
|
2023-07-07 11:12:41 +00:00
|
|
|
/// @deprecated
|
2023-03-10 02:50:05 +00:00
|
|
|
Map(MapDiff<LoroValue>),
|
2023-07-04 06:31:50 +00:00
|
|
|
NewMap(MapDelta),
|
2022-11-23 08:26:38 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 11:02:22 +00:00
|
|
|
impl Diff {
|
|
|
|
pub(crate) fn compose(self, diff: Diff) -> Result<Diff, Self> {
|
|
|
|
// PERF: avoid clone
|
|
|
|
match (self, diff) {
|
|
|
|
(Diff::List(a), Diff::List(b)) => Ok(Diff::List(a.compose(b))),
|
|
|
|
(Diff::SeqRaw(a), Diff::SeqRaw(b)) => Ok(Diff::SeqRaw(a.compose(b))),
|
|
|
|
(Diff::Text(a), Diff::Text(b)) => Ok(Diff::Text(a.compose(b))),
|
|
|
|
(Diff::Map(a), Diff::Map(b)) => Ok(Diff::Map(a.compose(b))),
|
|
|
|
(Diff::NewMap(a), Diff::NewMap(b)) => Ok(Diff::NewMap(a.compose(b))),
|
|
|
|
(a, _) => Err(a),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Diff {
|
|
|
|
fn default() -> Self {
|
|
|
|
Diff::List(Delta::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 09:26:16 +00:00
|
|
|
// pub type Observer = Box<dyn FnMut(&Event) + Send>;
|
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct ObserverOptions {
|
|
|
|
pub(crate) once: bool,
|
|
|
|
pub(crate) container: Option<ContainerID>,
|
|
|
|
pub(crate) deep: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObserverOptions {
|
|
|
|
fn with_container(mut self, container: ContainerID) -> Self {
|
|
|
|
self.container.replace(container);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
pub type ObserverHandler = Box<dyn FnMut(&Event) + Send>;
|
2023-01-31 09:26:16 +00:00
|
|
|
|
2023-03-21 02:50:18 +00:00
|
|
|
pub(crate) struct Observer {
|
2023-01-31 09:26:16 +00:00
|
|
|
handler: ObserverHandler,
|
|
|
|
options: ObserverOptions,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Observer {
|
|
|
|
pub fn new_root(handler: ObserverHandler) -> Self {
|
|
|
|
Self {
|
|
|
|
handler,
|
|
|
|
options: ObserverOptions::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_container(handler: ObserverHandler, container: ContainerID) -> Self {
|
|
|
|
Self {
|
|
|
|
handler,
|
|
|
|
options: ObserverOptions::default().with_container(container),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn container(&self) -> &Option<ContainerID> {
|
|
|
|
&self.options.container
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn root(&self) -> bool {
|
|
|
|
self.options.container.is_none()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deep(&self) -> bool {
|
|
|
|
self.options.deep
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_once(mut self, once: bool) -> Self {
|
|
|
|
self.options.once = once;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_deep(mut self, deep: bool) -> Self {
|
|
|
|
self.options.deep = deep;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn once(&self) -> bool {
|
|
|
|
self.options.once
|
|
|
|
}
|
|
|
|
|
2023-04-03 01:29:25 +00:00
|
|
|
pub fn call(&mut self, event: &Event) {
|
|
|
|
(self.handler)(event)
|
2023-01-31 09:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-24 04:15:25 +00:00
|
|
|
|
2022-12-07 18:25:13 +00:00
|
|
|
pub type SubscriptionID = u32;
|