1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Module containing IPC [`Notification`] closure wrappers
use crate::ipc::imports::*;

/// Base trait representing an IPC notification, used to retain
/// notification structures in an IPC map without generics.
#[async_trait]
pub(crate) trait NotificationTrait: Send + Sync + 'static {
    async fn call_with_borsh(&self, data: &[u8]) -> ResponseResult<()>;
}

/// Notification closure type
pub type NotificationFn<Msg> =
    Arc<Box<dyn Send + Sync + Fn(Msg) -> NotificationFnReturn<()> + 'static>>;

/// Notification closure return type
pub type NotificationFnReturn<T> =
    Pin<Box<(dyn Send + 'static + Future<Output = ResponseResult<T>>)>>;

/// IPC notification wrapper. Contains the notification closure function.
pub struct Notification<Msg>
where
    Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
{
    method: NotificationFn<Msg>,
}

impl<Msg> Notification<Msg>
where
    Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
{
    pub fn new<FN>(method_fn: FN) -> Notification<Msg>
    where
        FN: Send + Sync + Fn(Msg) -> NotificationFnReturn<()> + 'static,
    {
        Notification {
            method: Arc::new(Box::new(method_fn)),
        }
    }
}

#[async_trait]
impl<Msg> NotificationTrait for Notification<Msg>
where
    Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
{
    async fn call_with_borsh(&self, data: &[u8]) -> ResponseResult<()> {
        let req = Msg::try_from_slice(data)
            .map_err(|err| ResponseError::NotificationDeserialize(err.to_string()))?;
        (self.method)(req).await
    }
}