use std::sync::Arc;
use tokio::sync::mpsc;
use crate::{
Config, Result,
blocking::runtime::BlockingRuntime,
screener::{
ScreenerContext,
types::{
ScreenerIndicatorsResponse, ScreenerRecommendStrategiesResponse,
ScreenerSearchResponse, ScreenerStrategyResponse, ScreenerUserStrategiesResponse,
},
},
};
pub struct ScreenerContextSync {
rt: BlockingRuntime<ScreenerContext>,
}
impl ScreenerContextSync {
pub fn new(config: Arc<Config>) -> Result<Self> {
let rt = BlockingRuntime::try_new(
move || {
let ctx = ScreenerContext::new(config);
let (tx, rx) = mpsc::unbounded_channel::<std::convert::Infallible>();
std::mem::forget(tx);
Ok::<_, crate::Error>((ctx, rx))
},
|_: std::convert::Infallible| {},
)?;
Ok(Self { rt })
}
pub fn screener_recommend_strategies(&self) -> Result<ScreenerRecommendStrategiesResponse> {
self.rt
.call(|ctx| async move { ctx.screener_recommend_strategies().await })
}
pub fn screener_user_strategies(&self) -> Result<ScreenerUserStrategiesResponse> {
self.rt
.call(|ctx| async move { ctx.screener_user_strategies().await })
}
pub fn screener_strategy(&self, id: i64) -> Result<ScreenerStrategyResponse> {
self.rt
.call(move |ctx| async move { ctx.screener_strategy(id).await })
}
pub fn screener_search(
&self,
market: impl Into<String> + Send + 'static,
strategy_id: Option<i64>,
page: u32,
size: u32,
) -> Result<ScreenerSearchResponse> {
self.rt.call(move |ctx| async move {
ctx.screener_search(market, strategy_id, page, size).await
})
}
pub fn screener_indicators(&self) -> Result<ScreenerIndicatorsResponse> {
self.rt
.call(|ctx| async move { ctx.screener_indicators().await })
}
}