Improve connection handling
Signed-off-by: trivernis <trivernis@protonmail.com>pull/4/head
parent
68ffdc323b
commit
5e4d6e098f
File diff suppressed because it is too large
Load Diff
@ -1 +1,17 @@
|
||||
use crate::context::Context;
|
||||
use crate::error::AppResult;
|
||||
|
||||
pub mod repo;
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn emit_info(context: tauri::State<'_, Context>) -> AppResult<()> {
|
||||
let ipc = context.ipc.read().await;
|
||||
if let Some(ipc) = &*ipc {
|
||||
ipc.emitter.emit("info", ()).await?;
|
||||
println!("Emitted info event.");
|
||||
} else {
|
||||
println!("No IPC Context");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -0,0 +1 @@
|
||||
use mediarepo::requests::AddFileRequest;
|
@ -0,0 +1,45 @@
|
||||
mod files;
|
||||
|
||||
use mediarepo::responses::InfoResponse;
|
||||
use rmp_ipc::context::{Context as IPCContext, Context};
|
||||
use rmp_ipc::{Event, IPCBuilder};
|
||||
use rmp_ipc::error::Result;
|
||||
use rmp_ipc::error_event::{ERROR_EVENT_NAME, ErrorEventData};
|
||||
use tauri::Window;
|
||||
use typemap_rev::TypeMapKey;
|
||||
use crate::error::AppResult;
|
||||
|
||||
pub struct WindowKey;
|
||||
|
||||
impl TypeMapKey for WindowKey {
|
||||
type Value = Window;
|
||||
}
|
||||
|
||||
pub async fn build_ipc_context(window: Window, address: &str) -> AppResult<IPCContext> {
|
||||
let ctx = IPCBuilder::new()
|
||||
.address(address)
|
||||
.insert::<WindowKey>(window)
|
||||
.on(ERROR_EVENT_NAME, |c, e|Box::pin(handle_error(c, e)))
|
||||
.on("info", |c, e| Box::pin(handle_info(c, e)))
|
||||
.build_client().await?;
|
||||
|
||||
Ok(ctx)
|
||||
}
|
||||
|
||||
async fn handle_error(ctx: &Context, event: Event) -> Result<()> {
|
||||
let error_data = event.data::<ErrorEventData>()?;
|
||||
let data = ctx.data.read().await;
|
||||
let window = data.get::<WindowKey>().unwrap();
|
||||
window.emit("error", error_data).expect("Failed to emit error event");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_info(ctx: &Context, event: Event) -> Result<()> {
|
||||
let info_data = event.data::<InfoResponse>()?;
|
||||
let data = ctx.data.read().await;
|
||||
let window = data.get::<WindowKey>().unwrap();
|
||||
window.emit("info", info_data).expect("Failed to emit info event");
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue