use crate::{
conversation::ConversationId, device::DeviceId, identity::UserId, message::MessageEnvelope,
sync::SyncCursor, Result,
};
use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
#[cfg(not(target_arch = "wasm32"))]
pub type TransportFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T>> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type TransportFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T>> + 'a>>;
pub trait Transport: Send + Sync + Debug {
fn send<'a>(&'a self, envelope: MessageEnvelope) -> TransportFuture<'a, ()>;
fn fetch_since<'a>(
&'a self,
conversation_id: ConversationId,
cursor: SyncCursor,
limit: u32,
) -> TransportFuture<'a, Vec<MessageEnvelope>>;
fn subscribe<'a>(
&'a self,
callback: Arc<dyn Fn(MessageEnvelope) + Send + Sync>,
) -> TransportFuture<'a, TransportSubscription>;
fn discover_devices<'a>(
&'a self,
user_id: UserId,
) -> TransportFuture<'a, Vec<DiscoveredDevice>>;
}
#[derive(Debug, Clone)]
pub struct DiscoveredDevice {
pub device_id: DeviceId,
pub key_package: Vec<u8>, }
pub struct TransportSubscription {
pub(crate) cancel: Box<dyn FnOnce() + Send>,
}
impl std::fmt::Debug for TransportSubscription {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("TransportSubscription")
}
}
impl TransportSubscription {
pub fn new(cancel: impl FnOnce() + Send + 'static) -> Self {
Self {
cancel: Box::new(cancel),
}
}
}
impl Drop for TransportSubscription {
fn drop(&mut self) {
let cancel = std::mem::replace(&mut self.cancel, Box::new(|| ()));
cancel();
}
}