use dioxus_core::prelude::{Runtime, RuntimeGuard, ScopeId}; use rustc_hash::FxHashMap; use std::{cell::RefCell, rc::Rc}; use wry::{http::Request, RequestAsyncResponder}; /// pub type AssetRequest = Request>; pub struct AssetHandler { f: Box, scope: ScopeId, } #[derive(Clone)] pub struct AssetHandlerRegistry { dom_rt: Rc, handlers: Rc>>, } impl AssetHandlerRegistry { pub fn new(dom_rt: Rc) -> Self { AssetHandlerRegistry { dom_rt, handlers: Default::default(), } } pub fn has_handler(&self, name: &str) -> bool { self.handlers.borrow().contains_key(name) } pub fn handle_request( &self, name: &str, request: AssetRequest, responder: RequestAsyncResponder, ) { if let Some(handler) = self.handlers.borrow().get(name) { // Push the runtime onto the stack let _guard = RuntimeGuard::new(self.dom_rt.clone()); // And run the handler in the scope of the component that created it self.dom_rt .on_scope(handler.scope, || (handler.f)(request, responder)); } } pub fn register_handler( &self, name: String, f: Box, scope: ScopeId, ) { self.handlers .borrow_mut() .insert(name, AssetHandler { f, scope }); } pub fn remove_handler(&self, name: &str) -> Option { self.handlers.borrow_mut().remove(name) } }