summaryrefslogtreecommitdiffstats
path: root/gui/src/gossip.rs
blob: ae941f58ab0256d71ce5f4e7ad41b9a2e4e946da (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
use std::sync::Arc;

use futures::StreamExt;
use tokio::sync::RwLock;

use distrox_lib::profile::Profile;
use distrox_lib::client::Client;
use distrox_lib::gossip::GossipDeserializer;
use distrox_lib::gossip::LogStrategy;

use crate::app::Message;

#[derive(Clone, Debug)]
pub struct GossipRecipe {
    profile: Arc<RwLock<Profile>>,
    subscription: Arc<ipfs::SubscriptionStream>,
}

impl GossipRecipe {
    pub fn new(profile: Arc<RwLock<Profile>>, subscription: ipfs::SubscriptionStream) -> Self {
        Self { profile, subscription: Arc::new(subscription) }
    }
}


// Make sure iced can use our download stream
impl<H, I> iced_native::subscription::Recipe<H, I> for GossipRecipe
where
    H: std::hash::Hasher,
{
    type Output = Message;

    fn hash(&self, state: &mut H) {
        use std::hash::Hash;
        struct Marker;
        std::any::TypeId::of::<Marker>().hash(state);
    }

    fn stream(self: Box<Self>, _input: futures::stream::BoxStream<'static, I>) -> futures::stream::BoxStream<'static, Self::Output> {
        // TODO: Do "right", whatever this means...
        let stream = Arc::try_unwrap(self.subscription).unwrap();

        Box::pin({
            GossipDeserializer::<LogStrategy>::new()
                .run(stream)
                .map(|(source, msg)| Message::GossipMessage(source, msg))
        })
    }
}