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>
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}