From 6246a7097b5598df48073885d9f9d3fae967bac6 Mon Sep 17 00:00:00 2001 From: Daniel Paoliello Date: Sun, 16 Mar 2025 11:13:55 -0700 Subject: [PATCH] Switch custom protocol handler to async --- src/main.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0374a8d..eb8aea5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use tao::{ window::WindowBuilder, }; use tokio::{sync::mpsc::channel, task, time::Instant}; -use wry::{WebViewBuilder, WebViewId}; +use wry::{RequestAsyncResponder, WebViewBuilder, WebViewId}; use crate::auth::AuthMessage; @@ -31,15 +31,20 @@ const ON_ERROR_REFRESH_TIME: Duration = Duration::from_secs(1); const ITEM_LIST_REFRESH_TIME: Duration = Duration::from_secs(60 * 60); fn protocol_handler( - _: WebViewId, + _: WebViewId<'_>, request: wry::http::Request>, -) -> wry::http::Response> { + responder: RequestAsyncResponder, +) { let path = CACHE_DIRECTORY.join(&request.uri().path()[1..]); - let content = Cow::Owned(std::fs::read(path).unwrap()); - wry::http::Response::builder() - .header(wry::http::header::CACHE_CONTROL, "no-store") - .body(content) - .unwrap() + task::spawn(async move { + let content = Cow::Owned(tokio::fs::read(path).await.unwrap()); + responder.respond( + wry::http::Response::builder() + .header(wry::http::header::CACHE_CONTROL, "no-store") + .body(content) + .unwrap(), + ) + }); } static CACHE_DIRECTORY: LazyLock = @@ -66,7 +71,7 @@ fn main() -> Result<(), wry::Error> { }; let builder = WebViewBuilder::new() - .with_custom_protocol("slideshow".to_string(), protocol_handler) + .with_asynchronous_custom_protocol("slideshow".to_string(), protocol_handler) .with_ipc_handler(handler) .with_accept_first_mouse(true);