Experiment with wasm and web workers

This commit is contained in:
Maximilian Ammann 2021-12-08 21:17:13 +01:00
parent f7cc115e1f
commit f4bfff51d3
6 changed files with 77 additions and 34 deletions

0
src/io/mod.rs Normal file
View File

View File

@ -1,4 +1,4 @@
use log::{info, trace};
use log::{info, trace, warn};
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Window, WindowBuilder};
@ -12,8 +12,11 @@ mod platform;
mod render;
#[cfg(target_arch = "wasm32")]
mod web;
mod io;
async fn setup(window: Window, event_loop: EventLoop<()>) {
use js_sys;
async fn setup(window: Window, event_loop: EventLoop<()>, u8sab: Option<js_sys::Uint8Array>) {
info!("== mapr ==");
info!("Controls:");
info!(" Arrow keys: scrolling");
@ -62,6 +65,8 @@ async fn setup(window: Window, event_loop: EventLoop<()>) {
}
}
Event::RedrawRequested(_) => {
warn!("{}", u8sab.as_ref().unwrap().get_index(1));
let now = Instant::now();
let dt = now - last_render_time;
last_render_time = now;
@ -95,5 +100,5 @@ fn main() {
.build(&event_loop)
.unwrap();
pollster::block_on(setup(window, event_loop));
pollster::block_on(setup(window, event_loop, None));
}

13
src/web/io.rs Normal file
View File

@ -0,0 +1,13 @@
use log::info;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsValue;
use web_sys::Window;
pub fn test_fetch(web_window: &Window) {
let cb: Closure<dyn FnMut(JsValue) + 'static> = Closure::wrap(Box::new(|value: JsValue| {
info!("interval elapsed!");
}) as Box<dyn FnMut(JsValue)>);
web_window.fetch_with_str("http://localhost:5555/web/mapr.html").then(&cb);
cb.forget();
}

View File

@ -1,43 +1,66 @@
extern crate console_error_panic_hook;
use std::cell::RefCell;
use std::panic;
use std::rc::Rc;
use log::{warn, Level};
use wasm_bindgen::prelude::wasm_bindgen;
use js_sys::Array;
use log::{info, warn, Level};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::Window as WebWindow;
use web_sys::{MessageEvent, Window as WebWindow};
use winit::dpi::{LogicalSize, Size};
use winit::event_loop::EventLoop;
use winit::platform::web::WindowBuilderExtWebSys;
use winit::window::{Window, WindowBuilder};
mod io;
#[wasm_bindgen(start)]
pub fn run() {
pub fn start() {
console_log::init_with_level(Level::Info).expect("error initializing log");
panic::set_hook(Box::new(console_error_panic_hook::hook));
wasm_bindgen_futures::spawn_local(async {
let event_loop = EventLoop::new();
let web_window: WebWindow = web_sys::window().unwrap();
let document = web_window.document().unwrap();
let body = document.body().unwrap();
let builder = WindowBuilder::new();
let canvas: web_sys::HtmlCanvasElement = document
.get_element_by_id("mapr")
.unwrap()
.dyn_into::<web_sys::HtmlCanvasElement>()
.unwrap();
let window: Window = builder
.with_canvas(Some(canvas))
.build(&event_loop)
.unwrap();
window.set_inner_size(LogicalSize {
width: body.client_width(),
height: body.client_height(),
});
super::setup(window, event_loop).await;
});
//wasm_bindgen_futures::spawn_local(run());
}
#[wasm_bindgen]
pub async fn run() {
let worker = web_sys::Worker::new("./fetch-worker.js").unwrap();
let callback = Closure::wrap(Box::new(move |event: MessageEvent| {
info!("{}{:?}", "Received response: ", &event.data());
}) as Box<dyn FnMut(_)>);
let sab = js_sys::SharedArrayBuffer::new(10);
let u8sab = js_sys::Uint8Array::new(sab.as_ref());
u8sab.set_index(0, 13);
//worker_handle.set_onmessage(Some(callback.as_ref().unchecked_ref()));
//worker_handle.post_message(&JsValue::from("hello"));
worker.post_message(&u8sab.as_ref());
//callback.forget();
let event_loop = EventLoop::new();
let web_window: WebWindow = web_sys::window().unwrap();
let document = web_window.document().unwrap();
let body = document.body().unwrap();
let builder = WindowBuilder::new();
let canvas: web_sys::HtmlCanvasElement = document
.get_element_by_id("mapr")
.unwrap()
.dyn_into::<web_sys::HtmlCanvasElement>()
.unwrap();
let window: Window = builder
.with_canvas(Some(canvas))
.build(&event_loop)
.unwrap();
window.set_inner_size(LogicalSize {
width: body.client_width(),
height: body.client_height(),
});
super::setup(window, event_loop, Some(u8sab)).await;
}

2
web/.gitignore vendored
View File

@ -1,3 +1,3 @@
*.ts
*.js
mapr*.js
*.wasm

View File

@ -6,7 +6,9 @@
<script type="module">
import init from "./mapr.js";
init();
const init_output = await init();
await init_output.run();
</script>
<canvas id="mapr"></canvas>
</body>