event_store/subscriptions/
supervisor.rs

1use std::marker::PhantomData;
2
3use super::subscription::Subscription;
4use super::SubscriptionOptions;
5use crate::Storage;
6use actix::prelude::*;
7
8#[derive(Default)]
9pub struct SubscriptionsSupervisor<S: Storage> {
10    _storage: PhantomData<S>,
11    subsctions: Vec<Addr<Subscription<S>>>,
12}
13
14impl<S: Storage> Actor for SubscriptionsSupervisor<S> {
15    type Context = Context<Self>;
16}
17
18impl<S: Storage> Supervised for SubscriptionsSupervisor<S> {}
19impl<S: Storage> ArbiterService for SubscriptionsSupervisor<S> {}
20
21impl<S: Storage> SubscriptionsSupervisor<S> {
22    pub async fn start_subscription(
23        options: &SubscriptionOptions,
24    ) -> Result<Addr<Subscription<S>>, ()> {
25        match Self::from_registry()
26            .send(CreateSubscription(options.clone(), PhantomData))
27            .await
28        {
29            Ok(v) => Ok(v),
30            Err(_) => Err(()),
31        }
32    }
33}
34
35#[derive(Message)]
36#[rtype(result = "Addr<Subscription<S>>")]
37struct CreateSubscription<S: Storage>(SubscriptionOptions, PhantomData<S>);
38
39impl<S: Storage> Handler<CreateSubscription<S>> for SubscriptionsSupervisor<S> {
40    type Result = MessageResult<CreateSubscription<S>>;
41
42    fn handle(&mut self, msg: CreateSubscription<S>, ctx: &mut Self::Context) -> Self::Result {
43        let addr = Subscription::start_with_options(&msg.0, ctx.address());
44
45        self.subsctions.push(addr.clone());
46
47        MessageResult(addr)
48    }
49}
50
51#[derive(Message)]
52#[rtype("()")]
53pub struct Started;
54
55impl<S: Storage> Handler<Started> for SubscriptionsSupervisor<S> {
56    type Result = MessageResult<Started>;
57
58    fn handle(&mut self, _: Started, _: &mut Self::Context) -> Self::Result {
59        MessageResult(())
60    }
61}
62
63#[derive(Message)]
64#[rtype("()")]
65pub struct GoingDown;
66
67impl<S: Storage> Handler<GoingDown> for SubscriptionsSupervisor<S> {
68    type Result = MessageResult<GoingDown>;
69
70    fn handle(&mut self, _: GoingDown, _: &mut Self::Context) -> Self::Result {
71        MessageResult(())
72    }
73}
74
75#[derive(Message)]
76#[rtype("()")]
77pub struct Down;
78
79impl<S: Storage> Handler<Down> for SubscriptionsSupervisor<S> {
80    type Result = MessageResult<Down>;
81
82    fn handle(&mut self, _: Down, _: &mut Self::Context) -> Self::Result {
83        MessageResult(())
84    }
85}