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 cursor actions
|
2024-08-05 19:13:48 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-08-16 16:46:32 +02:00
|
|
|
use tokio::sync::{
|
|
|
|
broadcast::{self, error::TryRecvError},
|
|
|
|
mpsc, watch, Mutex,
|
|
|
|
};
|
2023-08-16 23:09:47 +02:00
|
|
|
use tonic::async_trait;
|
|
|
|
|
2024-03-09 19:59:36 +01:00
|
|
|
use codemp_proto::cursor::{CursorEvent, CursorPosition};
|
2024-09-01 02:46:03 +02:00
|
|
|
use crate::{api::{controller::ControllerCallback, Controller, Cursor}, errors::ControllerResult};
|
2023-08-20 00:46:55 +02:00
|
|
|
/// the cursor controller implementation
|
|
|
|
///
|
|
|
|
/// this contains
|
|
|
|
/// * the unique identifier of current user
|
|
|
|
/// * a sink to send movements into
|
|
|
|
/// * a mutex over a stream of inbound cursor events
|
|
|
|
/// * a channel to stop the associated worker
|
|
|
|
///
|
2024-08-05 22:44:46 +02:00
|
|
|
/// for each controller a worker exists , managing outgoing and inbound event queues
|
2023-08-20 00:46:55 +02:00
|
|
|
///
|
|
|
|
/// upon dropping this handle will stop the associated worker
|
2024-08-05 19:13:48 +02: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 02:27:06 +02:00
|
|
|
pub struct CursorController(pub(crate) Arc<CursorControllerInner>);
|
2024-08-05 19:13:48 +02:00
|
|
|
|
2023-08-21 02:47:52 +02:00
|
|
|
#[derive(Debug)]
|
2024-08-08 02:27:06 +02:00
|
|
|
pub(crate) struct CursorControllerInner {
|
2024-08-15 19:31:34 +02:00
|
|
|
pub(crate) op: mpsc::Sender<CursorPosition>,
|
|
|
|
pub(crate) last_op: Mutex<watch::Receiver<CursorEvent>>,
|
|
|
|
pub(crate) stream: Mutex<broadcast::Receiver<CursorEvent>>,
|
2024-08-15 20:41:53 +02:00
|
|
|
pub(crate) callback: watch::Sender<Option<ControllerCallback<CursorController>>>,
|
2024-08-15 19:31:34 +02:00
|
|
|
pub(crate) stop: mpsc::UnboundedSender<()>,
|
2023-08-17 00:04:37 +02:00
|
|
|
}
|
|
|
|
|
2023-08-16 23:09:47 +02:00
|
|
|
#[async_trait]
|
2024-08-05 19:13:48 +02:00
|
|
|
impl Controller<Cursor> for CursorController {
|
2023-08-20 00:46:55 +02:00
|
|
|
/// enqueue a cursor event to be broadcast to current workspace
|
2023-09-10 23:20:07 +02:00
|
|
|
/// will automatically invert cursor start/end if they are inverted
|
2024-09-01 02:46:03 +02:00
|
|
|
async fn send(&self, mut cursor: Cursor) -> ControllerResult<()> {
|
2024-01-25 03:25:45 +01:00
|
|
|
if cursor.start > cursor.end {
|
2023-09-10 23:20:07 +02:00
|
|
|
std::mem::swap(&mut cursor.start, &mut cursor.end);
|
|
|
|
}
|
2024-08-14 15:56:10 +02:00
|
|
|
Ok(self.0.op.send(cursor.into()).await?)
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|
|
|
|
|
2023-08-21 02:35:56 +02:00
|
|
|
/// try to receive without blocking, but will still block on stream mutex
|
2024-08-05 19:13:48 +02:00
|
|
|
let mut stream = self.0.stream.lock().await;
|
2024-08-14 00:24:32 +02:00
|
|
|
match stream.try_recv() {
|
|
|
|
Ok(x) => Ok(Some(x.into())),
|
|
|
|
Err(TryRecvError::Empty) => Ok(None),
|
|
|
|
Err(TryRecvError::Closed) => Err(crate::Error::Channel { send: false }),
|
|
|
|
Err(TryRecvError::Lagged(n)) => {
|
|
|
|
tracing::warn!("cursor channel lagged, skipping {} events", n);
|
|
|
|
Ok(stream.try_recv().map(|x| x.into()).ok())
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-01 02:46:03 +02:00
|
|
|
async fn try_recv(&self) -> ControllerResult<Option<Cursor>> {
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|
2023-08-21 02:35:56 +02:00
|
|
|
|
|
|
|
/// await for changed mutex and then next op change
|
2024-08-05 19:13:48 +02:00
|
|
|
Ok(self.0.last_op.lock().await.changed().await?)
|
2024-09-01 02:46:03 +02:00
|
|
|
async fn poll(&self) -> ControllerResult<()> {
|
2023-08-21 02:35:56 +02:00
|
|
|
}
|
2024-08-08 00:28:15 +02:00
|
|
|
|
2024-08-15 22:47:49 +02:00
|
|
|
fn callback(&self, cb: impl Into<ControllerCallback<CursorController>>) {
|
|
|
|
if self.0.callback.send(Some(cb.into())).is_err() {
|
2024-08-15 19:32:01 +02:00
|
|
|
// TODO should we panic? we failed what we were supposed to do
|
|
|
|
tracing::error!("no active cursor worker to run registered callback!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_callback(&self) {
|
|
|
|
if self.0.callback.send(None).is_err() {
|
|
|
|
tracing::warn!("no active cursor worker to clear callback");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:28:15 +02:00
|
|
|
fn stop(&self) -> bool {
|
|
|
|
self.0.stop.send(()).is_ok()
|
|
|
|
}
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|