workflow_nw/ipc/
notification.rs1use crate::ipc::imports::*;
3
4#[async_trait]
7pub(crate) trait NotificationTrait: Send + Sync + 'static {
8 async fn call_with_borsh(&self, data: &[u8]) -> ResponseResult<()>;
9}
10
11pub type NotificationFn<Msg> =
13 Arc<Box<dyn Send + Sync + Fn(Msg) -> NotificationFnReturn<()> + 'static>>;
14
15pub type NotificationFnReturn<T> =
17 Pin<Box<(dyn Send + 'static + Future<Output = ResponseResult<T>>)>>;
18
19pub 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}