Skip to main content

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    /// Wraps the given async closure into a [`Notification`] that deserializes
32    /// an incoming message and invokes the closure with it.
33    pub fn new<FN>(method_fn: FN) -> Notification<Msg>
34    where
35        FN: Send + Sync + Fn(Msg) -> NotificationFnReturn<()> + 'static,
36    {
37        Notification {
38            method: Arc::new(Box::new(method_fn)),
39        }
40    }
41}
42
43#[async_trait]
44impl<Msg> NotificationTrait for Notification<Msg>
45where
46    Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
47{
48    async fn call_with_borsh(&self, data: &[u8]) -> ResponseResult<()> {
49        let req = Msg::try_from_slice(data)
50            .map_err(|err| ResponseError::NotificationDeserialize(err.to_string()))?;
51        (self.method)(req).await
52    }
53}