use hyphae::{Cell, CellImmutable};
#[cfg(not(target_arch = "wasm32"))]
mod native;
#[cfg(not(target_arch = "wasm32"))]
pub use native::AutoReconnectSocket;
#[cfg(target_arch = "wasm32")]
mod wasm;
#[cfg(target_arch = "wasm32")]
pub use wasm::WasmSocket;
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, ts_rs::TS)]
#[ts(export)]
#[serde(rename_all = "camelCase", tag = "type", content = "data")]
pub enum SocketConnectionStatus {
Idle,
Disconnected,
Connecting(String),
Reconnecting(String),
Connected(String),
}
#[derive(Clone, Debug)]
pub enum WsFrame {
Text(String),
Binary(Vec<u8>),
}
pub struct CallbackGuard {
drop_fn: Option<Box<dyn FnOnce() + Send + Sync>>,
}
impl CallbackGuard {
pub fn new(drop_fn: impl FnOnce() + Send + Sync + 'static) -> Self {
Self {
drop_fn: Some(Box::new(drop_fn)),
}
}
pub fn noop() -> Self {
Self { drop_fn: None }
}
}
impl Drop for CallbackGuard {
fn drop(&mut self) {
if let Some(f) = self.drop_fn.take() {
f();
}
}
}
pub trait SocketTransport: Send + Sync + 'static {
fn set_addr(&self, addr: Option<String>);
fn close(&self);
fn intended_connection_state(&self) -> Cell<SocketConnectionStatus, CellImmutable>;
fn actual_connection_state(&self) -> Cell<SocketConnectionStatus, CellImmutable>;
fn send(&self, frame: WsFrame) -> Result<(), String>;
fn read_rx(&self) -> flume::Receiver<WsFrame>;
}