summaryrefslogtreecommitdiffstats
path: root/native/src/subscription/events.rs
blob: a1ae6051087ee99ec5905e466216d0e9a7c9a41d (plain) (blame)
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
use crate::{
    subscription::{EventStream, Recipe},
    Event, Hasher,
};
use iced_futures::futures::future;
use iced_futures::futures::StreamExt;
use iced_futures::BoxStream;

pub struct Events<Message> {
    pub(super) f: fn(Event) -> Option<Message>,
}

impl<Message> Recipe<Hasher, Event> for Events<Message>
where
    Message: 'static + Send,
{
    type Output = Message;

    fn hash(&self, state: &mut Hasher) {
        use std::hash::Hash;

        struct Marker;
        std::any::TypeId::of::<Marker>().hash(state);
        self.f.hash(state);
    }

    fn stream(
        self: Box<Self>,
        event_stream: EventStream,
    ) -> BoxStream<Self::Output> {
        event_stream
            .filter_map(move |event| future::ready((self.f)(event)))
            .boxed()
    }
}