Skip to content

Commit 180167a

Browse files
not-elmnot-elm
andauthored
Fix responsor (#56)
* fix: fixed responsor Fixed an issue where responses were not returned correctly when multiple resources (such as icons) were requested on the same page. * fmt --------- Co-authored-by: not-elm <elmgameinfo@gmail.com>
1 parent 9a647dd commit 180167a

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

crates/bevy_webview_wry/src/webview/load_webview.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::webview::WryWebViews;
66
use crate::webview::handlers::{HandlerQueries, WryEventParams};
77
use crate::webview::load_webview::ipc::IpcHandlerParams;
88
use crate::webview::load_webview::protocol::feed_uri;
9-
use crate::webview::protocol::WryRequestSender;
9+
use crate::webview::protocol::{WryRequestSender, WryResponseHandles};
1010
use bevy::prelude::*;
1111
use bevy::winit::WinitWindows;
1212
use bevy_webview_core::bundle::embedding::{Bounds, EmbedWithin};
@@ -120,7 +120,7 @@ fn load_web_views(
120120
}
121121
commands
122122
.entity(webview_entity)
123-
.insert(WebviewInitialized(()));
123+
.insert((WebviewInitialized(()), WryResponseHandles::default()));
124124
web_views.0.insert(webview_entity, webview);
125125
}
126126
}

crates/bevy_webview_wry/src/webview/protocol.rs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ impl Plugin for CustomProtocolPlugin {
3434
}
3535
}
3636

37+
#[derive(Default, Component, Deref, DerefMut)]
38+
pub(crate) struct WryResponseHandles(HashMap<AssetId<WryResponseBody>, WryResponseHandle>);
39+
3740
pub struct WryRequest {
3841
pub webview: Entity,
3942
pub path: PathBuf,
@@ -51,6 +54,7 @@ pub struct WryResponseMap(pub HashMap<WryRequestArgs, RequestAsyncResponder>);
5154
fn start_load(
5255
mut commands: Commands,
5356
mut map: NonSendMut<WryResponseMap>,
57+
mut response_handles: Query<&mut WryResponseHandles>,
5458
rx: NonSend<WryRequestReceiver>,
5559
asset_server: Res<AssetServer>,
5660
) {
@@ -59,53 +63,66 @@ fn start_load(
5963
csp: request.csp,
6064
path: request.path.clone(),
6165
};
62-
commands.entity(request.webview).try_insert((
63-
args.clone(),
64-
WryResponseHandle(asset_server.load(request.path.clone())),
65-
));
66+
let Ok(mut handles) = response_handles.get_mut(request.webview) else {
67+
continue;
68+
};
69+
let response_handle = WryResponseHandle(asset_server.load(request.path.clone()));
70+
handles.insert(response_handle.0.id(), response_handle.clone());
71+
commands
72+
.entity(request.webview)
73+
.with_child((response_handle, args.clone()));
6674
map.0.insert(args, request.responder);
6775
}
6876
}
6977

7078
fn response(
7179
mut commands: Commands,
72-
responses: ResMut<Assets<WryResponseBody>>,
73-
mut handles: Query<(Entity, &WryRequestArgs, &WryResponseHandle)>,
7480
mut map: NonSendMut<WryResponseMap>,
81+
responses: ResMut<Assets<WryResponseBody>>,
82+
requests: Query<(Entity, &WryRequestArgs, &WryResponseHandle)>,
7583
) {
76-
for (webview_entity, args, handle) in handles.iter_mut() {
84+
for (request_entity, args, handle) in requests.iter() {
7785
let Some(response_body) = responses.get(handle.0.id()) else {
7886
continue;
7987
};
8088
let Some(responder) = map.0.remove(args) else {
8189
continue;
8290
};
8391
responder.respond(convert_to_response(response_body.0.clone(), args));
84-
commands
85-
.entity(webview_entity)
86-
.try_remove::<WryRequestArgs>();
92+
commands.entity(request_entity).despawn();
8793
}
8894
}
8995

9096
#[cfg(feature = "hot-reload")]
9197
fn hot_reload(
9298
mut er: EventReader<AssetEvent<WryResponseBody>>,
9399
wry_webviews: NonSend<crate::prelude::WryWebViews>,
94-
webviews: Query<(Entity, &WryResponseHandle), With<Webview>>,
100+
webviews: Query<(Entity, &WryResponseHandles), With<Webview>>,
95101
asset_server: Res<AssetServer>,
96102
) {
97-
for event in er.read() {
98-
if let AssetEvent::Modified { id } = event
99-
&& let Some(webview_entity) = webviews
100-
.iter()
101-
.find_map(|(entity, handle)| (id == &handle.0.id()).then_some(entity))
102-
&& let Some(webview) = wry_webviews.get(&webview_entity)
103-
{
104-
if let Some(path) = asset_server.get_path(*id) {
103+
let modified_ids = er
104+
.read()
105+
.filter_map(|event| {
106+
if let AssetEvent::Modified { id } = event {
107+
Some(id)
108+
} else {
109+
None
110+
}
111+
})
112+
.collect::<Vec<_>>();
113+
for (webview_entity, id) in webviews.iter().filter_map(|(entity, handles)| {
114+
handles
115+
.keys()
116+
.find_map(|id| modified_ids.contains(&id).then_some((entity, id)))
117+
}) {
118+
if let Some(webview) = wry_webviews.get(&webview_entity) {
119+
if let Some(path) = asset_server.get_path(id.untyped()) {
105120
info!("Reloading webview {webview_entity}: {path:?}");
106121
}
107122
if let Err(e) = webview.reload() {
108123
warn!("Failed to reload webview {webview_entity}: {e}");
124+
} else {
125+
info!("Reloaded webview {webview_entity}");
109126
}
110127
}
111128
}

crates/bevy_webview_wry/src/webview/protocol/asset.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::LazyLock;
99
use wry::http::Response;
1010
use wry::http::header::{CONTENT_SECURITY_POLICY, CONTENT_TYPE};
1111

12-
#[derive(Debug, Component)]
12+
#[derive(Debug, Component, Clone)]
1313
pub struct WryResponseHandle(pub Handle<WryResponseBody>);
1414

1515
#[derive(Debug, Asset, TypePath)]
@@ -156,6 +156,17 @@ const EXTENSION_MAP: &[(&[&str], &str)] = &[
156156
(&["wav"], "audio/wav"),
157157
(&["weba"], "audio/webm"),
158158
(&["webm"], "video/web"),
159+
(&["woff"], "font/woff"),
160+
(&["woff2"], "font/woff2"),
161+
(&["xhtml"], "application/xhtml+xml"),
162+
(&["xls"], "application/vnd.ms-excel"),
163+
(
164+
&["xlsx"],
165+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
166+
),
167+
(&["xml"], "application/xml"),
168+
(&["xul"], "application/vnd.mozilla.xul+xml"),
169+
(&["7z"], "application/x-7z-compressed"),
159170
];
160171

161172
static EXTENSIONS: LazyLock<Vec<&str>> = LazyLock::new(|| {

0 commit comments

Comments
 (0)