xmtp_proto/types/
incoming_event.rs1use super::{Cursor, ReceivedEnvelope, Topic, TopicCursor};
2use futures::StreamExt;
3use xmtp_common::{BoxDynStream, MaybeSend, MaybeSync};
4
5#[derive(Clone, Copy, Debug)]
7pub struct IncomingBatchLimits {
8 pub max_rows: usize,
10 pub max_bytes: usize,
12}
13
14impl IncomingBatchLimits {
15 pub const STREAM_BATCH: Self = Self {
18 max_rows: xmtp_configuration::STREAM_BATCH_ROWS as usize,
19 max_bytes: xmtp_configuration::STREAM_BATCH_BYTES as usize,
20 };
21}
22
23trait ReceiptSink: MaybeSend + MaybeSync {
24 fn acknowledge(&self, cursors: TopicCursor);
25}
26
27impl<F: Fn(TopicCursor) + MaybeSend + MaybeSync> ReceiptSink for F {
28 fn acknowledge(&self, cursors: TopicCursor) {
29 self(cursors);
30 }
31}
32
33trait ConnectionSource: MaybeSend + MaybeSync {
34 fn is_connected(&self) -> bool;
35}
36
37impl<F: Fn() -> bool + MaybeSend + MaybeSync> ConnectionSource for F {
38 fn is_connected(&self) -> bool {
39 self()
40 }
41}
42
43pub struct IncomingSubscription<E> {
45 pub events: BoxDynStream<'static, Result<IncomingEvent, E>>,
47 receipt: Box<dyn ReceiptSink>,
48 connection: Option<Box<dyn ConnectionSource>>,
49}
50
51impl<E> IncomingSubscription<E> {
52 pub fn new(
54 events: BoxDynStream<'static, Result<IncomingEvent, E>>,
55 receipt: impl Fn(TopicCursor) + MaybeSend + MaybeSync + 'static,
56 ) -> Self {
57 Self {
58 events,
59 receipt: Box::new(receipt),
60 connection: None,
61 }
62 }
63
64 pub fn with_connection_source(
66 mut self,
67 connection: impl Fn() -> bool + MaybeSend + MaybeSync + 'static,
68 ) -> Self {
69 self.connection = Some(Box::new(connection));
70 self
71 }
72
73 pub fn is_connected(&self) -> bool {
75 self.connection
76 .as_ref()
77 .is_none_or(|connection| connection.is_connected())
78 }
79
80 pub fn acknowledge_received(&self, cursors: TopicCursor) {
83 self.receipt.acknowledge(cursors);
84 }
85
86 pub fn map_error<U, F>(self, map: F) -> IncomingSubscription<U>
88 where
89 E: 'static,
90 U: 'static,
91 F: Fn(E) -> U + MaybeSend + 'static,
92 {
93 IncomingSubscription {
94 events: Box::pin(self.events.map(move |event| event.map_err(&map))),
95 receipt: self.receipt,
96 connection: self.connection,
97 }
98 }
99}
100
101#[derive(Clone, Debug)]
103pub struct OrderedEnvelopeBatch {
104 pub topic: Topic,
106 pub after: Cursor,
108 pub envelopes: Vec<ReceivedEnvelope>,
110}
111
112#[derive(Clone, Debug)]
114pub enum IncomingEvent {
115 Registered {
117 starts: TopicCursor,
119 targets: TopicCursor,
121 },
122 OrderedBatch(OrderedEnvelopeBatch),
124 Disconnected,
126}