loro/crates/loro-internal/src/event.rs

159 lines
3.5 KiB
Rust
Raw Normal View History

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};
#[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,
pub local: bool,
2022-11-23 17:01:40 +00:00
pub diff: Vec<Diff>,
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,
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,
pub absolute_path: Path,
2022-11-23 08:26:38 +00:00
pub diff: Vec<Diff>,
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,
}
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
// 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-12-07 18:25:13 +00:00
pub type SubscriptionID = u32;