Pull out asset handler refactor from https://github.com/DioxusLabs/dioxus/pull/2972 (#3000)
This commit is contained in:
1 parent
95976d9a17
commit
f9c2f740c2
4 files changed
+15
-28
No files matched your search
@@ -1,4 +1,4 @@
|
||||
use dioxus_core::prelude::{Runtime, ScopeId};
|
||||
use dioxus_core::prelude::Callback;
|
||||
use rustc_hash::FxHashMap;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use wry::{http::Request, RequestAsyncResponder};
|
||||
@@ -7,20 +7,17 @@ use wry::{http::Request, RequestAsyncResponder};
|
||||
pub type AssetRequest = Request<Vec<u8>>;
|
||||
|
||||
pub struct AssetHandler {
|
||||
f: Box<dyn Fn(AssetRequest, RequestAsyncResponder) + 'static>,
|
||||
scope: ScopeId,
|
||||
f: Callback<(AssetRequest, RequestAsyncResponder)>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AssetHandlerRegistry {
|
||||
dom_rt: Rc<Runtime>,
|
||||
handlers: Rc<RefCell<FxHashMap<String, AssetHandler>>>,
|
||||
}
|
||||
|
||||
impl AssetHandlerRegistry {
|
||||
pub fn new(dom_rt: Rc<Runtime>) -> Self {
|
||||
pub fn new() -> Self {
|
||||
AssetHandlerRegistry {
|
||||
dom_rt,
|
||||
handlers: Default::default(),
|
||||
}
|
||||
}
|
||||
@@ -37,20 +34,16 @@ impl AssetHandlerRegistry {
|
||||
) {
|
||||
if let Some(handler) = self.handlers.borrow().get(name) {
|
||||
// And run the handler in the scope of the component that created it
|
||||
self.dom_rt
|
||||
.on_scope(handler.scope, || (handler.f)(request, responder));
|
||||
handler.f.call((request, responder));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_handler(
|
||||
&self,
|
||||
name: String,
|
||||
f: Box<dyn Fn(AssetRequest, RequestAsyncResponder) + 'static>,
|
||||
scope: ScopeId,
|
||||
f: Callback<(AssetRequest, RequestAsyncResponder)>,
|
||||
) {
|
||||
self.handlers
|
||||
.borrow_mut()
|
||||
.insert(name, AssetHandler { f, scope });
|
||||
self.handlers.borrow_mut().insert(name, AssetHandler { f });
|
||||
}
|
||||
|
||||
pub fn remove_handler(&self, name: &str) -> Option<AssetHandler> {
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::{
|
||||
AssetRequest, Config, WryEventHandler,
|
||||
};
|
||||
use dioxus_core::{
|
||||
prelude::{current_scope_id, ScopeId},
|
||||
prelude::{Callback, ScopeId},
|
||||
VirtualDom,
|
||||
};
|
||||
use std::rc::{Rc, Weak};
|
||||
@@ -236,14 +236,10 @@ impl DesktopService {
|
||||
pub fn register_asset_handler(
|
||||
&self,
|
||||
name: String,
|
||||
handler: Box<dyn Fn(AssetRequest, RequestAsyncResponder) + 'static>,
|
||||
scope: Option<ScopeId>,
|
||||
handler: impl Fn(AssetRequest, RequestAsyncResponder) + 'static,
|
||||
) {
|
||||
self.asset_handlers.register_handler(
|
||||
name,
|
||||
handler,
|
||||
scope.unwrap_or(current_scope_id().unwrap_or(ScopeId(0))),
|
||||
)
|
||||
self.asset_handlers
|
||||
.register_handler(name, Callback::new(move |(req, resp)| handler(req, resp)))
|
||||
}
|
||||
|
||||
/// Removes an asset handler by its identifier.
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::{
|
||||
ShortcutHandle, ShortcutRegistryError, WryEventHandler,
|
||||
};
|
||||
use dioxus_core::{
|
||||
prelude::{consume_context, current_scope_id, use_hook_with_cleanup, RuntimeGuard},
|
||||
prelude::{consume_context, use_hook_with_cleanup, RuntimeGuard},
|
||||
use_hook, Runtime,
|
||||
};
|
||||
|
||||
@@ -69,11 +69,9 @@ pub fn use_asset_handler(
|
||||
|
||||
use_hook_with_cleanup(
|
||||
|| {
|
||||
crate::window().asset_handlers.register_handler(
|
||||
name.to_string(),
|
||||
Box::new(move |asset, responder| cb((asset, responder))),
|
||||
current_scope_id().unwrap(),
|
||||
);
|
||||
crate::window()
|
||||
.asset_handlers
|
||||
.register_handler(name.to_string(), cb);
|
||||
|
||||
Rc::new(name.to_string())
|
||||
},
|
||||
|
||||
@@ -188,7 +188,7 @@ impl WebviewInstance {
|
||||
|
||||
let mut web_context = WebContext::new(cfg.data_dir.clone());
|
||||
let edit_queue = WryQueue::default();
|
||||
let asset_handlers = AssetHandlerRegistry::new(dom.runtime());
|
||||
let asset_handlers = AssetHandlerRegistry::new();
|
||||
let edits = WebviewEdits::new(dom.runtime(), edit_queue.clone());
|
||||
let file_hover = NativeFileHover::default();
|
||||
let headless = !cfg.window.window.visible;
|
||||
|
||||
Reference in new issue
Block a user