workflow_nw/ipc/
notification.rs

1//! Module containing IPC [`Notification`] closure wrappers
2use crate::ipc::imports::*;
3
4/// Base trait representing an IPC notification, used to retain
5/// notification structures in an IPC map without generics.
6#[async_trait]
7pub(crate) trait NotificationTrait: Send + Sync + 'static {
8    async fn call_with_borsh(&self, data: &[u8]) -> ResponseResult<()>;
9}
10
11/// Notification closure type
12pub type NotificationFn<Msg> =
13    Arc<Box<dyn Send + Sync + Fn(Msg) -> NotificationFnReturn<()> + 'static>>;
14
15/// Notification closure return type
16pub type NotificationFnReturn<T> =
17    Pin<Box<(dyn Send + 'static + Future<Output = ResponseResult<T>>)>>;
18
19/// IPC notification wrapper. Contains the notification closure function.
20pub struct Notification<Msg>
21where
22    Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
23{
24    method: NotificationFn<Msg>,
25}
26
27impl<Msg> Notification<Msg>
28where
29    Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
30{
31    pub fn new<FN>(method_fn: FN) -> Notification<Msg>
32    where
33        FN: Send + Sync + Fn(Msg) -> NotificationFnReturn<()> + 'static,
34    {
35        Notification {
36            method: Arc::new(Box::new(method_fn)),
37        }
38    }
39}
40
41#[async_trait]
42impl<Msg> NotificationTrait for Notification<Msg>
43where
44    Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
45{
46    async fn call_with_borsh(&self, data: &[u8]) -> ResponseResult<()> {
47        let req = Msg::try_from_slice(data)
48            .map_err(|err| ResponseError::NotificationDeserialize(err.to_string()))?;
49        (self.method)(req).await
50    }
51}