Repository navigation
Expand file tree
/
Copy pathsubscription.rs
More file actions
63 lines (58 loc) · 1.99 KB
/
Copy pathsubscription.rs
File metadata and controls
63 lines (58 loc) · 1.99 KB
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
use async_graphql::futures_util::Stream;
use async_graphql::{Context, Subscription};
use tokio_stream::wrappers::BroadcastStream;
use tokio_stream::StreamExt as _;
use crate::state::{RefUpdateBroadcast, TaskEventBroadcast};
use super::types::{RefUpdateType, TaskEventType};
pub struct SubscriptionRoot;
#[Subscription]
impl SubscriptionRoot {
async fn ref_updates(
&self,
ctx: &Context<'_>,
repo: Option<String>,
) -> impl Stream<Item = RefUpdateType> {
let rx = ctx
.data_unchecked::<tokio::sync::broadcast::Sender<RefUpdateBroadcast>>()
.subscribe();
BroadcastStream::new(rx).filter_map(move |r| {
let rf = repo.clone();
match r {
Ok(ev) if rf.as_deref().map_or(true, |r| ev.repo == r) => Some(RefUpdateType {
repo: ev.repo,
ref_name: ev.ref_name,
old_sha: ev.old_sha,
new_sha: ev.new_sha,
pusher_did: ev.pusher_did,
node_did: ev.node_did,
timestamp: ev.timestamp,
}),
_ => None,
}
})
}
async fn task_events(
&self,
ctx: &Context<'_>,
task_id: Option<String>,
) -> impl Stream<Item = TaskEventType> {
let rx = ctx
.data_unchecked::<tokio::sync::broadcast::Sender<TaskEventBroadcast>>()
.subscribe();
BroadcastStream::new(rx).filter_map(move |r| {
let tf = task_id.clone();
match r {
Ok(ev) if tf.as_deref().map_or(true, |id| ev.task_id == id) => {
Some(TaskEventType {
task_id: ev.task_id,
old_status: ev.old_status,
new_status: ev.new_status,
by_did: ev.by_did,
at: ev.at,
})
}
_ => None,
}
})
}
}