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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::{
AsState,
source::Inner,
state::{State, StateEvent},
};
use std::{fmt::Debug, ops::Deref};
use tokio::{
select,
sync::broadcast::{Sender, channel, error::RecvError},
};
/// Reader of state, to receive state change events.
#[derive(Clone, Debug)]
pub struct Reader<S>(Inner<S>)
where
S: 'static + AsState;
impl<S> Deref for Reader<S>
where
S: 'static + AsState,
{
type Target = Inner<S>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<S> Reader<S>
where
S: 'static + AsState,
{
pub(crate) fn from(inner: Inner<S>) -> Self {
Self(inner)
}
pub(crate) fn new(capacity: usize, sender: Sender<StateEvent<S>>) -> Self {
Self(Inner {
capacity,
sender,
pass_checks: Default::default(),
})
}
}
impl<S> Reader<S>
where
S: 'static + AsState + Send,
{
/// Check is the channel has been closed.
pub fn is_closed(&self) -> bool {
self.sender.subscribe().is_closed()
}
/// Convert data type of state reader.
/// # Arguments
/// * `capacity` - capacity of the new broadcast channel will be created.
/// # Returns
/// Reader of new data type.
pub fn extend<T>(&self, capacity: usize) -> Reader<T>
where
T: AsState + From<S> + Send,
{
self.extend_with(capacity, T::from)
}
/// Convert data type of state reader, with an closure.
/// # Arguments
/// * `capacity` - capacity of the new broadcast channel will be created.
/// * `f` - an closure, which takes the old state value as parameter, and return the new state value.
/// # Returns
/// Reader of new data type.
pub fn extend_with<T, F>(&self, capacity: usize, f: F) -> Reader<T>
where
T: AsState + Send,
F: Fn(S) -> T + Send + 'static,
{
let (tx, _) = channel(capacity);
let tx_c = tx.clone();
let mut rx_o = self.sender.subscribe();
tokio::spawn(async move {
loop {
select! {
res = rx_o.recv() => {
match res {
Ok(s) => {
tracing::trace!("recv -- {:?}", s.state);
let s_new = StateEvent {
state: State {
value: f(s.state.value),
timestamp: s.state.timestamp,
},
is_touch: s.is_touch,
close_handle: s.close_handle,
};
if tx_c.send(s_new).is_err() {
break;
}
},
Err(e) => {
match e {
RecvError::Closed => break,
RecvError::Lagged(n) => {
tracing::warn!("lagged | skipped {n} messages.")
},
}
},
}
}
}
}
});
Reader(Inner {
capacity: self.capacity,
sender: tx,
pass_checks: self.pass_checks.clone(),
})
}
/// Convert data type of state reader, with an async closure.
/// # Arguments
/// * `capacity` - capacity of the new broadcast channel will be created.
/// * `f` - an async closure, which takes the old state value as parameter, and return the new state value.
/// # Returns
/// Reader of new data type.
pub fn async_entend_with<T, F, Fut>(&self, capacity: usize, f: F) -> Reader<T>
where
T: 'static + AsState + Send,
F: Fn(S) -> Fut + Send + Sync + 'static,
Fut: Future<Output = T> + Send,
{
let (tx, _) = channel(capacity);
let tx_c = tx.clone();
let mut rx_o = self.sender.subscribe();
tokio::spawn(async move {
loop {
select! {
res = rx_o.recv() => {
match res {
Ok(s) => {
tracing::trace!("recv -- {:?}", s.state);
let s_new = StateEvent {
state: State {
value: f(s.state.value).await,
timestamp: s.state.timestamp,
},
is_touch: s.is_touch,
close_handle: s.close_handle,
};
if tx_c.send(s_new).is_err() {
break;
}
},
Err(e) => {
match e {
RecvError::Closed => break,
RecvError::Lagged(n) => {
tracing::warn!("lagged | skipped {n} messages.")
},
}
},
}
}
}
}
});
Reader(Inner {
capacity,
sender: tx,
pass_checks: self.pass_checks.clone(),
})
}
}