-
Notifications
You must be signed in to change notification settings - Fork 69
/
pub_sub.rs
61 lines (49 loc) · 1.44 KB
/
pub_sub.rs
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
use crux_core::capability::{CapabilityContext, Operation};
use futures::StreamExt;
use serde::{Deserialize, Serialize};
// TODO add topics
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum PubSubOperation {
Publish(Vec<u8>),
Subscribe,
}
#[derive(Deserialize)]
pub struct Message(pub Vec<u8>);
impl Operation for PubSubOperation {
type Output = Message;
}
#[derive(crux_core::macros::Capability)]
pub struct PubSub<Event> {
context: CapabilityContext<PubSubOperation, Event>,
}
impl<Ev> PubSub<Ev>
where
Ev: 'static,
{
pub fn new(context: CapabilityContext<PubSubOperation, Ev>) -> Self {
Self { context }
}
pub fn subscribe<F>(&self, make_event: F)
where
F: FnOnce(Vec<u8>) -> Ev + Clone + Send + 'static,
{
self.context.spawn({
let context = self.context.clone();
async move {
let mut stream = context.stream_from_shell(PubSubOperation::Subscribe);
while let Some(message) = stream.next().await {
let make_event = make_event.clone();
context.update_app(make_event(message.0));
}
}
})
}
pub fn publish(&self, data: Vec<u8>) {
self.context.spawn({
let context = self.context.clone();
async move {
context.notify_shell(PubSubOperation::Publish(data)).await;
}
})
}
}