workflow_rpc/client/interface/
notification.rs1use crate::imports::*;
2
3#[async_trait]
4pub trait NotificationTrait: Send + Sync + 'static {
5 async fn call_with_borsh(&self, data: &[u8]) -> ServerResult<()>;
6 async fn call_with_serde_json(&self, value: Value) -> ServerResult<()>;
7}
8
9pub type NotificationFn<Msg> =
10 Arc<Box<dyn Send + Sync + Fn(Msg) -> NotificationFnReturn<()> + 'static>>;
11
12pub type NotificationFnReturn<T> =
13 Pin<Box<(dyn Send + 'static + Future<Output = ServerResult<T>>)>>;
14
15pub struct Notification<Msg>
16where
17 Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
18{
19 method: NotificationFn<Msg>,
20}
21
22impl<Msg> Notification<Msg>
23where
24 Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
25{
26 pub fn new<FN>(method_fn: FN) -> Notification<Msg>
27 where
28 FN: Send + Sync + Fn(Msg) -> NotificationFnReturn<()> + 'static,
29 {
30 Notification {
31 method: Arc::new(Box::new(method_fn)),
32 }
33 }
34}
35
36#[async_trait]
37impl<Msg> NotificationTrait for Notification<Msg>
38where
39 Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
40{
41 async fn call_with_borsh(&self, data: &[u8]) -> ServerResult<()> {
42 let msg = Msg::try_from_slice(data)
43 .map_err(|err| ServerError::NotificationDeserialize(err.to_string()))?;
44 (self.method)(msg).await
45 }
46
47 async fn call_with_serde_json(&self, value: Value) -> ServerResult<()> {
48 let msg: Msg = serde_json::from_value(value)
49 .map_err(|err| ServerError::NotificationDeserialize(err.to_string()))?;
50 (self.method)(msg).await
51 }
52}