From 1aff42302c228e4aec62e5a46d5f60f963d07ea2 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 14 Feb 2022 09:25:31 +0100 Subject: [PATCH] Rename `subscribe` to `add_{message,request}_handler` in `Client` This makes it easier to distinguish between messages and requests. --- crates/client/src/channel.rs | 3 +- crates/client/src/client.rs | 20 +++++------ crates/client/src/user.rs | 2 +- crates/project/src/project.rs | 66 +++++++++++++++++++++-------------- 4 files changed, 52 insertions(+), 39 deletions(-) diff --git a/crates/client/src/channel.rs b/crates/client/src/channel.rs index b508e36016..ab65b4d228 100644 --- a/crates/client/src/channel.rs +++ b/crates/client/src/channel.rs @@ -184,7 +184,8 @@ impl Channel { rpc: Arc, cx: &mut ModelContext, ) -> Self { - let _subscription = rpc.subscribe_to_entity(details.id, cx, Self::handle_message_sent); + let _subscription = + rpc.add_entity_message_handler(details.id, cx, Self::handle_message_sent); { let user_store = user_store.clone(); diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index a81de99649..44f81f3345 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -266,7 +266,7 @@ impl Client { } } - pub fn subscribe( + pub fn add_message_handler( self: &Arc, cx: &mut ModelContext, mut handler: F, @@ -311,7 +311,7 @@ impl Client { } } - pub fn subscribe_to_entity( + pub fn add_entity_message_handler( self: &Arc, remote_id: u64, cx: &mut ModelContext, @@ -369,7 +369,7 @@ impl Client { } } - pub fn subscribe_to_entity_request( + pub fn add_entity_request_handler( self: &Arc, remote_id: u64, cx: &mut ModelContext, @@ -384,7 +384,7 @@ impl Client { + FnMut(ModelHandle, TypedEnvelope, Arc, AsyncAppContext) -> Fut, Fut: 'static + Future>, { - self.subscribe_to_entity(remote_id, cx, move |model, envelope, client, cx| { + self.add_entity_message_handler(remote_id, cx, move |model, envelope, client, cx| { let receipt = envelope.receipt(); let response = handler(model, envelope, client.clone(), cx); async move { @@ -927,7 +927,7 @@ mod tests { let (mut done_tx1, mut done_rx1) = postage::oneshot::channel(); let (mut done_tx2, mut done_rx2) = postage::oneshot::channel(); let _subscription1 = model.update(&mut cx, |_, cx| { - client.subscribe_to_entity( + client.add_entity_message_handler( 1, cx, move |_, _: TypedEnvelope, _, _| { @@ -937,7 +937,7 @@ mod tests { ) }); let _subscription2 = model.update(&mut cx, |_, cx| { - client.subscribe_to_entity( + client.add_entity_message_handler( 2, cx, move |_, _: TypedEnvelope, _, _| { @@ -950,7 +950,7 @@ mod tests { // Ensure dropping a subscription for the same entity type still allows receiving of // messages for other entity IDs of the same type. let subscription3 = model.update(&mut cx, |_, cx| { - client.subscribe_to_entity( + client.add_entity_message_handler( 3, cx, |_, _: TypedEnvelope, _, _| async { Ok(()) }, @@ -976,14 +976,14 @@ mod tests { let (mut done_tx1, _done_rx1) = postage::oneshot::channel(); let (mut done_tx2, mut done_rx2) = postage::oneshot::channel(); let subscription1 = model.update(&mut cx, |_, cx| { - client.subscribe(cx, move |_, _: TypedEnvelope, _, _| { + client.add_message_handler(cx, move |_, _: TypedEnvelope, _, _| { postage::sink::Sink::try_send(&mut done_tx1, ()).unwrap(); async { Ok(()) } }) }); drop(subscription1); let _subscription2 = model.update(&mut cx, |_, cx| { - client.subscribe(cx, move |_, _: TypedEnvelope, _, _| { + client.add_message_handler(cx, move |_, _: TypedEnvelope, _, _| { postage::sink::Sink::try_send(&mut done_tx2, ()).unwrap(); async { Ok(()) } }) @@ -1003,7 +1003,7 @@ mod tests { let model = cx.add_model(|_| Model { subscription: None }); let (mut done_tx, mut done_rx) = postage::oneshot::channel(); model.update(&mut cx, |model, cx| { - model.subscription = Some(client.subscribe( + model.subscription = Some(client.add_message_handler( cx, move |model, _: TypedEnvelope, _, mut cx| { model.update(&mut cx, |model, _| model.subscription.take()); diff --git a/crates/client/src/user.rs b/crates/client/src/user.rs index 10388e00bf..1e4f7fe4d4 100644 --- a/crates/client/src/user.rs +++ b/crates/client/src/user.rs @@ -58,7 +58,7 @@ impl UserStore { let (mut current_user_tx, current_user_rx) = watch::channel(); let (mut update_contacts_tx, mut update_contacts_rx) = watch::channel::>(); - let update_contacts_subscription = client.subscribe( + let update_contacts_subscription = client.add_message_handler( cx, move |_: ModelHandle, msg: TypedEnvelope, _, _| { *update_contacts_tx.borrow_mut() = Some(msg.payload); diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 29478d2e2c..8f94b3dbba 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -280,31 +280,43 @@ impl Project { user_store, fs, subscriptions: vec![ - client.subscribe_to_entity(remote_id, cx, Self::handle_unshare_project), - 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_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( + client.add_entity_message_handler(remote_id, cx, Self::handle_unshare_project), + client.add_entity_message_handler(remote_id, cx, Self::handle_add_collaborator), + client.add_entity_message_handler( + remote_id, + cx, + Self::handle_remove_collaborator, + ), + client.add_entity_message_handler(remote_id, cx, Self::handle_share_worktree), + client.add_entity_message_handler( + remote_id, + cx, + Self::handle_unregister_worktree, + ), + client.add_entity_message_handler(remote_id, cx, Self::handle_update_worktree), + client.add_entity_message_handler( remote_id, cx, Self::handle_update_diagnostic_summary, ), - client.subscribe_to_entity( + client.add_entity_message_handler( remote_id, cx, Self::handle_disk_based_diagnostics_updating, ), - client.subscribe_to_entity( + client.add_entity_message_handler( remote_id, cx, Self::handle_disk_based_diagnostics_updated, ), - client.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer), - client.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer_file), - client.subscribe_to_entity(remote_id, cx, Self::handle_buffer_reloaded), - client.subscribe_to_entity(remote_id, cx, Self::handle_buffer_saved), + client.add_entity_message_handler(remote_id, cx, Self::handle_update_buffer), + client.add_entity_message_handler( + remote_id, + cx, + Self::handle_update_buffer_file, + ), + client.add_entity_message_handler(remote_id, cx, Self::handle_buffer_reloaded), + client.add_entity_message_handler(remote_id, cx, Self::handle_buffer_saved), ], client, client_state: ProjectClientState::Remote { @@ -340,24 +352,24 @@ impl Project { if let Some(remote_id) = remote_id { let client = &self.client; self.subscriptions.extend([ - client.subscribe_to_entity_request(remote_id, cx, Self::handle_open_buffer), - client.subscribe_to_entity(remote_id, cx, Self::handle_close_buffer), - 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_update_worktree), - client.subscribe_to_entity(remote_id, cx, Self::handle_update_buffer), - client.subscribe_to_entity_request(remote_id, cx, Self::handle_save_buffer), - client.subscribe_to_entity(remote_id, cx, Self::handle_buffer_saved), - client.subscribe_to_entity_request(remote_id, cx, Self::handle_format_buffers), - client.subscribe_to_entity_request(remote_id, cx, Self::handle_get_completions), - client.subscribe_to_entity_request( + client.add_entity_request_handler(remote_id, cx, Self::handle_open_buffer), + client.add_entity_message_handler(remote_id, cx, Self::handle_close_buffer), + client.add_entity_message_handler(remote_id, cx, Self::handle_add_collaborator), + client.add_entity_message_handler(remote_id, cx, Self::handle_remove_collaborator), + client.add_entity_message_handler(remote_id, cx, Self::handle_update_worktree), + client.add_entity_message_handler(remote_id, cx, Self::handle_update_buffer), + client.add_entity_request_handler(remote_id, cx, Self::handle_save_buffer), + client.add_entity_message_handler(remote_id, cx, Self::handle_buffer_saved), + client.add_entity_request_handler(remote_id, cx, Self::handle_format_buffers), + client.add_entity_request_handler(remote_id, cx, Self::handle_get_completions), + client.add_entity_request_handler( remote_id, cx, Self::handle_apply_additional_edits_for_completion, ), - client.subscribe_to_entity_request(remote_id, cx, Self::handle_get_code_actions), - client.subscribe_to_entity_request(remote_id, cx, Self::handle_apply_code_action), - client.subscribe_to_entity_request(remote_id, cx, Self::handle_get_definition), + client.add_entity_request_handler(remote_id, cx, Self::handle_get_code_actions), + client.add_entity_request_handler(remote_id, cx, Self::handle_apply_code_action), + client.add_entity_request_handler(remote_id, cx, Self::handle_get_definition), ]); } }