2022-11-24 15:28:36 +00:00
|
|
|
use enum_as_inner::EnumAsInner;
|
2022-11-23 08:26:38 +00:00
|
|
|
use fxhash::{FxHashMap, FxHashSet};
|
2022-11-29 10:31:57 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-11-23 08:26:38 +00:00
|
|
|
|
2022-11-23 17:01:40 +00:00
|
|
|
use crate::{container::ContainerID, delta::Delta, version::Frontiers, InternalString, LoroValue};
|
|
|
|
|
2022-11-24 07:10:21 +00:00
|
|
|
#[derive(Debug)]
|
2022-11-23 17:01:40 +00:00
|
|
|
pub struct RawEvent {
|
|
|
|
pub container_id: ContainerID,
|
|
|
|
pub old_version: Frontiers,
|
|
|
|
pub new_version: Frontiers,
|
2022-11-24 04:15:25 +00:00
|
|
|
pub local: bool,
|
2022-11-23 17:01:40 +00:00
|
|
|
pub diff: Vec<Diff>,
|
2022-12-08 10:52:43 +00:00
|
|
|
pub abs_path: Path,
|
2022-11-23 08:26:38 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 13:40:16 +00:00
|
|
|
#[derive(Debug, Serialize)]
|
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,
|
2022-11-23 08:26:38 +00:00
|
|
|
pub diff: Vec<Diff>,
|
2022-11-24 04:15:25 +00:00
|
|
|
pub local: bool,
|
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>,
|
|
|
|
}
|
|
|
|
|
2022-11-23 08:26:38 +00:00
|
|
|
pub type Path = Vec<Index>;
|
|
|
|
|
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-01-11 13:40:16 +00:00
|
|
|
#[derive(Clone, Debug, EnumAsInner, Serialize)]
|
2022-11-23 08:26:38 +00:00
|
|
|
pub enum Diff {
|
|
|
|
List(Delta<Vec<LoroValue>>),
|
|
|
|
Text(Delta<String>),
|
|
|
|
Map(MapDiff),
|
|
|
|
}
|
|
|
|
|
2023-01-11 13:40:16 +00:00
|
|
|
#[derive(Clone, Debug, Serialize)]
|
2022-11-23 08:26:38 +00:00
|
|
|
pub struct ValuePair {
|
|
|
|
pub old: LoroValue,
|
|
|
|
pub new: LoroValue,
|
|
|
|
}
|
|
|
|
|
2022-11-24 08:21:12 +00:00
|
|
|
impl From<(LoroValue, LoroValue)> for ValuePair {
|
|
|
|
fn from((old, new): (LoroValue, LoroValue)) -> Self {
|
|
|
|
ValuePair { old, new }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 13:40:16 +00:00
|
|
|
#[derive(Default, Clone, Debug, Serialize)]
|
2022-11-23 08:26:38 +00:00
|
|
|
pub struct MapDiff {
|
|
|
|
pub added: FxHashMap<InternalString, LoroValue>,
|
|
|
|
pub updated: FxHashMap<InternalString, ValuePair>,
|
|
|
|
pub deleted: FxHashSet<InternalString>,
|
|
|
|
}
|
2022-11-23 10:12:23 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type ObserverHandler = Box<dyn FnMut(&Event) + Send>;
|
|
|
|
|
|
|
|
pub struct Observer {
|
|
|
|
handler: ObserverHandler,
|
|
|
|
options: ObserverOptions,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Observer {
|
|
|
|
pub fn new(
|
|
|
|
handler: ObserverHandler,
|
|
|
|
container: Option<ContainerID>,
|
|
|
|
once: bool,
|
|
|
|
deep: bool,
|
|
|
|
) -> Self {
|
|
|
|
let options = ObserverOptions {
|
|
|
|
container,
|
|
|
|
once,
|
|
|
|
deep,
|
|
|
|
};
|
|
|
|
Self { handler, options }
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn call(&mut self, event: &Event) {
|
|
|
|
(self.handler)(event)
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 04:15:25 +00:00
|
|
|
|
2022-12-07 18:25:13 +00:00
|
|
|
pub type SubscriptionID = u32;
|