botx_api_framework/contexts/
notification.rs

1use std::collections::HashMap;
2
3use anthill_di_derive::constructor;
4use async_oneshot::{oneshot, Sender, Receiver};
5use uuid::Uuid;
6
7
8#[derive(constructor)]
9pub struct NotificationContext {
10    #[custom_resolve(value = "Default::default()")]
11    notification_waiters: HashMap<Uuid, Sender<()>>
12}
13
14impl NotificationContext {
15    pub fn new() -> Self { Self { notification_waiters: Default::default() } }
16
17    pub fn set_callback(&mut self, sync_id: Uuid) -> Receiver<()> {
18        let (sender,receiver) = oneshot::<()>();
19        self.notification_waiters.insert(sync_id, sender);
20        receiver
21    }
22
23    pub fn event(&mut self, sync_id: Uuid) {
24        self.notification_waiters.remove(&sync_id)
25            .map(|mut x| { x.send(()); });
26    }
27}