Hi Everyone, relatively new to Rust and I'm using it to build web assembly apps. Some of the functions of my app are quite computationally intensive and the user might assume that the page has crashed, when in reality it is just crunching through the data and the user just needs to wait a bit. So I would like to make some sort of progress bar or notification that the user can see.
Here's the problem: if I log information to the console, this appears in real time, as the function is running. However, if I call a JavaScript function that can update the user interface it doesn't update until the WASM function has completed (often after several minutes), so it is not useful for reassuring the user. I also tried overriding the console.log function in javascript to write to the page itself, and unfortunately this doesn't fix the problem.
I don't think this is really a rust issue per se, so I'm not sure if this is the right forum to post in, but if anybody has any ideas I would be really appreciative.
Here is some pseudo-code to show what I'm talking about.
in my lib.rs file:
// macro for writing to the console
#[macro_export]
macro_rules! log {
( $( $t:tt )* ) => {
web_sys::console::log_1(&format!( $( $t )* ).into());
}
}
// function for updating messages to the user
#[wasm_bindgen]
pub fn mod_element(message : String){
let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window");
let body = document.body().expect("document should have a body");
if let Some(val) = document.get_element_by_id("Status"){
val.set_inner_html(message);
}
}
// here is the function that is called by the user click
#[wasm_bindgen]
impl MyApp {
pub fn do_computationally_intensive_thing()->{
self.big_piece_of_data.do_thing()
}
}
In a separate file
impl MyData{
pub fn do_thing(){
for each in self.big_array(){
each.do_slow_thing();
log!("I did a slow thing!"); // this writes to the console in real time
mod_element("I did a slow thing!"); // this only updates the page when
// do_computationally_intensive_thing() has returned
}
}
}