diff --git a/src/api/change.rs b/src/api/change.rs index fc0a01d..1894c18 100644 --- a/src/api/change.rs +++ b/src/api/change.rs @@ -62,7 +62,7 @@ impl TextChange { !self.span.is_empty() } - // returns true if this TextChange adds new text + /// returns true if this TextChange adds new text pub fn is_addition(&self) -> bool { !self.content.is_empty() } @@ -72,6 +72,7 @@ impl TextChange { !self.is_deletion() && !self.is_addition() } + /// applies this text change to given text, returning a new string pub fn apply(&self, txt: &str) -> String { let pre_index = std::cmp::min(self.span.start, txt.len()); let pre = txt.get(..pre_index).unwrap_or("").to_string(); diff --git a/src/api/controller.rs b/src/api/controller.rs index 5866f6b..465a9f7 100644 --- a/src/api/controller.rs +++ b/src/api/controller.rs @@ -57,9 +57,6 @@ pub trait Controller : Sized + Send + Sync { async fn poll(&self) -> Result<()>; /// attempt to receive a value without blocking, return None if nothing is available - /// - /// note that this function does not circumvent race conditions, returning errors if it would - /// block. it's usually safe to ignore such errors and retry fn try_recv(&self) -> Result>; /// sync variant of [Self::recv], blocking invoking thread diff --git a/src/buffer/controller.rs b/src/buffer/controller.rs index b1ceb57..da47efa 100644 --- a/src/buffer/controller.rs +++ b/src/buffer/controller.rs @@ -16,12 +16,6 @@ use crate::api::TextChange; /// the buffer controller implementation /// -/// this contains -/// * a watch channel which always contains an updated view of the buffer content -/// * a sink to send buffer operations into -/// * a mutexed broadcast receiver for buffer operations -/// * a channel to stop the associated worker -/// /// for each controller a worker exists, managing outgoing and inbound /// queues, transforming outbound delayed ops and applying remote changes /// to the local buffer @@ -29,10 +23,9 @@ use crate::api::TextChange; /// upon dropping this handle will stop the associated worker #[derive(Debug, Clone)] pub struct BufferController { - /// unique identifier of buffer - pub name: String, + name: String, content: watch::Receiver, - seen: StatusCheck, + seen: StatusCheck, // internal buffer previous state operations: mpsc::UnboundedSender, poller: mpsc::UnboundedSender>, _stop: Arc, // just exist @@ -54,6 +47,12 @@ impl BufferController { } } + /// unique identifier of buffer + pub fn name(&self) -> &str { + &self.name + } + + /// return buffer whole content, updating internal buffer previous state pub fn content(&self) -> String { self.seen.update(self.content.borrow().clone()); self.content.borrow().clone() @@ -73,7 +72,8 @@ impl Drop for StopOnDrop { impl Controller for BufferController { type Input = TextChange; - // block until a new text change is available + /// block until a text change is available + /// this returns immediately if one is already available async fn poll(&self) -> crate::Result<()> { if self.seen.check() != *self.content.borrow() { return Ok(()); // short circuit: already available! @@ -84,7 +84,7 @@ impl Controller for BufferController { Ok(()) } - // if a new text change is available, return it immediately + /// if a text change is available, return it immediately fn try_recv(&self) -> crate::Result> { let seen = self.seen.check(); let actual = self.content.borrow().clone(); @@ -96,7 +96,7 @@ impl Controller for BufferController { Ok(Some(change)) } - // block until a new text change is available, and return it + /// block until a new text change is available, and return it async fn recv(&self) -> crate::Result { self.poll().await?; let seen = self.seen.check(); @@ -106,7 +106,8 @@ impl Controller for BufferController { Ok(change) } - /// enqueue an opseq for processing + /// enqueue a text change for processing + /// this also updates internal buffer previous state fn send(&self, op: TextChange) -> crate::Result<()> { let before = self.seen.check(); self.seen.update(op.apply(&before)); @@ -115,7 +116,7 @@ impl Controller for BufferController { } #[derive(Debug, Clone)] -pub struct StatusCheck { +struct StatusCheck { state: watch::Receiver, updater: Arc>, } @@ -128,11 +129,11 @@ impl Default for StatusCheck { } impl StatusCheck { - pub fn update(&self, state: T) -> T { + fn update(&self, state: T) -> T { self.updater.send_replace(state) } - pub fn check(&self) -> T { + fn check(&self) -> T { self.state.borrow().clone() } }