Rename `subscribe` to `add_{message,request}_handler` in `Client`

Antonio Scandurra created

This makes it easier to distinguish between messages and requests.

Change summary

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(-)

Detailed changes

crates/client/src/channel.rs 🔗

@@ -184,7 +184,8 @@ impl Channel {
         rpc: Arc<Client>,
         cx: &mut ModelContext<Self>,
     ) -> 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();

crates/client/src/client.rs 🔗

@@ -266,7 +266,7 @@ impl Client {
         }
     }
 
-    pub fn subscribe<T, M, F, Fut>(
+    pub fn add_message_handler<T, M, F, Fut>(
         self: &Arc<Self>,
         cx: &mut ModelContext<M>,
         mut handler: F,
@@ -311,7 +311,7 @@ impl Client {
         }
     }
 
-    pub fn subscribe_to_entity<T, M, F, Fut>(
+    pub fn add_entity_message_handler<T, M, F, Fut>(
         self: &Arc<Self>,
         remote_id: u64,
         cx: &mut ModelContext<M>,
@@ -369,7 +369,7 @@ impl Client {
         }
     }
 
-    pub fn subscribe_to_entity_request<T, M, F, Fut>(
+    pub fn add_entity_request_handler<T, M, F, Fut>(
         self: &Arc<Self>,
         remote_id: u64,
         cx: &mut ModelContext<M>,
@@ -384,7 +384,7 @@ impl Client {
             + FnMut(ModelHandle<M>, TypedEnvelope<T>, Arc<Self>, AsyncAppContext) -> Fut,
         Fut: 'static + Future<Output = Result<T::Response>>,
     {
-        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<proto::UnshareProject>, _, _| {
@@ -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<proto::UnshareProject>, _, _| {
@@ -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<proto::UnshareProject>, _, _| 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<proto::Ping>, _, _| {
+            client.add_message_handler(cx, move |_, _: TypedEnvelope<proto::Ping>, _, _| {
                 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<proto::Ping>, _, _| {
+            client.add_message_handler(cx, move |_, _: TypedEnvelope<proto::Ping>, _, _| {
                 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<proto::Ping>, _, mut cx| {
                     model.update(&mut cx, |model, _| model.subscription.take());

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::<Option<proto::UpdateContacts>>();
-        let update_contacts_subscription = client.subscribe(
+        let update_contacts_subscription = client.add_message_handler(
             cx,
             move |_: ModelHandle<Self>, msg: TypedEnvelope<proto::UpdateContacts>, _, _| {
                 *update_contacts_tx.borrow_mut() = Some(msg.payload);

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),
             ]);
         }
     }