mirror of
https://github.com/hexedtech/codemp.git
synced 2025-03-16 11:55:31 +01:00
Merge pull request #78 from hexedtech/fix/unknown-cursor
feat: handle unknown cursor
This commit is contained in:
commit
51fe6e4e82
1 changed files with 28 additions and 16 deletions
|
@ -18,11 +18,36 @@ struct CursorWorker {
|
||||||
stream: mpsc::Receiver<oneshot::Sender<Option<Cursor>>>,
|
stream: mpsc::Receiver<oneshot::Sender<Option<Cursor>>>,
|
||||||
poll: mpsc::UnboundedReceiver<oneshot::Sender<()>>,
|
poll: mpsc::UnboundedReceiver<oneshot::Sender<()>>,
|
||||||
pollers: Vec<oneshot::Sender<()>>,
|
pollers: Vec<oneshot::Sender<()>>,
|
||||||
store: std::collections::VecDeque<Cursor>,
|
store: std::collections::VecDeque<codemp_proto::cursor::CursorEvent>,
|
||||||
controller: std::sync::Weak<CursorControllerInner>,
|
controller: std::sync::Weak<CursorControllerInner>,
|
||||||
callback: watch::Receiver<Option<ControllerCallback<CursorController>>>,
|
callback: watch::Receiver<Option<ControllerCallback<CursorController>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CursorWorker {
|
||||||
|
fn handle_recv(&mut self, tx: oneshot::Sender<Option<Cursor>>) {
|
||||||
|
tx.send(
|
||||||
|
self.store.pop_front().and_then(|event| {
|
||||||
|
let user_id = Uuid::from(event.user);
|
||||||
|
if let Some(user_name) = self.map.get(&user_id).map(|u| u.name.clone()) {
|
||||||
|
Some(Cursor {
|
||||||
|
user: user_name,
|
||||||
|
sel: Selection {
|
||||||
|
buffer: event.position.buffer.path,
|
||||||
|
start_row: event.position.start.row,
|
||||||
|
start_col: event.position.start.col,
|
||||||
|
end_row: event.position.end.row,
|
||||||
|
end_col: event.position.end.col
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
tracing::warn!("received cursor for unknown user {user_id}");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
).unwrap_or_warn("client gave up receiving!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CursorController {
|
impl CursorController {
|
||||||
pub(crate) fn spawn(
|
pub(crate) fn spawn(
|
||||||
user_map: Arc<dashmap::DashMap<Uuid, User>>,
|
user_map: Arc<dashmap::DashMap<Uuid, User>>,
|
||||||
|
@ -88,19 +113,7 @@ impl CursorController {
|
||||||
None => break, // clean exit, just weird that we got it here
|
None => break, // clean exit, just weird that we got it here
|
||||||
Some(controller) => {
|
Some(controller) => {
|
||||||
tracing::debug!("received cursor from server");
|
tracing::debug!("received cursor from server");
|
||||||
let user_id = Uuid::from(cur.user);
|
worker.store.push_back(cur);
|
||||||
let cursor = Cursor {
|
|
||||||
user: worker.map.get(&user_id).map(|u| u.name.clone()).unwrap_or_default(),
|
|
||||||
sel: Selection {
|
|
||||||
buffer: cur.position.buffer.path,
|
|
||||||
start_row: cur.position.start.row,
|
|
||||||
start_col: cur.position.start.col,
|
|
||||||
end_row: cur.position.end.row,
|
|
||||||
end_col: cur.position.end.col
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
worker.store.push_back(cursor);
|
|
||||||
for tx in worker.pollers.drain(..) {
|
for tx in worker.pollers.drain(..) {
|
||||||
tx.send(()).unwrap_or_warn("poller dropped before unblocking");
|
tx.send(()).unwrap_or_warn("poller dropped before unblocking");
|
||||||
}
|
}
|
||||||
|
@ -112,8 +125,7 @@ impl CursorController {
|
||||||
},
|
},
|
||||||
|
|
||||||
// client wants to get next cursor event
|
// client wants to get next cursor event
|
||||||
Some(tx) = worker.stream.recv() => tx.send(worker.store.pop_front())
|
Some(tx) = worker.stream.recv() => worker.handle_recv(tx),
|
||||||
.unwrap_or_warn("client gave up receiving"),
|
|
||||||
|
|
||||||
else => break,
|
else => break,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue