summaryrefslogtreecommitdiffstats
path: root/core/src/subscription.rs
blob: e9559f3c24242ad28c7ed0554ca07ca57080261c (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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Generate events asynchronously for you application.

/// An event subscription.
pub struct Subscription<Hasher, Input, Output> {
    recipes: Vec<Box<dyn Recipe<Hasher, Input, Output = Output>>>,
}

impl<H, I, O> Subscription<H, I, O>
where
    H: std::hash::Hasher,
{
    pub fn none() -> Self {
        Self {
            recipes: Vec::new(),
        }
    }

    pub fn from_recipe(
        recipe: impl Recipe<H, I, Output = O> + 'static,
    ) -> Self {
        Self {
            recipes: vec![Box::new(recipe)],
        }
    }

    pub fn batch(
        subscriptions: impl Iterator<Item = Subscription<H, I, O>>,
    ) -> Self {
        Self {
            recipes: subscriptions
                .flat_map(|subscription| subscription.recipes)
                .collect(),
        }
    }

    pub fn recipes(self) -> Vec<Box<dyn Recipe<H, I, Output = O>>> {
        self.recipes
    }

    pub fn map<A>(
        mut self,
        f: impl Fn(O) -> A + Send + Sync + 'static,
    ) -> Subscription<H, I, A>
    where
        H: 'static,
        I: 'static,
        O: 'static,
        A: 'static,
    {
        let function = std::sync::Arc::new(f);

        Subscription {
            recipes: self
                .recipes
                .drain(..)
                .map(|recipe| {
                    Box::new(Map::new(recipe, function.clone()))
                        as Box<dyn Recipe<H, I, Output = A>>
                })
                .collect(),
        }
    }
}

impl<I, O, H> std::fmt::Debug for Subscription<I, O, H> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Subscription").finish()
    }
}

/// The connection of an event subscription.
pub trait Recipe<Hasher: std::hash::Hasher, Input> {
    type Output;

    fn hash(&self, state: &mut Hasher);

    fn stream(
        &self,
        input: Input,
    ) -> futures::stream::BoxStream<'static, Self::Output>;
}

struct Map<Hasher, Input, A, B> {
    recipe: Box<dyn Recipe<Hasher, Input, Output = A>>,
    mapper: std::sync::Arc<dyn Fn(A) -> B + Send + Sync>,
}

impl<H, I, A, B> Map<H, I, A, B> {
    fn new(
        recipe: Box<dyn Recipe<H, I, Output = A>>,
        mapper: std::sync::Arc<dyn Fn(A) -> B + Send + Sync + 'static>,
    ) -> Self {
        Map { recipe, mapper }
    }
}

impl<H, I, A, B> Recipe<H, I> for Map<H, I, A, B>
where
    A: 'static,
    B: 'static,
    H: std::hash::Hasher,
{
    type Output = B;

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

        std::any::TypeId::of::<B>().hash(state);
        self.recipe.hash(state);
    }

    fn stream(
        &self,
        input: I,
    ) -> futures::stream::BoxStream<'static, Self::Output> {
        use futures::StreamExt;

        let mapper = self.mapper.clone();

        self.recipe
            .stream(input)
            .map(move |element| mapper(element))
            .boxed()
    }
}