binary_options_tools_core_pre/
callback.rs1use std::sync::Arc;
2
3use async_trait::async_trait;
4use kanal::AsyncSender;
5use tokio_tungstenite::tungstenite::Message;
6
7use crate::{
8 error::CoreResult,
9 traits::{AppState, ReconnectCallback},
10};
11
12pub struct ConnectionCallback<S: AppState> {
13 pub on_connect: OnConnectCallback<S>,
14 pub on_reconnect: ReconnectCallbackStack<S>,
15}
16
17pub type OnConnectCallback<S> = Box<
19 dyn Fn(
20 Arc<S>,
21 &AsyncSender<Message>,
22 ) -> futures_util::future::BoxFuture<'static, CoreResult<()>>
23 + Send
24 + Sync,
25>;
26
27pub struct ReconnectCallbackStack<S: AppState> {
28 pub layers: Vec<Box<dyn ReconnectCallback<S>>>,
29}
30
31impl<S: AppState> Default for ReconnectCallbackStack<S> {
32 fn default() -> Self {
33 Self { layers: Vec::new() }
34 }
35}
36
37impl<S: AppState> ReconnectCallbackStack<S> {
38 pub fn add_layer(&mut self, layer: Box<dyn ReconnectCallback<S>>) {
39 self.layers.push(layer);
40 }
41}
42
43#[async_trait]
44impl<S: AppState> ReconnectCallback<S> for ReconnectCallbackStack<S> {
45 async fn call(&self, state: Arc<S>, sender: &AsyncSender<Message>) -> CoreResult<()> {
46 for layer in &self.layers {
47 layer.call(state.clone(), sender).await?;
48 }
49 Ok(())
50 }
51}