feat: return controllers, add attach

This commit is contained in:
əlemi 2023-08-17 04:36:24 +02:00
parent 463515da29
commit 1cc03838eb

View file

@ -4,7 +4,7 @@ use tokio::sync::Mutex;
use crate::{ use crate::{
buffer::controller::BufferController, buffer::controller::BufferController,
errors::Error, client::Client, cursor::controller::CursorController, errors::Error, client::Client, cursor::controller::CursorController, Controller,
}; };
@ -33,16 +33,14 @@ impl Instance {
Ok(()) Ok(())
} }
pub async fn join(&self, session: &str) -> Result<(), Error> { pub async fn join(&self, session: &str) -> Result<Arc<CursorController>, Error> {
self.client self.client
.lock() .lock()
.await .await
.as_mut() .as_mut()
.ok_or(Error::InvalidState { msg: "connect first".into() })? .ok_or(Error::InvalidState { msg: "connect first".into() })?
.join(session) .join(session)
.await?; .await
Ok(())
} }
pub async fn create(&self, path: &str, content: Option<&str>) -> Result<(), Error> { pub async fn create(&self, path: &str, content: Option<&str>) -> Result<(), Error> {
@ -52,9 +50,17 @@ impl Instance {
.as_mut() .as_mut()
.ok_or(Error::InvalidState { msg: "connect first".into() })? .ok_or(Error::InvalidState { msg: "connect first".into() })?
.create(path, content) .create(path, content)
.await?; .await
}
Ok(()) pub async fn attach(&self, path: &str) -> Result<Arc<BufferController>, Error> {
self.client
.lock()
.await
.as_mut()
.ok_or(Error::InvalidState { msg: "connect first".into() })?
.attach(path)
.await
} }
pub async fn get_cursor(&self) -> Result<Arc<CursorController>, Error> { pub async fn get_cursor(&self) -> Result<Arc<CursorController>, Error> {