Send worktree info only when sharing worktree

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-12-20 11:36:59 -08:00
parent 697e641e8e
commit 614ee4eac7
10 changed files with 200 additions and 208 deletions

View file

@ -213,7 +213,8 @@ impl Project {
subscriptions: vec![
client.subscribe_to_entity(remote_id, cx, Self::handle_add_collaborator),
client.subscribe_to_entity(remote_id, cx, Self::handle_remove_collaborator),
client.subscribe_to_entity(remote_id, cx, Self::handle_register_worktree),
client.subscribe_to_entity(remote_id, cx, Self::handle_share_worktree),
client.subscribe_to_entity(remote_id, cx, Self::handle_unregister_worktree),
client.subscribe_to_entity(remote_id, cx, Self::handle_update_worktree),
client.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer),
client.subscribe_to_entity(remote_id, cx, Self::handle_buffer_saved),
@ -231,14 +232,6 @@ impl Project {
*remote_id_tx.borrow_mut() = remote_id;
}
for worktree in &self.worktrees {
worktree.update(cx, |worktree, _| {
if let Some(worktree) = worktree.as_local_mut() {
worktree.set_project_remote_id(remote_id);
}
});
}
self.subscriptions.clear();
if let Some(remote_id) = remote_id {
self.subscriptions.extend([
@ -307,7 +300,11 @@ impl Project {
this.update(&mut cx, |this, cx| {
for worktree in &this.worktrees {
worktree.update(cx, |worktree, cx| {
worktree.as_local_mut().unwrap().share(cx).detach();
worktree
.as_local_mut()
.unwrap()
.share(remote_id, cx)
.detach();
});
}
});
@ -327,6 +324,13 @@ impl Project {
}
}
fn is_shared(&self) -> bool {
match &self.client_state {
ProjectClientState::Local { is_shared, .. } => *is_shared,
ProjectClientState::Remote { .. } => false,
}
}
pub fn add_local_worktree(
&mut self,
abs_path: &Path,
@ -337,32 +341,35 @@ impl Project {
let user_store = self.user_store.clone();
let languages = self.languages.clone();
let path = Arc::from(abs_path);
cx.spawn(|this, mut cx| async move {
cx.spawn(|project, mut cx| async move {
let worktree =
Worktree::open_local(client.clone(), user_store, path, fs, languages, &mut cx)
.await?;
this.update(&mut cx, |this, cx| {
if let Some(project_id) = this.remote_id() {
worktree.update(cx, |worktree, cx| {
let worktree = worktree.as_local_mut().unwrap();
worktree.set_project_remote_id(Some(project_id));
let serialized_worktree = worktree.to_proto(cx);
let authorized_logins = worktree.authorized_logins();
cx.foreground()
.spawn(async move {
client
.request(proto::RegisterWorktree {
project_id,
worktree: Some(serialized_worktree),
authorized_logins,
})
.log_err();
})
.detach();
});
}
this.add_worktree(worktree.clone(), cx);
let (remote_project_id, is_shared) = project.update(&mut cx, |project, cx| {
project.add_worktree(worktree.clone(), cx);
(project.remote_id(), project.is_shared())
});
if let Some(project_id) = remote_project_id {
let register_message = worktree.update(&mut cx, |worktree, _| {
let worktree = worktree.as_local_mut().unwrap();
proto::RegisterWorktree {
project_id,
root_name: worktree.root_name().to_string(),
authorized_logins: worktree.authorized_logins(),
}
});
client.request(register_message).await?;
if is_shared {
worktree
.update(&mut cx, |worktree, cx| {
worktree.as_local_mut().unwrap().share(project_id, cx)
})
.await?;
}
}
Ok(worktree)
})
}
@ -476,9 +483,9 @@ impl Project {
Ok(())
}
fn handle_register_worktree(
fn handle_share_worktree(
&mut self,
envelope: TypedEnvelope<proto::RegisterWorktree>,
envelope: TypedEnvelope<proto::ShareWorktree>,
client: Arc<Client>,
cx: &mut ModelContext<Self>,
) -> Result<()> {
@ -505,6 +512,19 @@ impl Project {
Ok(())
}
fn handle_unregister_worktree(
&mut self,
envelope: TypedEnvelope<proto::UnregisterWorktree>,
_: Arc<Client>,
cx: &mut ModelContext<Self>,
) -> Result<()> {
self.worktrees.retain(|worktree| {
worktree.read(cx).as_remote().unwrap().remote_id() != envelope.payload.worktree_id
});
cx.notify();
Ok(())
}
fn handle_update_worktree(
&mut self,
envelope: TypedEnvelope<proto::UpdateWorktree>,