8000 feat: controller::send now returns a future by alemidev · Pull Request #87 · hexedtech/codemp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: controller::send now returns a future #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
/// Details about the receiving end are left to the implementor.
pub trait AsyncSender<T: Sized + Send + Sync>: Sized + Send + Sync {
/// Enqueue a new value to be sent to all other users without blocking
fn send(&self, x: T) -> ControllerResult<()>;
fn send(&self, x: T) -> ControllerResult<impl std::future::Future<Output = bool>>;
}

/// Asynchronous and thread-safe handle to receive data from a stream.
Expand Down
9 changes: 5 additions & 4 deletions src/buffer/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) struct BufferControllerInner {
pub(crate) path: String,
pub(crate) latest_version: watch::Receiver<diamond_types::LocalVersion>,
pub(crate) local_version: watch::Receiver<diamond_types::LocalVersion>,
pub(crate) ops_in: mpsc::UnboundedSender<TextChange>,
pub(crate) ops_in: mpsc::UnboundedSender<(TextChange, oneshot::Sender<bool>)>,
pub(crate) poller: mpsc::UnboundedSender<oneshot::Sender<()>>,
pub(crate) content_request: mpsc::Sender<oneshot::Sender<String>>,
pub(crate) delta_request: mpsc::Sender<oneshot::Sender<Option<BufferUpdate>>>,
Expand All @@ -71,9 +71,10 @@ pub(crate) struct BufferControllerInner {
impl Controller<TextChange, BufferUpdate> for BufferController {}

impl AsyncSender<TextChange> for BufferController {
fn send(&self, op: TextChange) -> ControllerResult<()> {
self.0.ops_in.send(op)?;
Ok(())
fn send(&self, op: TextChange) -> ControllerResult<impl std::future::Future<Output = bool>> {
let (tx, rx) = oneshot::channel();
self.0.ops_in.send((op, tx))?;
Ok(async move { rx.await.unwrap_or(false) })
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/buffer/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct BufferWorker {
latest_version: watch::Sender<diamond_types::LocalVersion>,
local_version: watch::Sender<diamond_types::LocalVersion>,
ack_rx: mpsc::UnboundedReceiver<LocalVersion>,
ops_in: mpsc::UnboundedReceiver<TextChange>,
ops_in: mpsc::UnboundedReceiver<(TextChange, oneshot::Sender<bool>)>,
poller: mpsc::UnboundedReceiver<oneshot::Sender<()>>,
pollers: Vec<oneshot::Sender<()>>,
content_checkout: mpsc::Receiver<oneshot::Sender<String>>,
Expand Down Expand Up @@ -133,7 +133,7 @@ impl BufferController {
// received a text change from editor
res = worker.ops_in.recv() => match res {
None => break tracing::debug!("stopping: editor closed channel"),
Some(change) => worker.handle_editor_change(change, &tx).await,
Some((change, sent)) => worker.handle_editor_change(change, sent, &tx).await,
},

// received a message from server: add to oplog and update latest version (+unlock pollers)
Expand Down Expand Up @@ -170,7 +170,7 @@ impl BufferController {

impl BufferWorker {
#[tracing::instrument(skip(self, tx))]
async fn handle_editor_change(&mut self, change: TextChange, tx: &mpsc::Sender<Operation>) {
async fn handle_editor_change(&mut self, change: TextChange, sent: oneshot::Sender<bool>, tx: &mpsc::Sender<Operation>) {
let last_ver = self.oplog.local_version();
// clip to buffer extents
let clip_start = change.start_idx as usize;
Expand Down Expand Up @@ -199,14 +199,17 @@ impl BufferWorker {
tx.send(Operation {
data: self.oplog.encode_from(ENCODE_PATCH, &last_ver),
})
.await
.unwrap_or_warn("failed to send change!");
.await
.unwrap_or_warn("failed to send change!");
self.latest_version
.send(self.oplog.local_version())
.unwrap_or_warn("failed to update latest version!");
self.local_version
.send(self.branch.local_version())
.unwrap_or_warn("failed to update local version!");
let _ = sent.send(true);
} else {
let _ = sent.send(false);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/cursor/controller.rs
9382
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ impl Controller<Selection, Cursor> for CursorController {}

#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
impl AsyncSender<Selection> for CursorController {
fn send(&self, mut cursor: Selection) -> ControllerResult<()> {
fn send(&self, mut cursor: Selection) -> ControllerResult<impl std::future::Future<Output = bool>> {
if cursor.start_row > cursor.end_row
|| (cursor.start_row == cursor.end_row && cursor.start_col > cursor.end_col)
{
std::mem::swap(&mut cursor.start_row, &mut cursor.end_row);
std::mem::swap(&mut cursor.start_col, &mut cursor.end_col);
}

Ok(self.0.op.send(CursorPosition {
self.0.op.send(CursorPosition {
buffer: BufferNode {
path: cursor.buffer,
},
Expand All @@ -65,7 +65,9 @@ impl AsyncSender<Selection> for CursorController {
row: cursor.end_row,
col: cursor.end_col,
},
})?)
})?;

Ok(std::future::ready(true))
}
}

Expand Down
Loading
0