1use crate::{
4 conversation::ConversationId, device::DeviceId, identity::UserId, message::MessageEnvelope,
5 sync::SyncCursor, Result,
6};
7use std::fmt::Debug;
8use std::future::Future;
9use std::pin::Pin;
10use std::sync::Arc;
11
12#[cfg(not(target_arch = "wasm32"))]
15pub type TransportFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T>> + Send + 'a>>;
16#[cfg(target_arch = "wasm32")]
17pub type TransportFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T>> + 'a>>;
18
19pub trait Transport: Send + Sync + Debug {
22 fn send<'a>(&'a self, envelope: MessageEnvelope) -> TransportFuture<'a, ()>;
24
25 fn fetch_since<'a>(
27 &'a self,
28 conversation_id: ConversationId,
29 cursor: SyncCursor,
30 limit: u32,
31 ) -> TransportFuture<'a, Vec<MessageEnvelope>>;
32
33 fn subscribe<'a>(
35 &'a self,
36 callback: Arc<dyn Fn(MessageEnvelope) + Send + Sync>,
37 ) -> TransportFuture<'a, TransportSubscription>;
38
39 fn discover_devices<'a>(
41 &'a self,
42 user_id: UserId,
43 ) -> TransportFuture<'a, Vec<DiscoveredDevice>>;
44}
45
46#[derive(Debug, Clone)]
47pub struct DiscoveredDevice {
48 pub device_id: DeviceId,
49 pub key_package: Vec<u8>, }
51
52pub struct TransportSubscription {
54 pub(crate) cancel: Box<dyn FnOnce() + Send>,
55}
56
57impl std::fmt::Debug for TransportSubscription {
58 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59 f.write_str("TransportSubscription")
60 }
61}
62
63impl TransportSubscription {
64 pub fn new(cancel: impl FnOnce() + Send + 'static) -> Self {
65 Self {
66 cancel: Box::new(cancel),
67 }
68 }
69}
70
71impl Drop for TransportSubscription {
72 fn drop(&mut self) {
73 let cancel = std::mem::replace(&mut self.cancel, Box::new(|| ()));
75 cancel();
76 }
77}