xmtp_proto/types/
incoming_event.rs1use super::{Cursor, Topic, TopicCursor};
2use crate::backend_v1::ServerEnvelope;
3use futures::StreamExt;
4use xmtp_common::{BoxDynStream, MaybeSend, MaybeSync};
5
6#[derive(Clone, Copy, Debug)]
8pub struct IncomingBatchLimits {
9 pub max_rows: usize,
11 pub max_bytes: usize,
13}
14
15trait ReceiptSink: MaybeSend + MaybeSync {
16 fn acknowledge(&self, cursors: TopicCursor);
17}
18
19impl<F: Fn(TopicCursor) + MaybeSend + MaybeSync> ReceiptSink for F {
20 fn acknowledge(&self, cursors: TopicCursor) {
21 self(cursors);
22 }
23}
24
25pub struct IncomingSubscription<E> {
27 pub events: BoxDynStream<'static, Result<IncomingEvent, E>>,
29 receipt: Box<dyn ReceiptSink>,
30}
31
32impl<E> IncomingSubscription<E> {
33 pub fn new(
35 events: BoxDynStream<'static, Result<IncomingEvent, E>>,
36 receipt: impl Fn(TopicCursor) + MaybeSend + MaybeSync + 'static,
37 ) -> Self {
38 Self {
39 events,
40 receipt: Box::new(receipt),
41 }
42 }
43
44 pub fn acknowledge_received(&self, cursors: TopicCursor) {
47 self.receipt.acknowledge(cursors);
48 }
49
50 pub fn map_error<U, F>(self, map: F) -> IncomingSubscription<U>
52 where
53 E: 'static,
54 U: 'static,
55 F: Fn(E) -> U + MaybeSend + 'static,
56 {
57 IncomingSubscription {
58 events: Box::pin(self.events.map(move |event| event.map_err(&map))),
59 receipt: self.receipt,
60 }
61 }
62}
63
64#[derive(Clone, Debug)]
66pub struct OrderedEnvelopeBatch {
67 pub topic: Topic,
69 pub after: Cursor,
71 pub envelopes: Vec<ServerEnvelope>,
73}
74
75#[derive(Clone, Debug)]
77pub enum IncomingEvent {
78 Registered {
80 starts: TopicCursor,
82 targets: TopicCursor,
84 },
85 OrderedBatch(OrderedEnvelopeBatch),
87 Disconnected,
89}