Send LeaveChannel message in Entity::release instead of Drop::drop

Max Brunsfeld created

Change summary

zed/src/channel.rs | 42 +++++++++++++++++++-----------------------
1 file changed, 19 insertions(+), 23 deletions(-)

Detailed changes

zed/src/channel.rs 🔗

@@ -1,8 +1,7 @@
 use crate::rpc::{self, Client};
 use anyhow::{Context, Result};
 use gpui::{
-    executor, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
-    WeakModelHandle,
+    AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext, WeakModelHandle,
 };
 use std::{
     collections::{hash_map, HashMap, VecDeque},
@@ -31,7 +30,6 @@ pub struct Channel {
     messages: Option<VecDeque<ChannelMessage>>,
     rpc: Arc<Client>,
     _subscription: rpc::Subscription,
-    background: Arc<executor::Background>,
 }
 
 pub struct ChannelMessage {
@@ -88,6 +86,19 @@ impl ChannelList {
 
 impl Entity for Channel {
     type Event = ();
+
+    // TODO: Implement the server side of leaving a channel
+    fn release(&mut self, cx: &mut MutableAppContext) {
+        let rpc = self.rpc.clone();
+        let channel_id = self.details.id;
+        cx.foreground()
+            .spawn(async move {
+                if let Err(error) = rpc.send(proto::LeaveChannel { channel_id }).await {
+                    log::error!("error leaving channel: {}", error);
+                };
+            })
+            .detach()
+    }
 }
 
 impl Channel {
@@ -118,10 +129,14 @@ impl Channel {
             first_message_id: None,
             messages: None,
             _subscription,
-            background: cx.background().clone(),
         }
     }
 
+
+    pub fn messages(&self) -> Option<&VecDeque<ChannelMessage>> {
+        self.messages.as_ref()
+    }
+
     fn handle_message_sent(
         &mut self,
         message: TypedEnvelope<ChannelMessageSent>,
@@ -130,25 +145,6 @@ impl Channel {
     ) -> Result<()> {
         Ok(())
     }
-
-    pub fn messages(&self) -> Option<&VecDeque<ChannelMessage>> {
-        self.messages.as_ref()
-    }
-}
-
-// TODO: Implement the server side of leaving a channel
-impl Drop for Channel {
-    fn drop(&mut self) {
-        let rpc = self.rpc.clone();
-        let channel_id = self.details.id;
-        self.background
-            .spawn(async move {
-                if let Err(error) = rpc.send(proto::LeaveChannel { channel_id }).await {
-                    log::error!("error leaving channel: {}", error);
-                };
-            })
-            .detach()
-    }
 }
 
 impl From<proto::Channel> for ChannelDetails {