feat: make SourceStream more reusable

This commit is contained in:
Lucas Colombo 2024-06-04 10:43:44 -03:00
parent ab8bd69ff4
commit 3393edfd90
Signed by: lucas
GPG Key ID: EF34786CFEFFAE35
3 changed files with 9 additions and 6 deletions

View File

@ -1,6 +1,7 @@
use std::{fmt::Debug, pin::Pin};
use futures::Stream;
use redis::stream::StreamMsg;
use {eyre::Result, tonic::async_trait};
@ -24,7 +25,7 @@ pub trait ToFromBusMessage {
/// 🐎 » supertrait combining all bus object traits + debug + send + sync + 'static
pub trait BusMessage:
ToBusVal + ToBusKey + ToFromBusMessage + Debug + Clone + Send + Sync + PartialEq + 'static
ToBusVal + ToBusKey + ToFromBusMessage + Debug + PartialEq + StreamMsg + 'static
{
}

View File

@ -6,19 +6,20 @@ use {
tokio::sync::broadcast::{self, Sender},
};
use crate::bus::BusMessage;
pub trait StreamMsg: Clone + Send + Sync + 'static {}
pub struct SourceStream<RM: BusMessage> {
// TODO: move this to a separate module, or maybe to the lool library
pub struct SourceStream<RM: StreamMsg> {
sender: Option<Sender<RM>>,
}
impl<RM: BusMessage> Default for SourceStream<RM> {
impl<RM: StreamMsg> Default for SourceStream<RM> {
fn default() -> Self {
Self::new()
}
}
impl<RM: BusMessage> SourceStream<RM> {
impl<RM: StreamMsg> SourceStream<RM> {
// Create a new SourceStream with a broadcast channel
pub fn new() -> Self {
let (sender, _) = broadcast::channel(100); // Adjust the buffer size as needed

View File

@ -4,7 +4,7 @@ pub extern crate eyre;
use {
super::svc::RustlerMsg,
crate::{
bus::{BusMessage, ToBusKey, ToBusVal, ToFromBusMessage},
bus::{redis::stream::StreamMsg, BusMessage, ToBusKey, ToBusVal, ToFromBusMessage},
entities::{market, ticker},
},
async_trait::async_trait,
@ -163,6 +163,7 @@ impl PartialEq<Quote> for Quote {
}
}
impl StreamMsg for Quote {}
impl BusMessage for Quote {}
#[derive(Debug, Clone)]