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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use std::fmt::Debug;
use tokio::sync::broadcast::{
    self,
    error::{RecvError, TryRecvError},
    Receiver,
};

use super::ActorError;

/// A simple caching struct that can be used to locally maintain a synchronized state with an actor
#[derive(Debug)]
pub struct Cache<T> {
    inner: Option<T>,
    rx: broadcast::Receiver<T>,
    has_listenend: bool,
}

impl<T> Clone for Cache<T>
where
    T: Clone + Debug + Send + Sync + 'static,
{
    fn clone(&self) -> Self {
        Cache {
            inner: self.inner.clone(),
            rx: self.rx.resubscribe(),
            has_listenend: self.has_listenend,
        }
    }
}

impl<T> Cache<T>
where
    T: Clone + Debug + Send + Sync + 'static,
{
    pub(crate) fn new_initialized(rx: Receiver<T>, init: T) -> Self {
        let mut cache = Cache::new(rx);
        cache.inner = Some(init);
        cache
    }

    pub(crate) fn new(rx: Receiver<T>) -> Self {
        Self {
            inner: None,
            rx,
            has_listenend: false,
        }
    }

    /// Returns if any new updates are received
    pub fn has_updates(&self) -> bool {
        !self.rx.is_empty()
    }

    /// Returns the newest value available
    /// Note that when not initialized, this might return None while the actor has a value
    pub fn get_newest(&mut self) -> Result<Option<&T>, ActorError> {
        _ = self.try_recv_newest()?; // Update if possible
        Ok(self.get_inner())
    }

    /// Returns the current value held by the cache
    pub fn get_inner(&self) -> Option<&T> {
        self.inner.as_ref()
    }

    /// Receive the newest updated value broadcasted by the actor, discarding any older messages.
    /// If the cache was already initialized, it will return that value immediately the first time
    /// If it was not initialized, this method might wait indefinitely while the actor actually has a value.
    pub async fn recv_newest(&mut self) -> Result<&T, ActorError> {
        self._recv(true).await
    }

    /// Receive the last updated value broadcasted by the actor (FIFO).
    /// If the cache was already initialized, it will return that value immediately the first time.
    /// If it was not initialized, this method might wait indefinitely while the actor actually has a value.
    /// A warning is printed if the channel is lagging behind, so that older messages have been discarded.
    pub async fn recv(&mut self) -> Result<&T, ActorError> {
        self._recv(false).await
    }

    async fn _recv(&mut self, recv_newest: bool) -> Result<&T, ActorError> {
        // If listening for the first time, it returns immediately if a value is present
        if !self.has_listenend {
            self.has_listenend = true;

            // Update if possible if only interested in the newest value
            if recv_newest {
                _ = self.try_recv_newest()?;
            }

            if self.inner.is_some() {
                return Ok(self.get_inner().unwrap()); // Safe unwrap
            }
        }

        loop {
            match self.rx.recv().await {
                Ok(val) => {
                    self.inner = Some(val);
                    // If not only checking for the newest, or it is the last message, return the value
                    if !recv_newest || self.rx.is_empty() {
                        break Ok(self.inner.as_ref().unwrap());
                    }
                }
                Err(e) => match e {
                    RecvError::Closed => break Err(e.into()),
                    RecvError::Lagged(e) => {
                        let msg = format!(
                            "Cache of actor type {} lagged {e:?} messages",
                            std::any::type_name::<T>()
                        );
                        if !recv_newest {
                            log::warn!("{msg:?}")
                        } else {
                            log::debug!("{msg:?}")
                        }
                    }
                },
            }
        }
    }

    /// Try to receive the newest updated value broadcasted by the actor once, discarding any older messages.
    /// Note that when not initialized, this might return None while the actor has a value
    pub fn try_recv_newest(&mut self) -> Result<Option<&T>, ActorError> {
        self._try_recv(true)
    }

    /// Try to receive the last updated value broadcasted by the actor once (FIFO).
    /// Note that when not initialized, this might return None while the actor has a value
    /// A warning is printed if the channel is lagging behind, so that older messages have been discarded
    pub fn try_recv(&mut self) -> Result<Option<&T>, ActorError> {
        self._try_recv(false)
    }

    fn _try_recv(&mut self, recv_newest: bool) -> Result<Option<&T>, ActorError> {
        // If listening for the first time and not for the newest, return the initialized value immediately
        if !self.has_listenend && !recv_newest {
            self.has_listenend = true;
            if self.inner.is_some() {
                return Ok(self.get_inner());
            }
        }

        // In any other case, check for updates first
        loop {
            match self.rx.try_recv() {
                Ok(val) => {
                    self.has_listenend = true;
                    self.inner = Some(val);
                    // If not interested in the newest, break on the first result
                    // If interested in the newest and the channel is empty, break too
                    if !recv_newest || self.rx.is_empty() {
                        break Ok(self.get_inner());
                    }
                }
                Err(e) => match e {
                    TryRecvError::Closed => break Err(e.into()),
                    TryRecvError::Empty => {
                        // If no new updates are present when listening for the newest value the first time
                        // Then simply exit with the initialized value if present
                        if !self.has_listenend && recv_newest {
                            self.has_listenend = true;
                            if self.inner.is_some() {
                                return Ok(self.get_inner());
                            }
                        }
                        break Ok(None);
                    }
                    TryRecvError::Lagged(e) => {
                        let msg = format!(
                            "Cache of actor type {} lagged {e:?} messages",
                            std::any::type_name::<T>()
                        );
                        if !recv_newest {
                            log::warn!("{msg:?}")
                        } else {
                            log::debug!("{msg:?}")
                        }
                    }
                },
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::Handle;
    use tokio::time::{sleep, Duration};

    #[tokio::test]
    async fn test_unitialized() {
        let handle = Handle::new(1);
        let mut cache = handle.create_uninitialized_cache();
        assert_eq!(cache.get_newest().unwrap(), None); // Not initalized, so none although value is set
        handle.set(2).await.unwrap();
        assert_eq!(cache.get_newest().unwrap(), Some(&2)); // The new value is broadcasted and processed
    }

    #[tokio::test]
    async fn test_has_updates() {
        let handle = Handle::new(1);
        let cache = handle.create_initialized_cache().await.unwrap();
        assert_eq!(cache.has_updates(), false);
        handle.set(2).await.unwrap();
        assert!(cache.has_updates());
    }

    #[tokio::test]
    async fn test_recv_cache() {
        let handle = Handle::new(1);
        let mut cache = handle.create_initialized_cache().await.unwrap();
        _ = cache.recv().await.unwrap(); // First listen returns 10
        handle.set(2).await.unwrap();
        handle.set(3).await.unwrap(); // Not updated yet, as returning oldest value first
        assert_eq!(cache.recv().await.unwrap(), &2)
    }

    #[tokio::test]
    async fn test_recv_cache_newest() {
        let handle = Handle::new(1);
        let mut cache = handle.create_initialized_cache().await.unwrap();
        handle.set(2).await.unwrap();
        handle.set(3).await.unwrap();
        assert_eq!(cache.recv_newest().await.unwrap(), &3)
    }

    #[tokio::test]
    async fn test_immediate_cache_return() {
        let handle = Handle::new(1);
        let mut cache = handle.create_initialized_cache().await.unwrap();
        handle.set(2).await.unwrap(); // Not updated yet, as returning oldest value first
        assert_eq!(cache.recv().await.unwrap(), &1)
    }

    #[tokio::test]
    async fn test_immediate_cache_return_with_newest() {
        let handle = Handle::new(1);
        let mut cache = handle.create_initialized_cache().await.unwrap();
        handle.set(2).await.unwrap(); // Try to check for any newer
        assert_eq!(cache.recv_newest().await.unwrap(), &2)
    }

    #[tokio::test]
    async fn test_delayed_cache_return() {
        let handle = Handle::new(2);
        let mut cache = handle.create_initialized_cache().await.unwrap();

        let _ = cache.recv().await.unwrap(); // First listen exits immediately

        let res = tokio::select! {
            _ = async {
                sleep(Duration::from_millis(200)).await;
                handle.set(10).await.unwrap();
                sleep(Duration::from_millis(1000)).await; // Allow listen to exit
            } => {panic!("The listen() did no respond succesfully")}
            res = cache.recv() => {res.unwrap()}
        };

        assert_eq!(res, &10)
    }

    #[tokio::test]
    async fn test_try_recv_initialized() {
        let handle = Handle::new(2);
        let mut cache = handle.create_initialized_cache().await.unwrap();
        assert!(cache.try_recv().unwrap().is_some());
        assert!(cache.try_recv().unwrap().is_none())
    }

    #[tokio::test]
    async fn test_try_recv_uninitialized() {
        let handle = Handle::new(2);
        let mut cache = handle.create_uninitialized_cache();
        assert!(cache.try_recv().unwrap().is_none())
    }

    #[tokio::test]
    async fn test_try_recv_newest_initialized() {
        let handle = Handle::new(2);
        let mut cache = handle.create_initialized_cache().await.unwrap();
        assert!(cache.try_recv_newest().unwrap().is_some());
        assert!(cache.try_recv_newest().unwrap().is_none())
    }

    #[tokio::test]
    async fn test_try_recv_newest_uninitialized() {
        let handle = Handle::new(2);
        let mut cache = handle.create_uninitialized_cache();
        assert!(cache.try_recv_newest().unwrap().is_none())
    }

    #[tokio::test]
    async fn test_try_recv_some() {
        let handle = Handle::new(1);
        let mut cache = handle.create_initialized_cache().await.unwrap();
        handle.set(2).await.unwrap();
        handle.set(3).await.unwrap(); // Not returned, as returning oldes value first
        _ = cache.try_recv().unwrap(); // Initial value is retuned immediately
        assert_eq!(cache.try_recv().unwrap(), Some(&2))
    }

    #[tokio::test]
    async fn test_try_recv_some_newest() {
        let handle = Handle::new(1);
        let mut cache = handle.create_initialized_cache().await.unwrap();
        handle.set(2).await.unwrap();
        handle.set(3).await.unwrap(); // Returned, as newest value first
        assert_eq!(cache.try_recv_newest().unwrap(), Some(&3))
    }
}