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
|
use std::sync::Arc;
use futures::StreamExt;
use tokio::sync::RwLock;
use distrox_lib::profile::Profile;
use distrox_lib::client::Client;
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> {
use distrox_lib::gossip::deserializer;
use distrox_lib::gossip::handler;
// TODO: Do "right", whatever this means...
let stream = Arc::try_unwrap(self.subscription).unwrap();
Box::pin({
let stream = deserializer::GossipDeserializer::<deserializer::LogStrategy>::new().run(stream);
let stream = handler::GossipHandler::<handler::LogStrategy>::new(self.profile.clone()).run(stream);
stream.map(|(gossip_message, _handling_result)| {
Message::GossipHandled(gossip_message)
})
})
}
}
|