Stop following when leader disconnects

This commit is contained in:
Antonio Scandurra 2022-03-22 11:15:39 +01:00
parent ffaf409a31
commit e5a99cf8cd
3 changed files with 60 additions and 10 deletions

View file

@ -125,6 +125,7 @@ pub enum Event {
DiskBasedDiagnosticsFinished, DiskBasedDiagnosticsFinished,
DiagnosticsUpdated(ProjectPath), DiagnosticsUpdated(ProjectPath),
RemoteIdChanged(Option<u64>), RemoteIdChanged(Option<u64>),
CollaboratorLeft(PeerId),
} }
enum LanguageServerEvent { enum LanguageServerEvent {
@ -3368,6 +3369,7 @@ impl Project {
buffer.update(cx, |buffer, cx| buffer.remove_peer(replica_id, cx)); buffer.update(cx, |buffer, cx| buffer.remove_peer(replica_id, cx));
} }
} }
cx.emit(Event::CollaboratorLeft(peer_id));
cx.notify(); cx.notify();
Ok(()) Ok(())
}) })

View file

@ -4259,6 +4259,7 @@ mod tests {
// Client A opens some editors. // Client A opens some editors.
let workspace_a = client_a.build_workspace(&project_a, cx_a); let workspace_a = client_a.build_workspace(&project_a, cx_a);
let pane_a = workspace_a.read_with(cx_a, |workspace, _| workspace.active_pane().clone());
let editor_a1 = workspace_a let editor_a1 = workspace_a
.update(cx_a, |workspace, cx| { .update(cx_a, |workspace, cx| {
workspace.open_path((worktree_id, "1.txt"), cx) workspace.open_path((worktree_id, "1.txt"), cx)
@ -4287,19 +4288,19 @@ mod tests {
.downcast::<Editor>() .downcast::<Editor>()
.unwrap(); .unwrap();
let client_a_id = project_b.read_with(cx_b, |project, _| {
project.collaborators().values().next().unwrap().peer_id
});
let client_b_id = project_a.read_with(cx_a, |project, _| {
project.collaborators().values().next().unwrap().peer_id
});
// When client B starts following client A, all visible view states are replicated to client B. // When client B starts following client A, all visible view states are replicated to client B.
editor_a1.update(cx_a, |editor, cx| editor.select_ranges([0..1], None, cx)); editor_a1.update(cx_a, |editor, cx| editor.select_ranges([0..1], None, cx));
editor_a2.update(cx_a, |editor, cx| editor.select_ranges([2..3], None, cx)); editor_a2.update(cx_a, |editor, cx| editor.select_ranges([2..3], None, cx));
workspace_b workspace_b
.update(cx_b, |workspace, cx| { .update(cx_b, |workspace, cx| {
let leader_id = project_b workspace.toggle_follow(&client_a_id.into(), cx).unwrap()
.read(cx)
.collaborators()
.values()
.next()
.unwrap()
.peer_id;
workspace.toggle_follow(&leader_id.into(), cx).unwrap()
}) })
.await .await
.unwrap(); .unwrap();
@ -4370,6 +4371,33 @@ mod tests {
.id()), .id()),
editor_b1.id() editor_b1.id()
); );
// Client A starts following client B.
workspace_a
.update(cx_a, |workspace, cx| {
workspace.toggle_follow(&client_b_id.into(), cx).unwrap()
})
.await
.unwrap();
assert_eq!(
workspace_a.read_with(cx_a, |workspace, _| workspace.leader_for_pane(&pane_a)),
Some(client_b_id)
);
assert_eq!(
workspace_a.read_with(cx_a, |workspace, cx| workspace
.active_item(cx)
.unwrap()
.id()),
editor_a1.id()
);
// Following interrupts when client B disconnects.
client_b.disconnect(&cx_b.to_async()).unwrap();
cx_a.foreground().run_until_parked();
assert_eq!(
workspace_a.read_with(cx_a, |workspace, _| workspace.leader_for_pane(&pane_a)),
None
);
} }
#[gpui::test(iterations = 10)] #[gpui::test(iterations = 10)]

View file

@ -667,9 +667,15 @@ impl Workspace {
.detach(); .detach();
cx.subscribe(&params.project, move |this, project, event, cx| { cx.subscribe(&params.project, move |this, project, event, cx| {
if let project::Event::RemoteIdChanged(remote_id) = event { match event {
project::Event::RemoteIdChanged(remote_id) => {
this.project_remote_id_changed(*remote_id, cx); this.project_remote_id_changed(*remote_id, cx);
} }
project::Event::CollaboratorLeft(peer_id) => {
this.collaborator_left(*peer_id, cx);
}
_ => {}
}
if project.read(cx).is_read_only() { if project.read(cx).is_read_only() {
cx.blur(); cx.blur();
} }
@ -1241,6 +1247,20 @@ impl Workspace {
} }
} }
fn collaborator_left(&mut self, peer_id: PeerId, cx: &mut ViewContext<Self>) {
self.leader_state.followers.remove(&peer_id);
if let Some(states_by_pane) = self.follower_states_by_leader.remove(&peer_id) {
for state in states_by_pane.into_values() {
for item in state.items_by_leader_view_id.into_values() {
if let FollowerItem::Loaded(item) = item {
item.set_leader_replica_id(None, cx);
}
}
}
}
cx.notify();
}
pub fn toggle_follow( pub fn toggle_follow(
&mut self, &mut self,
ToggleFollow(leader_id): &ToggleFollow, ToggleFollow(leader_id): &ToggleFollow,