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

This commit is contained in:
Max Brunsfeld 2021-08-19 17:27:21 -07:00
parent 467512eedd
commit c28a366e4a

View file

@ -1,8 +1,7 @@
use crate::rpc::{self, Client}; use crate::rpc::{self, Client};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use gpui::{ use gpui::{
executor, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext, WeakModelHandle,
WeakModelHandle,
}; };
use std::{ use std::{
collections::{hash_map, HashMap, VecDeque}, collections::{hash_map, HashMap, VecDeque},
@ -31,7 +30,6 @@ pub struct Channel {
messages: Option<VecDeque<ChannelMessage>>, messages: Option<VecDeque<ChannelMessage>>,
rpc: Arc<Client>, rpc: Arc<Client>,
_subscription: rpc::Subscription, _subscription: rpc::Subscription,
background: Arc<executor::Background>,
} }
pub struct ChannelMessage { pub struct ChannelMessage {
@ -88,6 +86,19 @@ impl ChannelList {
impl Entity for Channel { impl Entity for Channel {
type Event = (); 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 { impl Channel {
@ -118,10 +129,14 @@ impl Channel {
first_message_id: None, first_message_id: None,
messages: None, messages: None,
_subscription, _subscription,
background: cx.background().clone(),
} }
} }
pub fn messages(&self) -> Option<&VecDeque<ChannelMessage>> {
self.messages.as_ref()
}
fn handle_message_sent( fn handle_message_sent(
&mut self, &mut self,
message: TypedEnvelope<ChannelMessageSent>, message: TypedEnvelope<ChannelMessageSent>,
@ -130,25 +145,6 @@ impl Channel {
) -> Result<()> { ) -> Result<()> {
Ok(()) 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 { impl From<proto::Channel> for ChannelDetails {