mirror of
https://github.com/storytold/storyteller-bevy.git
synced 2026-10-23 08:16:05 +00:00
clippy + fmt + comments
This commit is contained in:
+47
-35
@@ -6,7 +6,7 @@ use bevy::{
|
|||||||
prelude::*,
|
prelude::*,
|
||||||
utils::BoxedFuture,
|
utils::BoxedFuture,
|
||||||
};
|
};
|
||||||
use futures::Future;
|
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
|
|
||||||
use super::response::GetMediaFileSuccessResponse;
|
use super::response::GetMediaFileSuccessResponse;
|
||||||
@@ -82,7 +82,6 @@ impl RemoteAssetReader {
|
|||||||
|
|
||||||
#[cfg(feature = "wasm")]
|
#[cfg(feature = "wasm")]
|
||||||
async fn load_local<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
async fn load_local<'a>(&'a self, path: &'a Path) -> Result<Box<Reader<'a>>, AssetReaderError> {
|
||||||
|
|
||||||
let token = path
|
let token = path
|
||||||
.ext_file_prefix()
|
.ext_file_prefix()
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
@@ -93,11 +92,12 @@ impl RemoteAssetReader {
|
|||||||
})?
|
})?
|
||||||
.to_str()
|
.to_str()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let bytes =
|
let bytes = wasm::LoadLocalFromIdb::new(token);
|
||||||
wasm::LoadLocalFromIdb::new(token);
|
|
||||||
info!("Sent request to bevy");
|
info!("Sent request to bevy");
|
||||||
let bytes = bytes.await.map_err(|e| std::io::Error::new(ErrorKind::Other, format!("{e:?}")))?;
|
let bytes = bytes
|
||||||
|
.await
|
||||||
|
.map_err(|e| std::io::Error::new(ErrorKind::Other, format!("{e:?}")))?;
|
||||||
|
|
||||||
let reader: Box<Reader> = Box::new(VecReader::new(bytes));
|
let reader: Box<Reader> = Box::new(VecReader::new(bytes));
|
||||||
Ok(reader)
|
Ok(reader)
|
||||||
@@ -113,17 +113,17 @@ impl AssetReader for RemoteAssetReader {
|
|||||||
info!("wasm mode not detected");
|
info!("wasm mode not detected");
|
||||||
// NB: Headless mode (server rendering) is running without a Tokio runtime.
|
// NB: Headless mode (server rendering) is running without a Tokio runtime.
|
||||||
// The reqwest HTTP client requires async-compat to function at runtime.
|
// The reqwest HTTP client requires async-compat to function at runtime.
|
||||||
Box::pin(Compat::new( async move { self.load(path).await }))
|
Box::pin(Compat::new(async move { self.load(path).await }))
|
||||||
} else {
|
} else {
|
||||||
info!("wasm mode");
|
info!("wasm mode");
|
||||||
if let Some(window) = web_sys::window() {
|
if let Some(window) = web_sys::window() {
|
||||||
if let Ok(search) = window.location().search() {
|
if let Ok(search) = window.location().search() {
|
||||||
if search.contains("mock-api") {
|
if search.contains("mock-api") {
|
||||||
return Box::pin( async move { self.load_local(path).await });
|
return Box::pin(async move { self.load_local(path).await });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Box::pin( async move { self.load(path).await })
|
Box::pin(async move { self.load(path).await })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,16 +163,14 @@ impl IntoError<AssetReaderError> for reqwest::Error {
|
|||||||
mod wasm {
|
mod wasm {
|
||||||
use std::task::Waker;
|
use std::task::Waker;
|
||||||
|
|
||||||
use bevy::{log::info, utils::{HashMap, HashSet}};
|
use bevy::log::info;
|
||||||
use dashmap::{DashMap, DashSet};
|
use dashmap::DashMap;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use js_sys::Uint8Array;
|
use js_sys::Uint8Array;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use wasm_bindgen::{prelude::*, JsValue};
|
use wasm_bindgen::{prelude::*, JsValue};
|
||||||
use web_sys::{CustomEvent, CustomEventInit};
|
use web_sys::{CustomEvent, CustomEventInit};
|
||||||
|
|
||||||
use crate::wasm::{StaticBuffer, WasmMessageBuffer};
|
|
||||||
|
|
||||||
#[wasm_bindgen(typescript_custom_section)]
|
#[wasm_bindgen(typescript_custom_section)]
|
||||||
const TIMELINE_TYPES: &str = r#"
|
const TIMELINE_TYPES: &str = r#"
|
||||||
export interface RequestFileEvent extends CustomEvent {
|
export interface RequestFileEvent extends CustomEvent {
|
||||||
@@ -187,9 +185,11 @@ mod wasm {
|
|||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
static FILE_WAKERS: Lazy<DashMap<String, Waker>> = Lazy::new(|| DashMap::new());
|
// DashMap + Lazy allow us to create static, global hashmaps that work well concurrently
|
||||||
static FILE_RESPONSES : Lazy<DashMap<String, Result<Vec<u8>, String>>> = Lazy::new(|| DashMap::new());
|
static FILE_WAKERS: Lazy<DashMap<String, Waker>> = Lazy::new(DashMap::new);
|
||||||
|
static FILE_RESPONSES: Lazy<DashMap<String, Result<Vec<u8>, String>>> = Lazy::new(DashMap::new);
|
||||||
|
|
||||||
|
/// Notifies us that the requested file couldn't be received
|
||||||
#[wasm_bindgen(js_name = requestedFileFailed)]
|
#[wasm_bindgen(js_name = requestedFileFailed)]
|
||||||
pub fn requested_file_failed(token: String, error: JsValue) {
|
pub fn requested_file_failed(token: String, error: JsValue) {
|
||||||
FILE_RESPONSES.insert(token.clone(), Err(format!("Error: {error:?}")));
|
FILE_RESPONSES.insert(token.clone(), Err(format!("Error: {error:?}")));
|
||||||
@@ -199,6 +199,7 @@ mod wasm {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Provides us with the requested file's contents
|
||||||
#[wasm_bindgen(js_name = requestedFileReceived)]
|
#[wasm_bindgen(js_name = requestedFileReceived)]
|
||||||
pub fn requested_file_received(token: String, file: Uint8Array) {
|
pub fn requested_file_received(token: String, file: Uint8Array) {
|
||||||
info!("Received File From Mock API {token}");
|
info!("Received File From Mock API {token}");
|
||||||
@@ -210,30 +211,35 @@ mod wasm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub enum LoadLocalFromIdb {
|
pub enum LoadLocalFromIdb {
|
||||||
RequestingFile {
|
RequestingFile { token: String },
|
||||||
token: String
|
CheckingForFile { token: String },
|
||||||
},
|
|
||||||
CheckingForFile {
|
|
||||||
token: String
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LoadLocalFromIdb {
|
impl LoadLocalFromIdb {
|
||||||
pub fn new (file: impl ToString) -> Self {
|
pub fn new(file: impl ToString) -> Self {
|
||||||
Self::RequestingFile { token: file.to_string() }
|
Self::RequestingFile {
|
||||||
|
token: file.to_string(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We are implementing Future ourselves here because all the existing IndexedDb rust libraries
|
||||||
|
// rely on JsValue, which is not Send - preventing us from using it in a BoxedFuture.
|
||||||
|
// Instead, we ask the JS front end to grab the data from our mock API, and return it to us.
|
||||||
impl Future for LoadLocalFromIdb {
|
impl Future for LoadLocalFromIdb {
|
||||||
type Output = Result<Vec<u8>, String>;
|
type Output = Result<Vec<u8>, String>;
|
||||||
|
|
||||||
fn poll(mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
|
fn poll(
|
||||||
|
mut self: std::pin::Pin<&mut Self>,
|
||||||
|
cx: &mut std::task::Context<'_>,
|
||||||
|
) -> std::task::Poll<Self::Output> {
|
||||||
match self.as_ref().get_ref() {
|
match self.as_ref().get_ref() {
|
||||||
LoadLocalFromIdb::RequestingFile { token } => {
|
LoadLocalFromIdb::RequestingFile { token } => {
|
||||||
info!("Requesting file {token}");
|
info!("Requesting file {token}");
|
||||||
let Some(window) = web_sys::window() else {
|
let Some(window) = web_sys::window() else {
|
||||||
return std::task::Poll::Ready(Err("Couldn't find browser window".to_string()));
|
return std::task::Poll::Ready(Err(
|
||||||
|
"Couldn't find browser window".to_string()
|
||||||
|
));
|
||||||
};
|
};
|
||||||
let event = CustomEvent::new_with_event_init_dict(
|
let event = CustomEvent::new_with_event_init_dict(
|
||||||
"request-file",
|
"request-file",
|
||||||
@@ -247,28 +253,34 @@ mod wasm {
|
|||||||
Ok(e) => e,
|
Ok(e) => e,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return std::task::Poll::Ready(Err(err));
|
return std::task::Poll::Ready(Err(err));
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
FILE_WAKERS.insert(token.clone(), cx.waker().clone());
|
FILE_WAKERS.insert(token.clone(), cx.waker().clone());
|
||||||
if let Err(err) = window.document().ok_or(JsValue::null()).and_then(|document| document.dispatch_event(&event)) {
|
if let Err(err) = window
|
||||||
return std::task::Poll::Ready(Err(format!("Error Dispatching Message: {err:?}")));
|
.document()
|
||||||
|
.ok_or(JsValue::null())
|
||||||
|
.and_then(|document| document.dispatch_event(&event))
|
||||||
|
{
|
||||||
|
return std::task::Poll::Ready(Err(format!(
|
||||||
|
"Error Dispatching Message: {err:?}"
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
self.set(LoadLocalFromIdb::CheckingForFile { token: token.clone() });
|
self.set(LoadLocalFromIdb::CheckingForFile {
|
||||||
|
token: token.clone(),
|
||||||
|
});
|
||||||
std::task::Poll::Pending
|
std::task::Poll::Pending
|
||||||
},
|
}
|
||||||
LoadLocalFromIdb::CheckingForFile { token } => {
|
LoadLocalFromIdb::CheckingForFile { token } => {
|
||||||
info!("Checking on file {token}");
|
info!("Checking on file {token}");
|
||||||
match FILE_RESPONSES.remove(token) {
|
match FILE_RESPONSES.remove(token) {
|
||||||
Some((_, value)) => {
|
Some((_, value)) => {
|
||||||
info!("Found {token}");
|
info!("Found {token}");
|
||||||
std::task::Poll::Ready(value)
|
std::task::Poll::Ready(value)
|
||||||
},
|
|
||||||
None => {
|
|
||||||
std::task::Poll::Pending
|
|
||||||
}
|
}
|
||||||
|
None => std::task::Poll::Pending,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -588,7 +588,7 @@ fn serialize_standart_materal(
|
|||||||
|
|
||||||
ev_inspect.clear();
|
ev_inspect.clear();
|
||||||
|
|
||||||
if let Some((target, handle)) = r_selection
|
if let Some((_target, handle)) = r_selection
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|selection| find_handle_in_self_or_children(***selection, &q_children, &q_m))
|
.and_then(|selection| find_handle_in_self_or_children(***selection, &q_children, &q_m))
|
||||||
.and_then(|target| q_m.get(target).ok().map(|h| (target, h.clone())))
|
.and_then(|target| q_m.get(target).ok().map(|h| (target, h.clone())))
|
||||||
|
|||||||
Reference in New Issue
Block a user