Add custom asynchronous asset handlers

This commit is contained in:
Will Crichton committed 2023-12-15 11:16:25 -08:00
1 parent 5fdff4b7ed
commit d5ec22a26f
4 files changed
+194 -19

No files matched your search

+141 -2
View File
@@ -1,13 +1,25 @@
use dioxus_core::ScopeState;
use dioxus_interpreter_js::{COMMON_JS, INTERPRETER_JS};
use std::{
borrow::Cow,
collections::HashMap,
future::Future,
path::{Path, PathBuf},
pin::Pin,
rc::Rc,
sync::Arc,
};
use tokio::{
runtime::Handle,
sync::{OnceCell, RwLock},
};
use wry::{
http::{status::StatusCode, Request, Response},
Result,
};
use crate::{use_window, DesktopContext};
fn module_loader(root_name: &str) -> String {
let js = INTERPRETER_JS.replace(
"/*POST_HANDLE_EDITS*/",
@@ -51,12 +63,132 @@ fn module_loader(root_name: &str) -> String {
)
}
pub(super) fn desktop_handler(
/// An arbitrary asset is an HTTP response containing a binary body.
pub type AssetResponse = Response<Cow<'static, [u8]>>;
/// A future that returns an [`AssetResponse`]. This future may be spawned in a new thread,
/// so it must be [`Send`], [`Sync`], and `'static`.
pub trait AssetFuture: Future<Output = Option<AssetResponse>> + Send + Sync + 'static {}
impl<T: Future<Output = Option<AssetResponse>> + Send + Sync + 'static> AssetFuture for T {}
/// A handler that takes an asset [`Path`] and returns a future that loads the path.
/// This handler is stashed indefinitely in a context object, so it must be `'static`.
pub trait AssetHandler<F: AssetFuture>: Fn(&Path) -> F + Send + Sync + 'static {}
impl<F: AssetFuture, T: Fn(&Path) -> F + Send + Sync + 'static> AssetHandler<F> for T {}
/// An identifier for a registered asset handler, returned by [`AssetHandlerRegistry::register_handler`].
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct AssetHandlerId(usize);
struct AssetHandlerRegistryInner {
handlers: HashMap<
AssetHandlerId,
Box<dyn Fn(&Path) -> Pin<Box<dyn AssetFuture>> + Send + Sync + 'static>,
>,
counter: AssetHandlerId,
}
#[derive(Clone)]
pub struct AssetHandlerRegistry(Arc<RwLock<AssetHandlerRegistryInner>>);
impl AssetHandlerRegistry {
pub fn new() -> Self {
AssetHandlerRegistry(Arc::new(RwLock::new(AssetHandlerRegistryInner {
handlers: HashMap::new(),
counter: AssetHandlerId(0),
})))
}
pub async fn register_handler<F: AssetFuture>(
&self,
f: impl AssetHandler<F>,
) -> AssetHandlerId {
let mut registry = self.0.write().await;
let id = registry.counter;
registry
.handlers
.insert(id, Box::new(move |path| Box::pin(f(path))));
registry.counter.0 += 1;
id
}
pub async fn remove_handler(&self, id: AssetHandlerId) -> Option<()> {
let mut registry = self.0.write().await;
registry.handlers.remove(&id).map(|_| ())
}
pub async fn try_handlers(&self, path: &Path) -> Option<AssetResponse> {
let registry = self.0.read().await;
for handler in registry.handlers.values() {
if let Some(response) = handler(path).await {
return Some(response);
}
}
None
}
}
/// A handle to a registered asset handler.
pub struct AssetHandlerHandle {
desktop: DesktopContext,
handler_id: Rc<OnceCell<AssetHandlerId>>,
}
impl AssetHandlerHandle {
/// Returns the [`AssetHandlerId`] for this handle.
///
/// Because registering an ID is asynchronous, this may return `None` if the
/// registration has not completed yet.
pub fn handler_id(&self) -> Option<AssetHandlerId> {
self.handler_id.get().copied()
}
}
impl Drop for AssetHandlerHandle {
fn drop(&mut self) {
let cell = Rc::clone(&self.handler_id);
let desktop = Rc::clone(&self.desktop);
tokio::task::block_in_place(move || {
Handle::current().block_on(async move {
if let Some(id) = cell.get() {
desktop.asset_handlers.remove_handler(*id).await;
}
})
});
}
}
/// Provide a callback to handle asset loading yourself.
///
/// The callback takes a path as requested by the web view, and it should return `Some(response)`
/// if you want to load the asset, and `None` if you want to fallback on the default behavior.
pub fn use_asset_handler<F: AssetFuture>(
cx: &ScopeState,
handler: impl AssetHandler<F>,
) -> &AssetHandlerHandle {
let desktop = Rc::clone(&use_window(cx));
cx.use_hook(|| {
let handler_id = Rc::new(OnceCell::new());
let handler_id_ref = Rc::clone(&handler_id);
let desktop_ref = Rc::clone(&desktop);
cx.push_future(async move {
let id = desktop.asset_handlers.register_handler(handler).await;
handler_id.set(id).unwrap();
});
AssetHandlerHandle {
desktop: desktop_ref,
handler_id: handler_id_ref,
}
})
}
pub(super) async fn desktop_handler(
request: &Request<Vec<u8>>,
custom_head: Option<String>,
custom_index: Option<String>,
root_name: &str,
) -> Result<Response<Cow<'static, [u8]>>> {
asset_handlers: &AssetHandlerRegistry,
) -> Result<AssetResponse> {
// If the request is for the root, we'll serve the index.html file.
if request.uri().path() == "/" {
// If a custom index is provided, just defer to that, expecting the user to know what they're doing.
@@ -96,6 +228,13 @@ pub(super) fn desktop_handler(
.expect("expected URL to be UTF-8 encoded");
let path = PathBuf::from(&*decoded);
// If the user provided a custom asset handler, then call it and return the response
// if the request was handled.
if let Some(response) = asset_handlers.try_handlers(&path).await {
return Ok(response);
}
// Else, try to serve a file from the filesystem.
// If the path is relative, we'll try to serve it from the assets directory.
let mut asset = get_asset_root()
.unwrap_or_else(|| Path::new(".").to_path_buf())