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
use crate::{subscriptions::Subscriptions, ChannelEvent, Version};
use async_broadcast::Receiver as BroadcastReceiver;
use async_channel::Receiver;
use futures_lite::{stream::Race, Stream, StreamExt};
use std::{
    pin::Pin,
    task::{Context, Poll},
};
use trillium_websockets::Message;

#[derive(Debug)]
pub struct ClientReceiver {
    subscriptions: Subscriptions,
    race: Pin<Box<Race<BroadcastReceiver<ChannelEvent>, Receiver<ChannelEvent>>>>,
    version: Version,
}

impl ClientReceiver {
    pub fn new(
        individual: Receiver<ChannelEvent>,
        broadcast: BroadcastReceiver<ChannelEvent>,
        subscriptions: Subscriptions,
        version: Version,
    ) -> Self {
        Self {
            race: Box::pin(broadcast.race(individual)),
            subscriptions,
            version,
        }
    }
}

impl Stream for ClientReceiver {
    type Item = Message;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        loop {
            match self.race.poll_next(cx) {
                Poll::Ready(Some(event)) if !self.subscriptions.subscribes(&event) => continue,
                Poll::Ready(Some(event)) => {
                    if let Ok(text) = event.serialize(self.version) {
                        log::trace!(
                            "serialized {:?} with {:?} as {:?}",
                            event,
                            &self.version,
                            &text
                        );
                        break Poll::Ready(Some(Message::Text(text)));
                    }
                }
                Poll::Pending => break Poll::Pending,
                Poll::Ready(None) => break Poll::Ready(None),
            }
        }
    }
}