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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! The in-process channel that wakes a panel user's open bells when their stored notifications
//! change: it carries whose, never what, so a bell reads the change back through its own scoped
//! query.
use std::sync::Arc;
use tokio::sync::broadcast;
use topcoat::context::{Cx, try_context};
/// How many wakes a slow bell may fall behind by before it skips to the latest; a bell that
/// lags re-reads once, which is all a wake asks of it.
const CAPACITY: usize = 256;
/// Whose stored notifications changed: one panel user on one panel.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Wake {
pub(crate) panel: String,
pub(crate) user_id: String,
}
/// The router's channel of [`Wake`]s, shared by every panel it mounts. Cloning it is cheap.
#[derive(Clone)]
pub(crate) struct NotificationBus(broadcast::Sender<Arc<Wake>>);
impl NotificationBus {
pub(crate) fn new() -> Self {
Self(broadcast::channel(CAPACITY).0)
}
/// Wakes every bell `user_id` has open on `panel`.
pub(crate) fn wake(&self, panel: &str, user_id: &str) {
// No open bell is no one to wake.
let _ = self.0.send(Arc::new(Wake {
panel: panel.to_owned(),
user_id: user_id.to_owned(),
}));
}
/// A receiver for the wakes of `user_id` on `panel`. Subscribe before the read the wake
/// refreshes, so a change between the read and the wait is not missed.
pub(crate) fn subscribe(&self, panel: &str, user_id: &str) -> Subscription {
Subscription {
receiver: self.0.subscribe(),
wake: Wake {
panel: panel.to_owned(),
user_id: user_id.to_owned(),
},
}
}
}
/// One bell's wait for its user's wakes.
pub(crate) struct Subscription {
receiver: broadcast::Receiver<Arc<Wake>>,
wake: Wake,
}
impl Subscription {
/// Waits for the next wake of this subscription's user. A bell that fell behind wakes at
/// once; `false` means the bus is gone and no wake will come.
pub(crate) async fn changed(&mut self) -> bool {
loop {
match self.receiver.recv().await {
Ok(wake) if *wake == self.wake => return true,
Ok(_) => {}
Err(broadcast::error::RecvError::Lagged(_)) => return true,
Err(broadcast::error::RecvError::Closed) => return false,
}
}
}
}
/// The router's bus, or one no bell listens on outside a router.
pub(crate) fn bus(cx: &Cx) -> NotificationBus {
try_context::<NotificationBus>(cx)
.cloned()
.unwrap_or_else(NotificationBus::new)
}
#[cfg(test)]
mod tests {
use super::*;
/// A wake reaches its own user's bell on its own panel, and no one else's.
#[tokio::test]
async fn a_wake_reaches_only_its_users_bell_on_its_panel() {
let bus = NotificationBus::new();
let mut mine = bus.subscribe("/admin", "1");
bus.wake("/admin", "1");
assert!(mine.changed().await, "the user's own wake reaches the bell");
bus.wake("/admin", "2");
bus.wake("/portal", "1");
drop(bus);
assert!(
!mine.changed().await,
"another user's wake, or the user's on another panel, does not"
);
}
}