Start on subscribing to messages in channel entity instances
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com> Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
co-authored by
Max Brunsfeld
Antonio Scandurra
parent
5b599a32b8
commit
541f58e12c
+49
-14
@@ -1,7 +1,8 @@
|
||||
use crate::rpc::{self, Client};
|
||||
use anyhow::{anyhow, Result};
|
||||
use futures::StreamExt;
|
||||
use gpui::{
|
||||
AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext, WeakModelHandle,
|
||||
AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext, Task, WeakModelHandle,
|
||||
};
|
||||
use std::{
|
||||
collections::{HashMap, VecDeque},
|
||||
@@ -24,6 +25,7 @@ pub struct Channel {
|
||||
first_message_id: Option<u64>,
|
||||
messages: Option<VecDeque<ChannelMessage>>,
|
||||
rpc: Arc<Client>,
|
||||
_receive_messages: Task<()>,
|
||||
}
|
||||
|
||||
pub struct ChannelMessage {
|
||||
@@ -43,19 +45,20 @@ impl ChannelList {
|
||||
) -> Self {
|
||||
// Subscribe to messages.
|
||||
let this = cx.handle().downgrade();
|
||||
rpc.on_message(
|
||||
router,
|
||||
|envelope, rpc, cx: &mut AsyncAppContext| async move {
|
||||
cx.update(|cx| {
|
||||
if let Some(this) = this.upgrade(cx) {
|
||||
this.update(cx, |this, cx| this.receive_message(envelope, cx))
|
||||
} else {
|
||||
Err(anyhow!("can't upgrade ChannelList handle"))
|
||||
}
|
||||
})
|
||||
},
|
||||
cx,
|
||||
);
|
||||
|
||||
// rpc.on_message(
|
||||
// router,
|
||||
// |envelope, rpc, cx: &mut AsyncAppContext| async move {
|
||||
// cx.update(|cx| {
|
||||
// if let Some(this) = this.upgrade(cx) {
|
||||
// this.update(cx, |this, cx| this.receive_message(envelope, cx))
|
||||
// } else {
|
||||
// Err(anyhow!("can't upgrade ChannelList handle"))
|
||||
// }
|
||||
// })
|
||||
// },
|
||||
// cx,
|
||||
// );
|
||||
|
||||
Self {
|
||||
available_channels: Default::default(),
|
||||
@@ -72,3 +75,35 @@ impl ChannelList {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Entity for Channel {
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
pub fn new(details: ChannelDetails, rpc: Arc<Client>, cx: &mut ModelContext<Self>) -> Self {
|
||||
let messages = rpc.subscribe();
|
||||
let receive_messages = cx.spawn_weak(|this, cx| async move {
|
||||
while let Some(message) = messages.next().await {
|
||||
if let Some(this) = this.upgrade(&cx) {
|
||||
this.update(&mut cx, |this, cx| this.message_received(&message, cx));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Self {
|
||||
details,
|
||||
rpc,
|
||||
first_message_id: None,
|
||||
messages: None,
|
||||
_receive_messages: receive_messages,
|
||||
}
|
||||
}
|
||||
|
||||
fn message_received(
|
||||
&mut self,
|
||||
message: &TypedEnvelope<ChannelMessageSent>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -2,6 +2,7 @@ use crate::{language::LanguageRegistry, worktree::Worktree};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use async_tungstenite::tungstenite::http::Request;
|
||||
use async_tungstenite::tungstenite::{Error as WebSocketError, Message as WebSocketMessage};
|
||||
use futures::Stream;
|
||||
use gpui::{AsyncAppContext, ModelHandle, Task, WeakModelHandle};
|
||||
use lazy_static::lazy_static;
|
||||
use smol::lock::RwLock;
|
||||
@@ -29,7 +30,6 @@ pub struct Client {
|
||||
pub struct ClientState {
|
||||
connection_id: Option<ConnectionId>,
|
||||
pub shared_worktrees: HashMap<u64, WeakModelHandle<Worktree>>,
|
||||
pub channel_list: Option<WeakModelHandle<ChannelList>>,
|
||||
pub languages: Arc<LanguageRegistry>,
|
||||
}
|
||||
|
||||
@@ -82,6 +82,10 @@ impl Client {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn subscribe<T: EnvelopedMessage>(&self) -> impl Stream<Item = Arc<TypedEnvelope<T>>> {
|
||||
self.peer.subscribe()
|
||||
}
|
||||
|
||||
pub async fn log_in_and_connect(
|
||||
&self,
|
||||
router: Arc<ForegroundRouter>,
|
||||
|
||||
Reference in New Issue
Block a user