Skip to main content

psbus/sync/
subscribe.rs

1/*
2    ABSTRACT: Definition of a thread-safe generic subscriber which can subscribe to an
3    intermediary event bus (see bus.rs) which dispatches relevant generic events
4    that are published to them by one or more publishers (see publish.rs)
5*/
6use crate::{sync::Event, types::BusRequest};
7use std::hash::Hash;
8use uuid::Uuid;
9
10/// A generic, thread-safe `Subscriber` which subscribes to an `EventBus` to receive events `E` of category `T`, which are published by a `Publisher`.
11///
12/// - `T` is meant to be implemented by the module consumer as an enum, depicting the various categories an event can belong to.
13///
14/// - `E` is meant to be implemented by the module consumer as an enum, depicting the individual events which exist in the system. See `Event`.
15pub trait Subscriber<T, E>
16where
17    T: Eq + PartialEq + Hash + Clone + Send + Sync + 'static,
18    E: Event<T> + Eq + PartialEq + Hash + Clone + Send + Sync + 'static,
19{
20    fn id(&self) -> &Uuid;
21    fn on_event(&self, event: &E) -> BusRequest;
22}