Remove weak handles when worktree gets dropped
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
e5662dd426
commit
2fcf1aee6b
2 changed files with 20 additions and 10 deletions
|
@ -301,7 +301,7 @@ impl Project {
|
||||||
language_servers: Default::default(),
|
language_servers: Default::default(),
|
||||||
};
|
};
|
||||||
for worktree in worktrees {
|
for worktree in worktrees {
|
||||||
this.add_worktree(&worktree, false, cx);
|
this.add_worktree(&worktree, cx);
|
||||||
}
|
}
|
||||||
this
|
this
|
||||||
}))
|
}))
|
||||||
|
@ -902,7 +902,7 @@ impl Project {
|
||||||
Worktree::open_local(client.clone(), user_store, path, weak, fs, &mut cx).await?;
|
Worktree::open_local(client.clone(), user_store, path, weak, fs, &mut cx).await?;
|
||||||
|
|
||||||
let (remote_project_id, is_shared) = project.update(&mut cx, |project, cx| {
|
let (remote_project_id, is_shared) = project.update(&mut cx, |project, cx| {
|
||||||
project.add_worktree(&worktree, weak, cx);
|
project.add_worktree(&worktree, cx);
|
||||||
(project.remote_id(), project.is_shared())
|
(project.remote_id(), project.is_shared())
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -934,14 +934,20 @@ impl Project {
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_worktree(
|
fn add_worktree(&mut self, worktree: &ModelHandle<Worktree>, cx: &mut ModelContext<Self>) {
|
||||||
&mut self,
|
|
||||||
worktree: &ModelHandle<Worktree>,
|
|
||||||
weak: bool,
|
|
||||||
cx: &mut ModelContext<Self>,
|
|
||||||
) {
|
|
||||||
cx.observe(&worktree, |_, _, cx| cx.notify()).detach();
|
cx.observe(&worktree, |_, _, cx| cx.notify()).detach();
|
||||||
if weak {
|
|
||||||
|
let push_weak_handle = {
|
||||||
|
let worktree = worktree.read(cx);
|
||||||
|
worktree.is_local() && worktree.is_weak()
|
||||||
|
};
|
||||||
|
if push_weak_handle {
|
||||||
|
cx.observe_release(&worktree, |this, cx| {
|
||||||
|
this.worktrees
|
||||||
|
.retain(|worktree| worktree.upgrade(cx).is_some());
|
||||||
|
cx.notify();
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
self.worktrees
|
self.worktrees
|
||||||
.push(WorktreeHandle::Weak(worktree.downgrade()));
|
.push(WorktreeHandle::Weak(worktree.downgrade()));
|
||||||
} else {
|
} else {
|
||||||
|
@ -1112,7 +1118,7 @@ impl Project {
|
||||||
let worktree =
|
let worktree =
|
||||||
Worktree::remote(remote_id, replica_id, worktree, client, user_store, &mut cx)
|
Worktree::remote(remote_id, replica_id, worktree, client, user_store, &mut cx)
|
||||||
.await?;
|
.await?;
|
||||||
this.update(&mut cx, |this, cx| this.add_worktree(&worktree, false, cx));
|
this.update(&mut cx, |this, cx| this.add_worktree(&worktree, cx));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
.log_err()
|
.log_err()
|
||||||
|
|
|
@ -267,6 +267,10 @@ impl Worktree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_local(&self) -> bool {
|
||||||
|
matches!(self, Worktree::Local(_))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn snapshot(&self) -> Snapshot {
|
pub fn snapshot(&self) -> Snapshot {
|
||||||
match self {
|
match self {
|
||||||
Worktree::Local(worktree) => worktree.snapshot(),
|
Worktree::Local(worktree) => worktree.snapshot(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue