2023-08-20 00:46:55 +02:00
|
|
|
//! ### controller
|
2024-08-05 22:44:46 +02:00
|
|
|
//!
|
2023-08-20 00:46:55 +02:00
|
|
|
//! a controller implementation for buffer actions
|
|
|
|
|
2023-11-09 05:21:24 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-11-23 15:52:36 +01:00
|
|
|
use tokio::sync::oneshot;
|
2024-08-05 22:44:46 +02:00
|
|
|
use tokio::sync::{mpsc, watch};
|
2023-08-16 23:09:47 +02:00
|
|
|
use tonic::async_trait;
|
2023-08-11 15:33:40 +02:00
|
|
|
|
2023-11-17 05:47:57 +01:00
|
|
|
use crate::api::Controller;
|
2023-08-16 23:09:47 +02:00
|
|
|
|
2023-11-17 05:45:31 +01:00
|
|
|
use crate::api::TextChange;
|
2023-08-11 15:33:40 +02:00
|
|
|
|
2024-08-08 00:27:24 +02:00
|
|
|
use super::tools::InternallyMutable;
|
|
|
|
|
2023-08-20 00:46:55 +02:00
|
|
|
/// the buffer controller implementation
|
|
|
|
///
|
|
|
|
/// for each controller a worker exists, managing outgoing and inbound
|
2024-08-05 22:44:46 +02:00
|
|
|
/// queues, transforming outbound delayed ops and applying remote changes
|
2023-08-20 00:46:55 +02:00
|
|
|
/// to the local buffer
|
|
|
|
///
|
|
|
|
/// upon dropping this handle will stop the associated worker
|
2023-11-09 05:21:24 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2024-08-05 22:44:46 +02:00
|
|
|
#[cfg_attr(feature = "python", pyo3::pyclass)]
|
2024-08-07 23:06:33 +02:00
|
|
|
#[cfg_attr(feature = "js", napi_derive::napi)]
|
2024-08-08 00:28:15 +02:00
|
|
|
pub struct BufferController(pub(crate) Arc<BufferControllerInner>);
|
|
|
|
|
|
|
|
impl BufferController {
|
|
|
|
/// unique identifier of buffer
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
&self.0.name
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return buffer whole content, updating internal buffer previous state
|
|
|
|
pub fn content(&self) -> String {
|
|
|
|
self.0.seen.set(self.0.content.borrow().clone());
|
|
|
|
self.0.content.borrow().clone()
|
|
|
|
}
|
|
|
|
}
|
2024-08-05 19:13:48 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2024-08-08 00:28:15 +02:00
|
|
|
pub(crate) struct BufferControllerInner {
|
2023-11-30 03:41:53 +01:00
|
|
|
name: String,
|
2023-08-16 23:09:47 +02:00
|
|
|
content: watch::Receiver<String>,
|
2024-08-08 00:27:24 +02:00
|
|
|
seen: InternallyMutable<String>, // internal buffer previous state
|
2023-11-09 05:21:24 +01:00
|
|
|
operations: mpsc::UnboundedSender<TextChange>,
|
2023-11-30 03:03:09 +01:00
|
|
|
poller: mpsc::UnboundedSender<oneshot::Sender<()>>,
|
2024-08-08 00:28:15 +02:00
|
|
|
stopper: mpsc::UnboundedSender<()>, // just exist
|
2023-07-09 03:44:27 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 00:28:15 +02:00
|
|
|
impl BufferControllerInner {
|
2023-08-17 00:04:37 +02:00
|
|
|
pub(crate) fn new(
|
2023-11-17 17:38:47 +01:00
|
|
|
name: String,
|
2023-08-17 00:04:37 +02:00
|
|
|
content: watch::Receiver<String>,
|
2023-11-09 05:21:24 +01:00
|
|
|
operations: mpsc::UnboundedSender<TextChange>,
|
2023-11-30 03:03:09 +01:00
|
|
|
poller: mpsc::UnboundedSender<oneshot::Sender<()>>,
|
2023-08-19 04:02:21 +02:00
|
|
|
stop: mpsc::UnboundedSender<()>,
|
2023-08-17 00:04:37 +02:00
|
|
|
) -> Self {
|
2024-08-08 00:28:15 +02:00
|
|
|
Self {
|
2024-08-05 22:44:46 +02:00
|
|
|
name,
|
|
|
|
content,
|
|
|
|
operations,
|
|
|
|
poller,
|
2024-08-08 00:27:24 +02:00
|
|
|
seen: InternallyMutable::default(),
|
2024-08-08 00:28:15 +02:00
|
|
|
stopper: stop,
|
|
|
|
}
|
2023-08-17 00:04:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 23:09:47 +02:00
|
|
|
#[async_trait]
|
2023-11-16 06:52:25 +01:00
|
|
|
impl Controller<TextChange> for BufferController {
|
2023-11-30 03:41:53 +01:00
|
|
|
/// block until a text change is available
|
|
|
|
/// this returns immediately if one is already available
|
2023-11-17 05:47:57 +01:00
|
|
|
async fn poll(&self) -> crate::Result<()> {
|
2024-08-08 00:27:24 +02:00
|
|
|
if self.0.seen.get() != *self.0.content.borrow() {
|
2023-11-30 03:03:09 +01:00
|
|
|
return Ok(()); // short circuit: already available!
|
|
|
|
}
|
2023-11-23 15:52:36 +01:00
|
|
|
let (tx, rx) = oneshot::channel::<()>();
|
2024-08-05 19:13:48 +02:00
|
|
|
self.0.poller.send(tx)?;
|
2024-08-05 22:44:46 +02:00
|
|
|
rx.await
|
|
|
|
.map_err(|_| crate::Error::Channel { send: false })?;
|
2023-11-30 03:03:09 +01:00
|
|
|
Ok(())
|
2023-08-21 02:35:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-30 03:41:53 +01:00
|
|
|
/// if a text change is available, return it immediately
|
2023-11-17 05:47:57 +01:00
|
|
|
fn try_recv(&self) -> crate::Result<Option<TextChange>> {
|
2024-08-08 00:27:24 +02:00
|
|
|
let seen = self.0.seen.get();
|
2024-08-05 19:13:48 +02:00
|
|
|
let actual = self.0.content.borrow().clone();
|
2023-11-17 18:38:29 +01:00
|
|
|
if seen == actual {
|
|
|
|
return Ok(None);
|
2023-11-16 06:52:25 +01:00
|
|
|
}
|
2023-11-17 18:38:29 +01:00
|
|
|
let change = TextChange::from_diff(&seen, &actual);
|
2024-08-08 00:27:24 +02:00
|
|
|
self.0.seen.set(actual);
|
2023-11-17 18:38:29 +01:00
|
|
|
Ok(Some(change))
|
2023-08-21 02:35:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-30 03:41:53 +01:00
|
|
|
/// block until a new text change is available, and return it
|
2023-11-17 05:47:57 +01:00
|
|
|
async fn recv(&self) -> crate::Result<TextChange> {
|
2023-11-16 06:52:25 +01:00
|
|
|
self.poll().await?;
|
2024-08-08 00:27:24 +02:00
|
|
|
let seen = self.0.seen.get();
|
2024-08-05 19:13:48 +02:00
|
|
|
let actual = self.0.content.borrow().clone();
|
2023-11-30 03:03:09 +01:00
|
|
|
let change = TextChange::from_diff(&seen, &actual);
|
2024-08-08 00:27:24 +02:00
|
|
|
self.0.seen.set(actual);
|
2023-11-17 05:47:57 +01:00
|
|
|
Ok(change)
|
2023-07-09 03:44:27 +02:00
|
|
|
}
|
|
|
|
|
2023-11-30 03:41:53 +01:00
|
|
|
/// enqueue a text change for processing
|
|
|
|
/// this also updates internal buffer previous state
|
2023-11-17 05:47:57 +01:00
|
|
|
fn send(&self, op: TextChange) -> crate::Result<()> {
|
2024-08-08 00:27:24 +02:00
|
|
|
let before = self.0.seen.get();
|
|
|
|
self.0.seen.set(op.apply(&before));
|
2024-08-05 19:13:48 +02:00
|
|
|
Ok(self.0.operations.send(op)?)
|
2023-07-09 03:44:27 +02:00
|
|
|
}
|
2023-11-30 03:03:09 +01:00
|
|
|
|
2024-08-08 00:28:15 +02:00
|
|
|
fn stop(&self) -> bool {
|
|
|
|
self.0.stopper.send(()).is_ok()
|
2023-11-30 03:03:09 +01:00
|
|
|
}
|
|
|
|
}
|