Skip to content

Commit

Permalink
wip: fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
TadaTeruki committed Aug 19, 2024
1 parent 4b7468c commit bf32c78
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
6 changes: 5 additions & 1 deletion graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ wgpu = "22.1.0"
web-sys = { version = "0.3", features = [
"Document",
"Window",
"HtmlCanvasElement",
"Element",
"HtmlCanvasElement",
"Request",
"RequestInit",
"RequestMode",
"Response",
"KeyboardEvent",
]}
wasm-logger = "0.2.0"
Expand Down
3 changes: 3 additions & 0 deletions graphics/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
build:
mkdir -p ../view/public/resources
cp -r resources ../view/public
echo "*" > ../view/public/resources/.gitignore
wasm-pack build --target web --out-dir ../view/pkg
35 changes: 35 additions & 0 deletions graphics/src/fetch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::any::Any;

use log::info;
use wasm_bindgen::{convert::IntoWasmAbi, JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use web_sys::{Request, RequestInit, RequestMode, Response};

pub async fn fetch() -> Result<(), JsValue> {

info!("fetching");

let opts = RequestInit::new();
opts.set_method("GET");
opts.set_mode(RequestMode::Cors);

let url = "localhost:5173/test.txt";

let request = Request::new_with_str_and_init(&url, &opts)?;

let window = web_sys::window().unwrap();
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?;



// `resp_value` is a `Response` object.
assert!(resp_value.is_instance_of::<Response>());
let resp: Response = resp_value.dyn_into().unwrap();
info!("conv");
let json = JsFuture::from(resp.array_buffer()?).await?.as_string().unwrap();

info!("{:?}", json);
info!("ok");

Ok(())
}
5 changes: 4 additions & 1 deletion graphics/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use log::error;
use fetch::fetch;
use log::{error, info};
use state::State;
use wasm_bindgen::prelude::*;

Expand All @@ -8,6 +9,7 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
mod camera;
mod key;
mod model;
mod fetch;
mod state;

#[wasm_bindgen(start)]
Expand All @@ -20,6 +22,7 @@ pub async fn create_state(
canvas: web_sys::HtmlCanvasElement,
use_gl_instead: bool,
) -> Option<State> {
info!("{:?}", fetch().await);
match State::new(canvas, use_gl_instead).await {
Ok(state) => Some(state),
Err(e) => {
Expand Down
5 changes: 2 additions & 3 deletions graphics/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ pub struct State {
device: wgpu::Device,
queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration,

key_states: KeyStateMap,

camera: Camera,

render_pipeline: wgpu::RenderPipeline,
models: Vec<Model>,
}
Expand Down Expand Up @@ -129,6 +126,8 @@ impl State {
],
});



let main_model = Model::create(
&device,
&queue,
Expand Down
1 change: 1 addition & 0 deletions view/public/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello!

0 comments on commit bf32c78

Please sign in to comment.