mirror of
https://github.com/maplibre/maplibre-rs.git
synced 2025-12-08 19:05:57 +00:00
Run cache loop in web
This commit is contained in:
parent
ce788619a1
commit
5cde7afe27
@ -19,5 +19,5 @@ fn main() {
|
|||||||
cache_io.run_loop();
|
cache_io.run_loop();
|
||||||
});
|
});
|
||||||
|
|
||||||
pollster::block_on(main_loop::setup(window, event_loop, cache_main));
|
pollster::block_on(main_loop::setup(window, event_loop, Box::new(cache_main)));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ use crate::io::cache::Cache;
|
|||||||
use crate::platform::Instant;
|
use crate::platform::Instant;
|
||||||
use crate::render::state::State;
|
use crate::render::state::State;
|
||||||
|
|
||||||
pub async fn setup(window: winit::window::Window, event_loop: EventLoop<()>, cache: Cache) {
|
pub async fn setup(window: winit::window::Window, event_loop: EventLoop<()>, cache: Box<Cache>) {
|
||||||
info!("== mapr ==");
|
info!("== mapr ==");
|
||||||
|
|
||||||
for x in 0..6 {
|
for x in 0..6 {
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
|
|
||||||
@ -1,22 +1,24 @@
|
|||||||
use console_error_panic_hook;
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::panic;
|
use std::panic;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use js_sys::Array;
|
|
||||||
use log::{info, warn, Level};
|
use log::{info, warn, Level};
|
||||||
use wasm_bindgen::prelude::*;
|
|
||||||
use wasm_bindgen::JsCast;
|
|
||||||
use web_sys::{MessageEvent, Window as WebWindow};
|
|
||||||
use winit::dpi::{LogicalSize, Size};
|
use winit::dpi::{LogicalSize, Size};
|
||||||
use winit::event_loop::EventLoop;
|
use winit::event_loop::EventLoop;
|
||||||
use winit::platform::web::WindowBuilderExtWebSys;
|
use winit::platform::web::WindowBuilderExtWebSys;
|
||||||
use winit::window::{Window, WindowBuilder};
|
use winit::window::{Window, WindowBuilder};
|
||||||
|
|
||||||
mod io;
|
use console_error_panic_hook;
|
||||||
mod wasm_experiment;
|
|
||||||
|
|
||||||
pub use instant::Instant;
|
pub use instant::Instant;
|
||||||
|
use js_sys::Array;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
use wasm_bindgen::JsValue;
|
||||||
|
use web_sys::{MessageEvent, Window as WebWindow};
|
||||||
|
|
||||||
|
use crate::io::cache::Cache;
|
||||||
|
|
||||||
|
mod wasm_experiment;
|
||||||
|
|
||||||
// WebGPU
|
// WebGPU
|
||||||
#[cfg(not(feature = "web-webgl"))]
|
#[cfg(not(feature = "web-webgl"))]
|
||||||
@ -28,14 +30,16 @@ pub const COLOR_TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8
|
|||||||
|
|
||||||
#[wasm_bindgen(start)]
|
#[wasm_bindgen(start)]
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
if let Err(_) = console_log::init_with_level(Level::Trace) {
|
if let Err(_) = console_log::init_with_level(Level::Info) {
|
||||||
// Failed to initialize logging. No need to log a message.
|
// Failed to initialize logging. No need to log a message.
|
||||||
}
|
}
|
||||||
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub async fn run() {
|
pub async fn run(cache_ptr: *mut Cache) {
|
||||||
|
let cache: Box<Cache> = unsafe { Box::from_raw(cache_ptr) };
|
||||||
|
|
||||||
let event_loop = EventLoop::new();
|
let event_loop = EventLoop::new();
|
||||||
let web_window: WebWindow = web_sys::window().unwrap();
|
let web_window: WebWindow = web_sys::window().unwrap();
|
||||||
let document = web_window.document().unwrap();
|
let document = web_window.document().unwrap();
|
||||||
@ -57,5 +61,29 @@ pub async fn run() {
|
|||||||
height: body.client_height(),
|
height: body.client_height(),
|
||||||
});
|
});
|
||||||
|
|
||||||
crate::main_loop::setup(window, event_loop).await;
|
/* for x in 0..6 {
|
||||||
|
for y in 0..6 {
|
||||||
|
cache.fetch((2178 + x, 1421 + y, 12).into())
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// Either call forget or the main loop to keep cache alive
|
||||||
|
//std::mem::forget(cache);
|
||||||
|
crate::main_loop::setup(window, event_loop, cache).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn create_cache() -> *mut Cache {
|
||||||
|
let mut cache = Box::new(Cache::new());
|
||||||
|
let ptr = Box::into_raw(cache);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub async fn run_cache_loop(cache_ptr: *mut Cache) {
|
||||||
|
let mut cache: Box<Cache> = unsafe { Box::from_raw(cache_ptr) };
|
||||||
|
|
||||||
|
// Either call forget or the cache loop to keep cache alive
|
||||||
|
// std::mem::forget(cache);
|
||||||
|
cache.run_loop();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,7 +21,7 @@ pub fn test_fetch(web_window: &Window) {
|
|||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn test_shared_mem(memory: &JsValue) {
|
pub fn test_shared_mem(memory: &JsValue) {
|
||||||
let worker = web_sys::Worker::new_with_options(
|
let worker = web_sys::Worker::new_with_options(
|
||||||
"./fetch-worker.js",
|
"./cache-worker.js",
|
||||||
// Works only on chrome
|
// Works only on chrome
|
||||||
&web_sys::WorkerOptions::new().type_(web_sys::WorkerType::Module),
|
&web_sys::WorkerOptions::new().type_(web_sys::WorkerType::Module),
|
||||||
)
|
)
|
||||||
|
|||||||
18
web/cache-worker.js
Normal file
18
web/cache-worker.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import init from "./dist/libs/mapr";
|
||||||
|
|
||||||
|
let initialized = false;
|
||||||
|
|
||||||
|
onmessage = async m => {
|
||||||
|
let msg = m.data;
|
||||||
|
|
||||||
|
if (msg.type === "init") {
|
||||||
|
if (initialized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
initialized = true;
|
||||||
|
const module = await init(undefined, msg.memory);
|
||||||
|
console.log("Started cache-worker: " + msg.cache_address)
|
||||||
|
|
||||||
|
module.run_cache_loop(msg.cache_address);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -1,13 +0,0 @@
|
|||||||
import init from "./dist/libs/mapr";
|
|
||||||
|
|
||||||
onmessage = async m => {
|
|
||||||
let msg = m.data;
|
|
||||||
|
|
||||||
//await fetch("http://localhost:8080/mapr.html")
|
|
||||||
|
|
||||||
if (msg.type === "init") {
|
|
||||||
const init_output = await init(undefined, msg.memory);
|
|
||||||
console.log(msg.address)
|
|
||||||
postMessage(init_output.get54(msg.address));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
20
web/index.js
20
web/index.js
@ -1,20 +1,24 @@
|
|||||||
import init from "./dist/libs/mapr";
|
import init from "./dist/libs/mapr";
|
||||||
|
|
||||||
const start = async () => {
|
const start = async () => {
|
||||||
const memory = new WebAssembly.Memory({initial: 1024, maximum: 10 * 1024, shared: true});
|
let MEMORY = 16 * 1024;
|
||||||
const init_output = await init(undefined, memory);
|
const memory = new WebAssembly.Memory({initial: 1024, maximum: MEMORY, shared: true});
|
||||||
|
const module = await init(undefined, memory);
|
||||||
|
|
||||||
const fetch_worker = new Worker(new URL('./fetch-worker.js', import.meta.url), {
|
const worker = new Worker(new URL('./cache-worker.js', import.meta.url), {
|
||||||
type: "module",
|
type: "module",
|
||||||
});
|
});
|
||||||
|
|
||||||
fetch_worker.postMessage({type: "init", memory, address: init_output.test_alloc()});
|
let cache_address = module.create_cache();
|
||||||
|
|
||||||
fetch_worker.onmessage = (e) => {
|
console.log("Starting cache-worker")
|
||||||
|
worker.postMessage({type: "init", memory, cache_address});
|
||||||
|
|
||||||
|
/* worker.onmessage = (e) => {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
}
|
}*/
|
||||||
|
|
||||||
await init_output.run();
|
await module.run(cache_address);
|
||||||
}
|
}
|
||||||
|
|
||||||
start();
|
start().then(r => console.log("started via wasm"));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user