mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 23:44:55 +01:00
feat: reworked cursor handle mechanism
instead of storing cursors it just streams them as they happen. instead of just getting cursors from the controller, now you also send your operations into it, mimicking more the behavior used for text ops
This commit is contained in:
parent
340eafb432
commit
38911bdc31
2 changed files with 140 additions and 80 deletions
|
@ -6,9 +6,9 @@ use tracing::{error, warn, debug};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cursor::{CursorController, CursorStorage},
|
cursor::{CursorControllerHandle, CursorControllerWorker, CursorProvider},
|
||||||
operation::{OperationProcessor, OperationController},
|
operation::{OperationProcessor, OperationController},
|
||||||
proto::{buffer_client::BufferClient, BufferPayload, OperationRequest, CursorMov},
|
proto::{buffer_client::BufferClient, BufferPayload, OperationRequest, CursorMov}, errors::IgnorableError,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -24,6 +24,8 @@ impl From::<BufferClient<Channel>> for CodempClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodempClient {
|
impl CodempClient {
|
||||||
|
pub fn id(&self) -> &str { &self.id }
|
||||||
|
|
||||||
pub async fn create(&mut self, path: String, content: Option<String>) -> Result<bool, Status> {
|
pub async fn create(&mut self, path: String, content: Option<String>) -> Result<bool, Status> {
|
||||||
let req = BufferPayload {
|
let req = BufferPayload {
|
||||||
path, content,
|
path, content,
|
||||||
|
@ -35,7 +37,7 @@ impl CodempClient {
|
||||||
Ok(res.into_inner().accepted)
|
Ok(res.into_inner().accepted)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn listen(&mut self) -> Result<Arc<CursorController>, Status> {
|
pub async fn listen(&mut self) -> Result<CursorControllerHandle, Status> {
|
||||||
let req = BufferPayload {
|
let req = BufferPayload {
|
||||||
path: "".into(),
|
path: "".into(),
|
||||||
content: None,
|
content: None,
|
||||||
|
@ -44,20 +46,30 @@ impl CodempClient {
|
||||||
|
|
||||||
let mut stream = self.client.listen(req).await?.into_inner();
|
let mut stream = self.client.listen(req).await?.into_inner();
|
||||||
|
|
||||||
let controller = Arc::new(CursorController::new());
|
let mut controller = CursorControllerWorker::new(self.id().to_string());
|
||||||
|
let handle = controller.subscribe();
|
||||||
|
let mut _client = self.client.clone();
|
||||||
|
|
||||||
let _controller = controller.clone();
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
match stream.message().await {
|
tokio::select!{
|
||||||
|
res = stream.message() => {
|
||||||
|
match res {
|
||||||
Err(e) => break error!("error receiving cursor: {}", e),
|
Err(e) => break error!("error receiving cursor: {}", e),
|
||||||
Ok(None) => break debug!("cursor worker clean exit"),
|
Ok(None) => break debug!("cursor worker clean exit"),
|
||||||
Ok(Some(x)) => { _controller.update(x); },
|
Ok(Some(x)) => { controller.broadcast(x); },
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Some(op) = controller.wait() => {
|
||||||
|
_client.cursor(CursorMov::from(op)).await
|
||||||
|
.unwrap_or_warn("could not send cursor update")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(controller)
|
Ok(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn attach(&mut self, path: String) -> Result<Arc<OperationController>, Status> {
|
pub async fn attach(&mut self, path: String) -> Result<Arc<OperationController>, Status> {
|
||||||
|
@ -125,13 +137,4 @@ impl CodempClient {
|
||||||
|
|
||||||
Ok(factory)
|
Ok(factory)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn cursor(&mut self, path: String, row: i64, col: i64) -> Result<bool, Status> {
|
|
||||||
let req = CursorMov {
|
|
||||||
path, row, col,
|
|
||||||
user: self.id.clone(),
|
|
||||||
};
|
|
||||||
let res = self.client.cursor(req).await?.into_inner();
|
|
||||||
Ok(res.accepted)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
173
src/cursor.rs
173
src/cursor.rs
|
@ -1,22 +1,40 @@
|
||||||
use std::{collections::HashMap, sync::Mutex};
|
use std::sync::Arc;
|
||||||
|
|
||||||
use tokio::sync::broadcast;
|
use tokio::sync::{mpsc, broadcast};
|
||||||
use tracing::{error, debug, warn};
|
use tonic::async_trait;
|
||||||
|
|
||||||
use crate::proto::CursorMov;
|
use crate::{proto::CursorMov, errors::IgnorableError};
|
||||||
|
|
||||||
/// Note that this differs from any hashmap in its put method: no &mut!
|
// TODO temp struct before we update protocol for real
|
||||||
pub trait CursorStorage {
|
#[derive(Clone, Debug, Default)]
|
||||||
fn get(&self, id: &str) -> Option<Cursor>;
|
pub struct Cursor {
|
||||||
fn put(&self, id: String, val: Cursor);
|
pub user: String,
|
||||||
|
pub buffer: String,
|
||||||
|
pub start: Position,
|
||||||
|
pub end: Position,
|
||||||
|
}
|
||||||
|
|
||||||
fn update(&self, event: CursorMov) -> Option<Cursor> {
|
impl From::<Cursor> for CursorMov {
|
||||||
let mut cur = self.get(&event.user)?;
|
fn from(cursor: Cursor) -> CursorMov {
|
||||||
cur.buffer = event.path;
|
CursorMov {
|
||||||
cur.start = (event.row, event.col).into();
|
user: cursor.user,
|
||||||
self.put(event.user, cur.clone());
|
path: cursor.buffer,
|
||||||
Some(cur)
|
row: cursor.start.row,
|
||||||
|
col: cursor.start.col,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From::<CursorMov> for Cursor {
|
||||||
|
fn from(cursor: CursorMov) -> Self {
|
||||||
|
Cursor {
|
||||||
|
user: cursor.user,
|
||||||
|
buffer: cursor.path,
|
||||||
|
start: (cursor.row, cursor.col).into(),
|
||||||
|
end: (0,0).into(), // TODO temp!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Default)]
|
#[derive(Copy, Clone, Debug, Default)]
|
||||||
|
@ -31,64 +49,103 @@ impl From::<(i64, i64)> for Position {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
impl From::<(i32, i32)> for Position {
|
||||||
pub struct Cursor {
|
fn from((row, col): (i32, i32)) -> Self {
|
||||||
pub buffer: String,
|
Position { row: row as i64, col: col as i64 }
|
||||||
pub start: Position,
|
}
|
||||||
pub end: Position,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[async_trait]
|
||||||
pub struct CursorController {
|
pub trait CursorSubscriber {
|
||||||
users: Mutex<HashMap<String, Cursor>>,
|
async fn send(&self, path: &str, start: Position, end: Position);
|
||||||
bus: broadcast::Sender<(String, Cursor)>,
|
async fn poll(&mut self) -> Option<Cursor>;
|
||||||
_bus_keepalive: Mutex<broadcast::Receiver<(String, Cursor)>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for CursorController {
|
pub struct CursorControllerHandle {
|
||||||
fn default() -> Self {
|
uid: String,
|
||||||
let (tx, _rx) = broadcast::channel(64);
|
op: mpsc::Sender<Cursor>,
|
||||||
CursorController {
|
stream: broadcast::Receiver<Cursor>,
|
||||||
users: Mutex::new(HashMap::new()),
|
original: Arc<broadcast::Sender<Cursor>>,
|
||||||
bus: tx,
|
}
|
||||||
_bus_keepalive: Mutex::new(_rx),
|
|
||||||
|
impl Clone for CursorControllerHandle {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
uid: self.uid.clone(),
|
||||||
|
op: self.op.clone(),
|
||||||
|
stream: self.original.subscribe(),
|
||||||
|
original: self.original.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CursorController {
|
#[async_trait]
|
||||||
pub fn new() -> Self {
|
impl CursorSubscriber for CursorControllerHandle {
|
||||||
CursorController::default()
|
async fn send(&self, path: &str, start: Position, end: Position) {
|
||||||
|
self.op.send(Cursor {
|
||||||
|
user: self.uid.clone(),
|
||||||
|
buffer: path.to_string(),
|
||||||
|
start, end
|
||||||
|
}).await.unwrap_or_warn("could not send cursor op")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sub(&self) -> broadcast::Receiver<(String, Cursor)> {
|
async fn poll(&mut self) -> Option<Cursor> {
|
||||||
self.bus.subscribe()
|
match self.stream.recv().await {
|
||||||
|
Ok(x) => Some(x),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("could not poll for cursor: {}", e);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CursorStorage for CursorController {
|
#[async_trait]
|
||||||
fn update(&self, event: CursorMov) -> Option<Cursor> {
|
pub(crate) trait CursorProvider<T>
|
||||||
debug!("processing cursor event: {:?}", event);
|
where T : CursorSubscriber {
|
||||||
let mut cur = self.get(&event.user).unwrap_or(Cursor::default());
|
fn subscribe(&self) -> T;
|
||||||
cur.buffer = event.path;
|
fn broadcast(&self, op: CursorMov);
|
||||||
cur.start = (event.row, event.col).into();
|
async fn wait(&mut self) -> Option<Cursor>;
|
||||||
cur.end = (event.row, event.col).into();
|
}
|
||||||
self.put(event.user.clone(), cur.clone());
|
|
||||||
if let Err(e) = self.bus.send((event.user, cur.clone())) {
|
|
||||||
error!("could not broadcast cursor event: {}", e);
|
|
||||||
} else { // this is because once there are no receivers, nothing else can be sent
|
|
||||||
if let Err(e) = self._bus_keepalive.lock().unwrap().try_recv() {
|
|
||||||
warn!("could not consume event: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(cur)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get(&self, id: &str) -> Option<Cursor> {
|
pub(crate) struct CursorControllerWorker {
|
||||||
Some(self.users.lock().unwrap().get(id)?.clone())
|
uid: String,
|
||||||
}
|
producer: mpsc::Sender<Cursor>,
|
||||||
|
op: mpsc::Receiver<Cursor>,
|
||||||
|
channel: Arc<broadcast::Sender<Cursor>>,
|
||||||
|
}
|
||||||
|
|
||||||
fn put(&self, id: String, val: Cursor) {
|
impl CursorControllerWorker {
|
||||||
self.users.lock().unwrap().insert(id, val);
|
pub(crate) fn new(uid: String) -> Self {
|
||||||
|
let (op_tx, op_rx) = mpsc::channel(64);
|
||||||
|
let (cur_tx, _cur_rx) = broadcast::channel(64);
|
||||||
|
CursorControllerWorker {
|
||||||
|
uid,
|
||||||
|
producer: op_tx,
|
||||||
|
op: op_rx,
|
||||||
|
channel: Arc::new(cur_tx),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl CursorProvider<CursorControllerHandle> for CursorControllerWorker {
|
||||||
|
fn broadcast(&self, op: CursorMov) {
|
||||||
|
self.channel.send(op.into()).unwrap_or_warn("could not broadcast cursor event")
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn wait(&mut self) -> Option<Cursor> {
|
||||||
|
self.op.recv().await
|
||||||
|
}
|
||||||
|
|
||||||
|
fn subscribe(&self) -> CursorControllerHandle {
|
||||||
|
CursorControllerHandle {
|
||||||
|
uid: self.uid.clone(),
|
||||||
|
op: self.producer.clone(),
|
||||||
|
stream: self.channel.subscribe(),
|
||||||
|
original: self.channel.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue