Share new worktrees when resharing project

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-01-06 12:58:19 -07:00
parent 585c23e9f6
commit 8487ae77e7
5 changed files with 54 additions and 70 deletions

View file

@ -1037,7 +1037,7 @@ impl Project {
if update_project.is_ok() {
for worktree in worktrees {
worktree.update(&mut cx, |worktree, cx| {
let worktree = &mut worktree.as_local_mut().unwrap();
let worktree = worktree.as_local_mut().unwrap();
worktree.share(project_id, cx).detach_and_log_err(cx)
});
}
@ -1062,17 +1062,7 @@ impl Project {
cx: &mut ModelContext<Self>,
) -> Result<()> {
self.set_collaborators_from_proto(message.collaborators, cx)?;
for worktree in self.worktrees.iter() {
if let Some(worktree) = worktree.upgrade(&cx) {
worktree.update(cx, |worktree, _| {
if let Some(worktree) = worktree.as_local_mut() {
worktree.reshare()
} else {
Ok(())
}
})?;
}
}
let _ = self.metadata_changed(cx);
Ok(())
}
@ -6259,18 +6249,14 @@ impl Entity for Project {
fn release(&mut self, _: &mut gpui::MutableAppContext) {
match &self.client_state {
Some(ProjectClientState::Local { remote_id, .. }) => {
self.client
.send(proto::UnshareProject {
project_id: *remote_id,
})
.log_err();
let _ = self.client.send(proto::UnshareProject {
project_id: *remote_id,
});
}
Some(ProjectClientState::Remote { remote_id, .. }) => {
self.client
.send(proto::LeaveProject {
project_id: *remote_id,
})
.log_err();
let _ = self.client.send(proto::LeaveProject {
project_id: *remote_id,
});
}
_ => {}
}

View file

@ -166,7 +166,7 @@ enum ScanState {
struct ShareState {
project_id: u64,
snapshots_tx: watch::Sender<LocalSnapshot>,
reshared: watch::Sender<()>,
resume_updates: watch::Sender<()>,
_maintain_remote_snapshot: Task<Option<()>>,
}
@ -975,12 +975,12 @@ impl LocalWorktree {
pub fn share(&mut self, project_id: u64, cx: &mut ModelContext<Worktree>) -> Task<Result<()>> {
let (share_tx, share_rx) = oneshot::channel();
if self.share.is_some() {
if let Some(share) = self.share.as_mut() {
let _ = share_tx.send(());
*share.resume_updates.borrow_mut() = ();
} else {
let (snapshots_tx, mut snapshots_rx) = watch::channel_with(self.snapshot());
let (reshared_tx, mut reshared_rx) = watch::channel();
let _ = reshared_rx.try_recv();
let (resume_updates_tx, mut resume_updates_rx) = watch::channel();
let worktree_id = cx.model_id() as u64;
for (path, summary) in self.diagnostic_summaries.iter() {
@ -1022,10 +1022,11 @@ impl LocalWorktree {
let update =
snapshot.build_update(&prev_snapshot, project_id, worktree_id, true);
for update in proto::split_worktree_update(update, MAX_CHUNK_SIZE) {
let _ = resume_updates_rx.try_recv();
while let Err(error) = client.request(update.clone()).await {
log::error!("failed to send worktree update: {}", error);
log::info!("waiting for worktree to be reshared");
if reshared_rx.next().await.is_none() {
log::info!("waiting to resume updates");
if resume_updates_rx.next().await.is_none() {
return Ok(());
}
}
@ -1046,7 +1047,7 @@ impl LocalWorktree {
self.share = Some(ShareState {
project_id,
snapshots_tx,
reshared: reshared_tx,
resume_updates: resume_updates_tx,
_maintain_remote_snapshot,
});
}
@ -1059,15 +1060,6 @@ impl LocalWorktree {
self.share.take();
}
pub fn reshare(&mut self) -> Result<()> {
let share = self
.share
.as_mut()
.ok_or_else(|| anyhow!("can't reshare a worktree that wasn't shared"))?;
*share.reshared.borrow_mut() = ();
Ok(())
}
pub fn is_shared(&self) -> bool {
self.share.is_some()
}