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:
Nathan Sobo
2021-08-18 11:45:29 -06:00
co-authored by Max Brunsfeld Antonio Scandurra
parent 5b599a32b8
commit 541f58e12c
5 changed files with 126 additions and 29 deletions
+13 -5
View File
@@ -3,15 +3,15 @@ use anyhow::{anyhow, Context, Result};
use async_lock::{Mutex, RwLock};
use async_tungstenite::tungstenite::{Error as WebSocketError, Message as WebSocketMessage};
use futures::{
future::{BoxFuture, LocalBoxFuture},
FutureExt, StreamExt,
future::{self, BoxFuture, LocalBoxFuture},
FutureExt, Stream, StreamExt,
};
use postage::{
mpsc,
prelude::{Sink, Stream},
broadcast, mpsc,
prelude::{Sink as _, Stream as _},
};
use std::{
any::TypeId,
any::{Any, TypeId},
collections::{HashMap, HashSet},
fmt,
future::Future,
@@ -77,6 +77,7 @@ pub struct RouterInternal<H> {
pub struct Peer {
connections: RwLock<HashMap<ConnectionId, Connection>>,
next_connection_id: AtomicU32,
incoming_messages: broadcast::Sender<Arc<dyn Any + Send + Sync>>,
}
#[derive(Clone)]
@@ -91,6 +92,7 @@ impl Peer {
Arc::new(Self {
connections: Default::default(),
next_connection_id: Default::default(),
incoming_messages: broadcast::channel(256).0,
})
}
@@ -189,6 +191,12 @@ impl Peer {
self.connections.write().await.clear();
}
pub fn subscribe<T: EnvelopedMessage>(&self) -> impl Stream<Item = Arc<TypedEnvelope<T>>> {
self.incoming_messages
.subscribe()
.filter_map(|envelope| future::ready(Arc::downcast(envelope).ok()))
}
pub fn request<T: RequestMessage>(
self: &Arc<Self>,
receiver_id: ConnectionId,
+1 -1
View File
@@ -8,7 +8,7 @@ use std::{
include!(concat!(env!("OUT_DIR"), "/zed.messages.rs"));
pub trait EnvelopedMessage: Clone + Sized + Send + 'static {
pub trait EnvelopedMessage: Clone + Sized + Send + Sync + 'static {
const NAME: &'static str;
fn into_envelope(
self,