dioxus icon indicating copy to clipboard operation
dioxus copied to clipboard

Using multiple recv in js inside an eval on web ignores all promises but the last

Open elias098 opened this issue 10 months ago • 1 comments

Problem

not completely sure if its intended or not something to care about.

Currently when you do

dioxus.recv()
  .then((a) => {
    console.log("fulfilled");
  })
  .catch((a) => {
    console.error("rejected");
  }); 
dioxus.recv()
  .then((a) => {
    console.log("fulfilled");
  })
  .catch((a) => {
    console.error("rejected");
  }); 

in the js inside an eval in web while theres no messages in the queue it ignores all promises except for the last one

Steps To Reproduce

Here is the code i used to test it

fn main() {
    // Init debug
    dioxus_logger::init(LevelFilter::Info).expect("failed to init logger");
    console_error_panic_hook::set_once();

    launch(App);
}

fn App() -> Element {
    let mut signal = use_signal(|| 0);
    let mut eval2 = use_signal(|| None);
    use_effect(move || {
        if signal() == 1 {
            *eval2.write() = Some(eval(
                r#"
                    dioxus.recv()
                        .then((a) => {
                            console.log("fulfilled");
                        })
                        .catch((a) => {
                            console.error("rejected");
                        }); 
                    dioxus.recv()
                        .then((a) => {
                            console.log("fulfilled");
                        })
                        .catch((a) => {
                            console.error("rejected");
                        }); 
                "#,
            ));
        } else if signal() > 1 {
            let eval2 = eval2().unwrap();
            eval2.send("data".into()).unwrap();
        }
    });

    let onclick = move |_| signal.with_mut(|v| *v += 1);

    rsx! {
        button {
            onclick,
            "locks js"
        }
        {format!("{}", signal())}
    }
}

Expected behavior

i would expect the promises to be fulfilled in the order they were created in

Environment:

  • Dioxus version: 0.5.1
  • Rust version: 1.76.0
  • OS info: MacOS
  • App platform: web

Questionnaire

  • [ ] I'm interested in fixing this myself but don't know where to start
  • [ ] I would like to fix and I have a solution
  • [ ] I don't have time to fix this right now, but maybe later

elias098 avatar Apr 11 '24 19:04 elias098

the code causing this should be that this.promiseResolve = resolve; overwrites the promise in eval.js

  recv() {
    return new Promise((resolve, _reject) => {
      // If data already exists, resolve immediately
      let data = this.received.shift();
      if (data) {
        resolve(data);
        return;
      }

      // Otherwise set a resolve callback
      this.promiseResolve = resolve;
    });
  }

elias098 avatar Apr 11 '24 19:04 elias098