use std::collections::BTreeMap;
use std::time::Duration;
use serde_json::Value;
use tauri::ipc::Channel;
use tauri::{Runtime, State, Webview};
use crate::models::{
Args, ClientInfo, ConnectOptions, Link, Message, State as ConnState, Subscribed,
};
use crate::registry::{SocketIo, SubscribeArgs, SubscriptionId};
use crate::{Error, Result};
fn sub_args<R: Runtime>(
webview: &Webview<R>,
events: Option<Vec<String>>,
channel: Channel<Message>,
) -> SubscribeArgs {
SubscribeArgs {
events,
channel,
webview: Some(webview.label().to_owned()),
}
}
fn millis(value: &Value, what: &str) -> Result<Duration> {
value
.as_f64()
.filter(|v| v.is_finite() && *v >= 0.0)
.map(|v| Duration::from_millis(v as u64))
.ok_or_else(|| {
Error::InvalidOptions(format!("{what} must be a finite number of milliseconds"))
})
}
#[tauri::command]
pub(crate) async fn connect<R: Runtime>(
webview: Webview<R>,
io: State<'_, SocketIo>,
id: String,
options: Value,
events: Option<Vec<String>>,
channel: Channel<Message>,
) -> Result<Subscribed> {
let options = ConnectOptions::parse(options)?;
let (url, path) = (options.url.clone(), options.path.clone());
let (builder, dynamic) = options.into_builder()?;
let sub = sub_args(&webview, events, channel);
let (subscription, state) = io
.connect_from_webview(id, &url, path.as_deref(), builder, dynamic, sub)
.await?;
Ok(Subscribed {
subscription,
state,
})
}
#[tauri::command]
pub(crate) async fn disconnect(io: State<'_, SocketIo>, id: String) -> Result<()> {
io.disconnect(&id).await
}
#[tauri::command]
pub(crate) fn emit(io: State<'_, SocketIo>, id: String, event: String, args: Args) -> Result<()> {
Ok(io.full(&id)?.emit(event, args)?)
}
#[tauri::command]
pub(crate) async fn emit_with_ack(
io: State<'_, SocketIo>,
id: String,
event: String,
args: Args,
timeout: Value,
) -> Result<Args> {
let timeout = millis(&timeout, "timeout")?;
let client = io.full(&id)?;
Ok(client.emit_with_ack(event, args, timeout).await?.into())
}
#[tauri::command]
pub(crate) fn ack<R: Runtime>(
webview: Webview<R>,
io: State<'_, SocketIo>,
id: String,
ack_id: u64,
args: Args,
) -> Result<()> {
io.ack(&id, ack_id, webview.label(), args.into())
}
#[tauri::command]
pub(crate) fn subscribe<R: Runtime>(
webview: Webview<R>,
io: State<'_, SocketIo>,
id: String,
events: Option<Vec<String>>,
channel: Channel<Message>,
) -> Result<Subscribed> {
let (subscription, state) = io.subscribe_with(&id, sub_args(&webview, events, channel))?;
Ok(Subscribed {
subscription,
state,
})
}
#[tauri::command]
pub(crate) fn unsubscribe<R: Runtime>(
webview: Webview<R>,
io: State<'_, SocketIo>,
id: String,
subscription: SubscriptionId,
) -> bool {
io.unsubscribe_from(&id, subscription, Some(webview.label()))
}
#[tauri::command]
pub(crate) fn list(io: State<'_, SocketIo>) -> Vec<ClientInfo> {
io.list()
}
#[tauri::command]
pub(crate) fn state(io: State<'_, SocketIo>, id: String) -> Result<ConnState> {
Ok(ConnState::from(&io.any(&id)?.state()))
}
#[tauri::command]
pub(crate) fn link_info(io: State<'_, SocketIo>, id: String) -> Result<Link> {
Ok(Link::from(&io.any(&id)?.link_info()))
}
#[tauri::command]
pub(crate) fn set_auth(io: State<'_, SocketIo>, id: String, auth: Value) -> Result<()> {
if !auth.is_object() {
return Err(Error::InvalidOptions("auth must be an object".into()));
}
io.configure(&id, |d| d.auth = Some(auth))
}
#[tauri::command]
pub(crate) fn set_headers(
io: State<'_, SocketIo>,
id: String,
headers: BTreeMap<String, String>,
) -> Result<()> {
crate::models::check_headers(&headers)?;
io.configure(&id, |d| d.headers = headers)
}
#[tauri::command]
pub(crate) fn set_query(
io: State<'_, SocketIo>,
id: String,
query: BTreeMap<String, String>,
) -> Result<()> {
io.configure(&id, |d| d.query = query)
}
#[tauri::command]
pub(crate) async fn reconnect(io: State<'_, SocketIo>, id: String) -> Result<ConnState> {
let client = io.reconnect(&id).await?;
Ok(ConnState::from(&client.state()))
}