Skip to content

Commit

Permalink
Refactor WebSocket struct and Feeder struct
Browse files Browse the repository at this point in the history
Add Debug trait to WebSocket and Feeder structs
  • Loading branch information
nguyenthdat committed Jan 4, 2024
1 parent fd634d0 commit 6323b6f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/chart/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use crate::{
};
use async_trait::async_trait;
use serde_json::Value;
use std::fmt::Debug;

#[derive(Clone)]
pub struct WebSocket<T> {
pub struct WebSocket<T: Clone + Debug + Send + Sync> {
feeder: Feeder<T>,
socket: SocketSession,
}
Expand All @@ -22,7 +23,7 @@ pub struct SeriesInfo {
pub options: ChartOptions,
}

impl<T> WebSocket<T> {
impl<T: Clone + Debug + Send + Sync> WebSocket<T> {
pub fn new(feeder: Feeder<T>, socket: SocketSession) -> Self {
Self { feeder, socket }
}
Expand Down Expand Up @@ -441,7 +442,7 @@ impl<T> WebSocket<T> {
}

#[async_trait]
impl<T> Socket for WebSocket<T> {
impl<T: Clone + Debug + Send + Sync> Socket for WebSocket<T> {
async fn handle_message_data(&mut self, message: SocketMessageDe) -> Result<()> {
let event = TradingViewDataEvent::from(message.m.clone());
self.feeder.handle_events(event, &message.p).await;
Expand Down
20 changes: 10 additions & 10 deletions src/feeder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use crate::{
};
use serde::Deserialize;
use serde_json::Value;
use std::future::Future;
use std::pin::Pin;
use std::{collections::HashMap, sync::Arc};
use std::{fmt::Debug, future::Future};
use tracing::{debug, error, info, trace, warn};

#[derive(Clone, Default)]
pub struct Feeder<T> {
pub struct Feeder<T: Clone + Debug + Send + Sync> {
pub(crate) metadata: Metadata,
callbacks: HashMap<
TradingViewDataEvent,
Expand All @@ -44,7 +44,7 @@ pub struct Metadata {
pub quote_session: String,
}

impl<T> Feeder<T> {
impl<T: Clone + Debug + Send + Sync> Feeder<T> {
// pub async fn subscribe<T: Socket + Send + Sync>(
// &mut self,
// listener: &mut T,
Expand All @@ -61,13 +61,13 @@ impl<T> Feeder<T> {
}
}

// pub fn process_with_callback<T, F>(data: T, callback: F)
// where
// T: serde::Serialize,
// F: Fn(T),
// {
// callback(data);
// }
pub fn process_with_callback<F>(data: T, callback: F)
where
T: serde::Serialize,
F: Fn(T),
{
callback(data);
}

// pub fn add(mut self, publisher: Box<dyn Socket + Send + Sync>) -> Self {
// self.publisher.push(publisher);
Expand Down
7 changes: 4 additions & 3 deletions src/quote/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ use crate::{
};
use async_trait::async_trait;
use serde_json::Value;
use std::fmt::Debug;

#[derive(Clone)]
pub struct WebSocket<T> {
pub struct WebSocket<T: Clone + Debug + Send + Sync> {
feeder: Feeder<T>,
socket: SocketSession,
}

impl<T> WebSocket<T> {
impl<T: Clone + Debug + Send + Sync> WebSocket<T> {
pub fn new(feeder: Feeder<T>, socket: SocketSession) -> Self {
Self { feeder, socket }
}
Expand Down Expand Up @@ -78,7 +79,7 @@ impl<T> WebSocket<T> {
}

#[async_trait]
impl<T> Socket for WebSocket<T> {
impl<T: Clone + Debug + Send + Sync> Socket for WebSocket<T> {
async fn handle_message_data(&mut self, message: SocketMessageDe) -> Result<()> {
let event = TradingViewDataEvent::from(message.m.clone());
self.feeder.handle_events(event, &message.p).await;
Expand Down

0 comments on commit 6323b6f

Please sign in to comment.