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
//! Combine multiple subscribers.

use lunatic::Process;
use serde::{Deserialize, Serialize};

use crate::{spawn_subscriber, Event, Metadata};

use super::Subscriber;

/// Combines multiple subscribers into a single subscriber.
///
/// Child subscriber processes are spawned, and each one is notified of incoming events.
#[derive(Default, Serialize, Deserialize)]
pub struct MultipleSubscribers {
    subscribers: Vec<Process<Event>>,
}

impl MultipleSubscribers {
    /// Creates an instance of [`MultipleSubscribers`].
    pub fn new() -> Self {
        MultipleSubscribers::default()
    }

    /// Adds a child subscriber which runs in its own process.
    pub fn add_subscriber(mut self, subscriber: impl Subscriber) -> Self {
        let process = spawn_subscriber(subscriber);
        self.subscribers.push(process);
        self
    }
}

impl Subscriber for MultipleSubscribers {
    fn enabled(&self, _metadata: &Metadata) -> bool {
        !self.subscribers.is_empty()
    }

    fn event(&self, event: &Event) {
        for subscriber in &self.subscribers {
            subscriber.send(event.clone());
        }
    }
}