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
use actix::{Actor, Context, Handler, Recipient, Supervised, SystemService};
use async_graphql::Result;
use slab::Slab;
use std::any::Any;
use std::sync::Arc;

#[derive(Message)]
#[rtype(result = "std::result::Result<(), ()>")]
pub struct PushMessage(pub Arc<dyn Any + Sync + Send>);

#[derive(Message)]
#[rtype(result = "usize")]
struct NewClient {
    recipient: Recipient<PushMessage>,
}

#[derive(Message)]
#[rtype(result = "()")]
struct RemoveClient {
    id: usize,
}

#[derive(Message)]
#[rtype(result = "()")]
struct PubMessage(Arc<dyn Any + Sync + Send>);

struct ClientInfo {
    recipient: Recipient<PushMessage>,
}

#[derive(Default)]
struct PubSubService {
    clients: Slab<ClientInfo>,
}

impl Actor for PubSubService {
    type Context = Context<Self>;
}

impl Handler<NewClient> for PubSubService {
    type Result = usize;

    fn handle(&mut self, msg: NewClient, _ctx: &mut Context<Self>) -> Self::Result {
        self.clients.insert(ClientInfo {
            recipient: msg.recipient,
        })
    }
}

impl Handler<RemoveClient> for PubSubService {
    type Result = ();

    fn handle(&mut self, msg: RemoveClient, _ctx: &mut Context<Self>) -> Self::Result {
        self.clients.remove(msg.id);
    }
}

impl Handler<PubMessage> for PubSubService {
    type Result = ();

    fn handle(&mut self, msg: PubMessage, _ctx: &mut Context<Self>) -> Self::Result {
        for (_, client) in &self.clients {
            client.recipient.do_send(PushMessage(msg.0.clone())).ok();
        }
    }
}

impl Supervised for PubSubService {}

impl SystemService for PubSubService {}

pub async fn new_client(recipient: Recipient<PushMessage>) -> Result<usize> {
    let id = PubSubService::from_registry()
        .send(NewClient { recipient })
        .await?;
    Ok(id)
}

pub fn remove_client(id: usize) {
    PubSubService::from_registry().do_send(RemoveClient { id });
}

/// Publish a message that will be pushed to all subscribed clients.
pub fn publish_message<T: Any + Send + Sync + Sized>(msg: T) {
    PubSubService::from_registry().do_send(PubMessage(Arc::new(msg)));
}