1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::service::ControlCommand;
use crate::INotifiee;
use futures::channel::{mpsc, oneshot};
use futures::SinkExt;
use std::fmt;
#[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord)]
pub struct RegId(u32);
impl RegId {
pub(crate) fn random() -> Self {
RegId(rand::random())
}
}
impl fmt::Display for RegId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl PartialEq for RegId {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl std::hash::Hash for RegId {
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
hasher.write_u32(self.0);
}
}
impl nohash_hasher::IsEnabled for RegId {}
pub struct Control {
tx: mpsc::Sender<ControlCommand>,
}
impl Control {
pub fn new(tx: mpsc::Sender<ControlCommand>) -> Self {
Control { tx }
}
pub async fn register_notifee(&mut self, noti: INotifiee) -> RegId {
let (tx, rx) = oneshot::channel();
let cmd = ControlCommand::RegisterNotifee(noti, tx);
self.tx.send(cmd).await.expect("send RegisterNotifee failed");
rx.await.expect("recv RegisterNotifee failed")
}
pub async fn unregister_notifee(&mut self, id: RegId) {
let _ = self.tx.send(ControlCommand::UnregisterNotifee(id)).await;
}
}