2023-08-20 00:46:55 +02:00
|
|
|
//! ### controller
|
|
|
|
//!
|
|
|
|
//! a controller implementation for cursor actions
|
|
|
|
|
2023-08-21 02:35:56 +02:00
|
|
|
use tokio::sync::{mpsc, broadcast::{self, error::{TryRecvError, RecvError}}, Mutex, watch};
|
2023-08-16 23:09:47 +02:00
|
|
|
use tonic::async_trait;
|
2024-01-25 02:13:45 +01:00
|
|
|
use uuid::Uuid;
|
2023-08-16 23:09:47 +02:00
|
|
|
|
2024-01-25 02:13:45 +01:00
|
|
|
use crate::{api::Controller, errors::IgnorableError, proto::{cursor::{CursorEvent, CursorPosition}, user::UserIdentity}};
|
2023-08-16 23:09:47 +02:00
|
|
|
|
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
|
|
|
|
///
|
|
|
|
/// for each controller a worker exists, managing outgoing and inbound event queues
|
|
|
|
///
|
|
|
|
/// upon dropping this handle will stop the associated worker
|
2023-08-21 02:47:52 +02:00
|
|
|
#[derive(Debug)]
|
2023-08-16 23:09:47 +02:00
|
|
|
pub struct CursorController {
|
2024-01-25 02:13:45 +01:00
|
|
|
user_id: Uuid,
|
2023-08-19 21:44:27 +02:00
|
|
|
op: mpsc::UnboundedSender<CursorEvent>,
|
2023-08-21 02:35:56 +02:00
|
|
|
last_op: Mutex<watch::Receiver<CursorEvent>>,
|
2023-08-16 23:09:47 +02:00
|
|
|
stream: Mutex<broadcast::Receiver<CursorEvent>>,
|
2023-08-19 04:02:21 +02:00
|
|
|
stop: mpsc::UnboundedSender<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for CursorController {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.stop.send(()).unwrap_or_warn("could not stop cursor actor")
|
|
|
|
}
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|
|
|
|
|
2023-08-17 00:04:37 +02:00
|
|
|
impl CursorController {
|
|
|
|
pub(crate) fn new(
|
2024-01-25 02:13:45 +01:00
|
|
|
user_id: Uuid,
|
2023-08-19 21:44:27 +02:00
|
|
|
op: mpsc::UnboundedSender<CursorEvent>,
|
2023-08-21 02:35:56 +02:00
|
|
|
last_op: Mutex<watch::Receiver<CursorEvent>>,
|
2023-08-19 04:02:21 +02:00
|
|
|
stream: Mutex<broadcast::Receiver<CursorEvent>>,
|
|
|
|
stop: mpsc::UnboundedSender<()>,
|
2023-08-17 00:04:37 +02:00
|
|
|
) -> Self {
|
2024-01-25 02:13:45 +01:00
|
|
|
CursorController { user_id, op, last_op, stream, stop }
|
2023-08-17 00:04:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 23:09:47 +02:00
|
|
|
#[async_trait]
|
|
|
|
impl Controller<CursorEvent> for CursorController {
|
|
|
|
type Input = CursorPosition;
|
|
|
|
|
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-01-25 02:13:45 +01:00
|
|
|
fn send(&self, mut cursor: CursorPosition) -> crate::Result<()> {
|
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);
|
|
|
|
}
|
2023-08-16 23:09:47 +02:00
|
|
|
Ok(self.op.send(CursorEvent {
|
2024-01-25 16:31:38 +01:00
|
|
|
user: UserIdentity { id: self.user_id.as_bytes().to_vec() },
|
2024-01-25 02:13:45 +01:00
|
|
|
position: cursor,
|
2023-08-19 21:44:27 +02:00
|
|
|
})?)
|
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
|
|
|
|
fn try_recv(&self) -> crate::Result<Option<CursorEvent>> {
|
|
|
|
let mut stream = self.stream.blocking_lock();
|
|
|
|
match stream.try_recv() {
|
|
|
|
Ok(x) => Ok(Some(x)),
|
|
|
|
Err(TryRecvError::Empty) => Ok(None),
|
2024-01-25 02:13:45 +01:00
|
|
|
Err(TryRecvError::Closed) => Err(crate::Error::Channel { send: false }),
|
2023-08-21 02:35:56 +02:00
|
|
|
Err(TryRecvError::Lagged(n)) => {
|
|
|
|
tracing::warn!("cursor channel lagged, skipping {} events", n);
|
|
|
|
Ok(stream.try_recv().ok())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 23:09:47 +02:00
|
|
|
// TODO is this cancelable? so it can be used in tokio::select!
|
|
|
|
// TODO is the result type overkill? should be an option?
|
2023-08-20 00:46:55 +02:00
|
|
|
/// get next cursor event from current workspace, or block until one is available
|
2024-01-25 02:13:45 +01:00
|
|
|
async fn recv(&self) -> crate::Result<CursorEvent> {
|
2023-08-16 23:09:47 +02:00
|
|
|
let mut stream = self.stream.lock().await;
|
|
|
|
match stream.recv().await {
|
|
|
|
Ok(x) => Ok(x),
|
2024-01-25 02:13:45 +01:00
|
|
|
Err(RecvError::Closed) => Err(crate::Error::Channel { send: false }),
|
2023-08-16 23:09:47 +02:00
|
|
|
Err(RecvError::Lagged(n)) => {
|
|
|
|
tracing::error!("cursor channel lagged behind, skipping {} events", n);
|
|
|
|
Ok(stream.recv().await.expect("could not receive after lagging"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-21 02:35:56 +02:00
|
|
|
|
|
|
|
/// await for changed mutex and then next op change
|
|
|
|
async fn poll(&self) -> crate::Result<()> {
|
|
|
|
Ok(self.last_op.lock().await.changed().await?)
|
|
|
|
}
|
|
|
|
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|