Unregister worktree when its last handle to it gets released
This commit is contained in:
parent
9505d6cdcf
commit
ee95775b1c
2 changed files with 85 additions and 26 deletions
|
@ -160,16 +160,13 @@ impl Project {
|
||||||
|
|
||||||
if let Some(project_id) = remote_id {
|
if let Some(project_id) = remote_id {
|
||||||
let mut registrations = Vec::new();
|
let mut registrations = Vec::new();
|
||||||
this.read_with(&cx, |this, cx| {
|
this.update(&mut cx, |this, cx| {
|
||||||
for worktree in &this.worktrees {
|
for worktree in &this.worktrees {
|
||||||
let worktree_id = worktree.id() as u64;
|
registrations.push(worktree.update(
|
||||||
let worktree = worktree.read(cx).as_local().unwrap();
|
cx,
|
||||||
registrations.push(rpc.request(
|
|worktree, cx| {
|
||||||
proto::RegisterWorktree {
|
let worktree = worktree.as_local_mut().unwrap();
|
||||||
project_id,
|
worktree.register(project_id, cx)
|
||||||
worktree_id,
|
|
||||||
root_name: worktree.root_name().to_string(),
|
|
||||||
authorized_logins: worktree.authorized_logins(),
|
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -407,7 +404,7 @@ impl Project {
|
||||||
for worktree in &this.worktrees {
|
for worktree in &this.worktrees {
|
||||||
worktree.update(cx, |worktree, cx| {
|
worktree.update(cx, |worktree, cx| {
|
||||||
let worktree = worktree.as_local_mut().unwrap();
|
let worktree = worktree.as_local_mut().unwrap();
|
||||||
tasks.push(worktree.share(project_id, cx));
|
tasks.push(worktree.share(cx));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -897,21 +894,15 @@ impl Project {
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(project_id) = remote_project_id {
|
if let Some(project_id) = remote_project_id {
|
||||||
let worktree_id = worktree.id() as u64;
|
worktree
|
||||||
let register_message = worktree.update(&mut cx, |worktree, _| {
|
.update(&mut cx, |worktree, cx| {
|
||||||
let worktree = worktree.as_local_mut().unwrap();
|
worktree.as_local_mut().unwrap().register(project_id, cx)
|
||||||
proto::RegisterWorktree {
|
})
|
||||||
project_id,
|
.await?;
|
||||||
worktree_id,
|
|
||||||
root_name: worktree.root_name().to_string(),
|
|
||||||
authorized_logins: worktree.authorized_logins(),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
client.request(register_message).await?;
|
|
||||||
if is_shared {
|
if is_shared {
|
||||||
worktree
|
worktree
|
||||||
.update(&mut cx, |worktree, cx| {
|
.update(&mut cx, |worktree, cx| {
|
||||||
worktree.as_local_mut().unwrap().share(project_id, cx)
|
worktree.as_local_mut().unwrap().share(cx)
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
@ -921,6 +912,12 @@ impl Project {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn remove_worktree(&mut self, id: WorktreeId, cx: &mut ModelContext<Self>) {
|
||||||
|
self.worktrees
|
||||||
|
.retain(|worktree| worktree.read(cx).id() != id);
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
|
|
||||||
fn add_worktree(&mut self, worktree: ModelHandle<Worktree>, cx: &mut ModelContext<Self>) {
|
fn add_worktree(&mut self, worktree: ModelHandle<Worktree>, cx: &mut ModelContext<Self>) {
|
||||||
cx.observe(&worktree, |_, _, cx| cx.notify()).detach();
|
cx.observe(&worktree, |_, _, cx| cx.notify()).detach();
|
||||||
self.worktrees.push(worktree);
|
self.worktrees.push(worktree);
|
||||||
|
@ -1104,9 +1101,7 @@ impl Project {
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
|
let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
|
||||||
self.worktrees
|
self.remove_worktree(worktree_id, cx);
|
||||||
.retain(|worktree| worktree.read(cx).as_remote().unwrap().id() != worktree_id);
|
|
||||||
cx.notify();
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,6 +66,24 @@ pub enum Worktree {
|
||||||
|
|
||||||
impl Entity for Worktree {
|
impl Entity for Worktree {
|
||||||
type Event = ();
|
type Event = ();
|
||||||
|
|
||||||
|
fn release(&mut self, cx: &mut MutableAppContext) {
|
||||||
|
if let Some(worktree) = self.as_local_mut() {
|
||||||
|
if let Registration::Done { project_id } = worktree.registration {
|
||||||
|
let client = worktree.client.clone();
|
||||||
|
let unregister_message = proto::UnregisterWorktree {
|
||||||
|
project_id,
|
||||||
|
worktree_id: worktree.id().to_proto(),
|
||||||
|
};
|
||||||
|
cx.foreground()
|
||||||
|
.spawn(async move {
|
||||||
|
client.send(unregister_message).await?;
|
||||||
|
Ok::<_, anyhow::Error>(())
|
||||||
|
})
|
||||||
|
.detach_and_log_err(cx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Worktree {
|
impl Worktree {
|
||||||
|
@ -747,6 +765,7 @@ pub struct LocalWorktree {
|
||||||
last_scan_state_rx: watch::Receiver<ScanState>,
|
last_scan_state_rx: watch::Receiver<ScanState>,
|
||||||
_background_scanner_task: Option<Task<()>>,
|
_background_scanner_task: Option<Task<()>>,
|
||||||
poll_task: Option<Task<()>>,
|
poll_task: Option<Task<()>>,
|
||||||
|
registration: Registration,
|
||||||
share: Option<ShareState>,
|
share: Option<ShareState>,
|
||||||
loading_buffers: LoadingBuffers,
|
loading_buffers: LoadingBuffers,
|
||||||
open_buffers: HashMap<usize, WeakModelHandle<Buffer>>,
|
open_buffers: HashMap<usize, WeakModelHandle<Buffer>>,
|
||||||
|
@ -759,6 +778,13 @@ pub struct LocalWorktree {
|
||||||
fs: Arc<dyn Fs>,
|
fs: Arc<dyn Fs>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
enum Registration {
|
||||||
|
None,
|
||||||
|
Pending,
|
||||||
|
Done { project_id: u64 },
|
||||||
|
}
|
||||||
|
|
||||||
struct ShareState {
|
struct ShareState {
|
||||||
project_id: u64,
|
project_id: u64,
|
||||||
snapshots_tx: Sender<Snapshot>,
|
snapshots_tx: Sender<Snapshot>,
|
||||||
|
@ -851,6 +877,7 @@ impl LocalWorktree {
|
||||||
background_snapshot: Arc::new(Mutex::new(snapshot)),
|
background_snapshot: Arc::new(Mutex::new(snapshot)),
|
||||||
last_scan_state_rx,
|
last_scan_state_rx,
|
||||||
_background_scanner_task: None,
|
_background_scanner_task: None,
|
||||||
|
registration: Registration::None,
|
||||||
share: None,
|
share: None,
|
||||||
poll_task: None,
|
poll_task: None,
|
||||||
loading_buffers: Default::default(),
|
loading_buffers: Default::default(),
|
||||||
|
@ -1316,11 +1343,48 @@ impl LocalWorktree {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn share(
|
pub fn register(
|
||||||
&mut self,
|
&mut self,
|
||||||
project_id: u64,
|
project_id: u64,
|
||||||
cx: &mut ModelContext<Worktree>,
|
cx: &mut ModelContext<Worktree>,
|
||||||
) -> Task<anyhow::Result<()>> {
|
) -> Task<anyhow::Result<()>> {
|
||||||
|
if self.registration != Registration::None {
|
||||||
|
return Task::ready(Ok(()));
|
||||||
|
}
|
||||||
|
|
||||||
|
self.registration = Registration::Pending;
|
||||||
|
let client = self.client.clone();
|
||||||
|
let register_message = proto::RegisterWorktree {
|
||||||
|
project_id,
|
||||||
|
worktree_id: self.id().to_proto(),
|
||||||
|
root_name: self.root_name().to_string(),
|
||||||
|
authorized_logins: self.authorized_logins(),
|
||||||
|
};
|
||||||
|
cx.spawn(|this, mut cx| async move {
|
||||||
|
let response = client.request(register_message).await;
|
||||||
|
this.update(&mut cx, |this, _| {
|
||||||
|
let worktree = this.as_local_mut().unwrap();
|
||||||
|
match response {
|
||||||
|
Ok(_) => {
|
||||||
|
worktree.registration = Registration::Done { project_id };
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
worktree.registration = Registration::None;
|
||||||
|
Err(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn share(&mut self, cx: &mut ModelContext<Worktree>) -> Task<anyhow::Result<()>> {
|
||||||
|
let project_id = if let Registration::Done { project_id } = self.registration {
|
||||||
|
project_id
|
||||||
|
} else {
|
||||||
|
return Task::ready(Err(anyhow!("cannot share worktree before registering it")));
|
||||||
|
};
|
||||||
|
|
||||||
if self.share.is_some() {
|
if self.share.is_some() {
|
||||||
return Task::ready(Ok(()));
|
return Task::ready(Ok(()));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue