Skip to content

Commit

Permalink
feat: implemented AsyncReceiver for Workspace...
Browse files Browse the repository at this point in the history
... its very bad tho, very very bad

Co-authored-by: zaaarf <[email protected]>
  • Loading branch information
alemidev and zaaarf committed Sep 28, 2024
1 parent 1b16d4a commit ddbad59
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! Buffers are typically organized in a filetree-like reminiscent of POSIX filesystems.

use crate::{
api::{Event, User},
api::{controller::{AsyncReceiver, ControllerCallback}, Event, User},
buffer, cursor,
errors::{ConnectionResult, ControllerResult, RemoteResult},
ext::InternallyMutable,
Expand All @@ -24,7 +24,7 @@ use codemp_proto::{

use dashmap::{DashMap, DashSet};
use std::{collections::BTreeSet, sync::Arc};
use tokio::sync::mpsc;
use tokio::sync::{mpsc::error::TryRecvError, mpsc};
use tonic::Streaming;
use uuid::Uuid;

Expand Down Expand Up @@ -55,6 +55,39 @@ struct WorkspaceInner {
users: Arc<DashMap<Uuid, User>>,
// TODO can we drop the mutex?
events: tokio::sync::Mutex<mpsc::UnboundedReceiver<crate::api::Event>>,
callback: std::sync::Mutex<Option<ControllerCallback<Workspace>>>, // TODO lmao another one
}

impl AsyncReceiver<Event> for Workspace {
async fn try_recv(&self) -> ControllerResult<Option<Event>> {
match self.0
.events
.lock()
.await
.try_recv()
{
Ok(x) => Ok(Some(x)),
Err(TryRecvError::Empty) => Ok(None),
Err(TryRecvError::Disconnected) => Err(crate::errors::ControllerError::Stopped),
}
}

async fn poll(&self) -> ControllerResult<()> {
loop {
if !self.0.events.lock().await.is_empty() { break Ok(()) }
// TODO disgusting, please send help
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
}
}

// TODO please send HELP ASAP this is hurting me emotionally
fn clear_callback(&self) {
*self.0.callback.lock().expect("mutex poisoned") = None;
}

fn callback(&self, cb: impl Into<ControllerCallback<Self>>) {
*self.0.callback.lock().expect("mutex poisoned") = Some(cb.into());
}
}

impl Workspace {
Expand Down Expand Up @@ -91,6 +124,7 @@ impl Workspace {
users,
events: tokio::sync::Mutex::new(ev_rx),
services,
callback: std::sync::Mutex::new(None),
}));

ws.fetch_users().await?;
Expand Down Expand Up @@ -161,18 +195,6 @@ impl Workspace {
}
}

/// Await next workspace [Event] and return it when it arrives.
// TODO this method is weird and ugly, can we make it more standard?
pub async fn event(&self) -> ControllerResult<Event> {
self.0
.events
.lock()
.await
.recv()
.await
.ok_or(crate::errors::ControllerError::Unfulfilled)
}

/// Re-fetch the list of available buffers in the workspace.
pub async fn fetch_buffers(&self) -> RemoteResult<()> {
let mut workspace_client = self.0.services.ws();
Expand Down

0 comments on commit ddbad59

Please sign in to comment.