feat: added detach buffer and leave workspace

Co-authored-by: zaaarf <me@zaaarf.foo>
This commit is contained in:
əlemi 2024-08-08 00:28:35 +02:00
parent 6e9727128d
commit 487422eb99
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 47 additions and 1 deletions

View file

@ -144,6 +144,12 @@ impl Client {
Ok(ws) Ok(ws)
} }
/// leaves a [Workspace] by name
pub fn leave_workspace(&self, id: &str) -> bool {
self.workspaces.remove(id).is_some()
}
/// gets a [Workspace] by name
pub fn get_workspace(&self, id: &str) -> Option<Workspace> { pub fn get_workspace(&self, id: &str) -> Option<Workspace> {
self.workspaces.get(id).map(|x| x.clone()) self.workspaces.get(id).map(|x| x.clone())
} }

View file

@ -166,6 +166,27 @@ impl Workspace {
Ok(controller) Ok(controller)
} }
/// detach from an active buffer
///
/// this option will be carried in background: [buffer::worker::BufferWorker] will be stopped and dropped. there
/// may still be some events enqueued in buffers to poll, but the [buffer::Controller] itself won't be
/// accessible anymore from [Workspace].
///
/// ### returns
/// [DetachResult::NotAttached] if buffer wasn't attached in the first place
/// [DetachResult::Detaching] if detach was correctly requested
/// [DetachResult::AlreadyDetached] if worker is already stopped
pub fn detach(&self, path: &str) -> DetachResult {
match self.0.buffers.remove(path) {
None => DetachResult::NotAttached,
Some((_name, controller)) => if controller.stop() {
DetachResult::Detaching
} else {
DetachResult::AlreadyDetached
}
}
}
/// fetch a list of all buffers in a workspace /// fetch a list of all buffers in a workspace
pub async fn fetch_buffers(&self) -> crate::Result<()> { pub async fn fetch_buffers(&self) -> crate::Result<()> {
let mut workspace_client = self.0.services.workspace.clone(); let mut workspace_client = self.0.services.workspace.clone();
@ -268,3 +289,22 @@ impl Workspace {
self.0.filetree.iter().map(|f| f.clone()).collect() self.0.filetree.iter().map(|f| f.clone()).collect()
} }
} }
impl Drop for WorkspaceInner {
fn drop(&mut self) {
for entry in self.buffers.iter() {
if !entry.value().stop() {
tracing::warn!("could not stop buffer worker {} for workspace {}", entry.value().name(), self.id);
}
}
if !self.cursor.stop() {
tracing::warn!("could not stop cursor worker for workspace {}", self.id);
}
}
}
pub enum DetachResult {
NotAttached,
Detaching,
AlreadyDetached,
}