summary refs log tree commit diff
path: root/src/system/aggregator.rs
blob: 0177249758b9ec2072e5b4f9a39adc74735c7825 (plain)
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
use lru::LruCache;
use std::num::NonZeroUsize;
use tokio::sync::mpsc::{channel, Receiver, Sender};

use super::{MessageEvent, MessageId, SystemEvent};

pub struct MessageAggregator {
    rx: Receiver<MessageEvent>,
    tx: Sender<MessageEvent>,
    message_cache: lru::LruCache<MessageId, MessageEvent>,
    emitter: Option<Sender<SystemEvent>>,
}

impl MessageAggregator {
    pub fn new() -> Self {
        let (tx, rx) = channel::<MessageEvent>(100);

        Self {
            tx,
            rx,
            message_cache: LruCache::new(NonZeroUsize::new(100).unwrap()),
            emitter: None,
        }
    }

    pub fn get_sender(&self) -> Sender<MessageEvent> {
        self.tx.clone()
    }

    pub fn set_handler(&mut self, emitter: Sender<SystemEvent>) -> () {
        self.emitter = Some(emitter);
    }

    pub fn start(mut self) -> () {
        tokio::spawn(async move {
            loop {
                match self.rx.recv().await {
                    None => return,
                    Some((timestamp, message)) => {
                        let last_seen_timestamp = self.message_cache.get(&message.id);
                        let current_timestamp = timestamp;

                        if last_seen_timestamp.is_none()
                            || last_seen_timestamp.unwrap().0.as_micros()
                                < current_timestamp.as_micros()
                        {
                            self.message_cache
                                .put(message.id, (timestamp, message.clone()));

                            if let Some(emitter) = &self.emitter {
                                emitter
                                    .send(SystemEvent::NewMessage((timestamp, message)))
                                    .await;
                            }
                        };
                    }
                }
            }
        });
    }
}