eval.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use dioxus::prelude::*;
  2. use dioxus_desktop::window;
  3. use serde::Deserialize;
  4. #[path = "./utils.rs"]
  5. mod utils;
  6. pub fn main() {
  7. #[cfg(not(windows))]
  8. utils::check_app_exits(app);
  9. }
  10. static EVALS_RECEIVED: GlobalSignal<usize> = Signal::global(|| 0);
  11. static EVALS_RETURNED: GlobalSignal<usize> = Signal::global(|| 0);
  12. fn app() -> Element {
  13. // Double 100 values in the value
  14. use_future(|| async {
  15. let mut eval = eval(
  16. r#"for (let i = 0; i < 100; i++) {
  17. let value = await dioxus.recv();
  18. dioxus.send(value*2);
  19. }"#,
  20. );
  21. for i in 0..100 {
  22. eval.send(serde_json::Value::from(i)).unwrap();
  23. let value = eval.recv().await.unwrap();
  24. assert_eq!(value, serde_json::Value::from(i * 2));
  25. EVALS_RECEIVED.with_mut(|x| *x += 1);
  26. }
  27. });
  28. // Make sure returning no value resolves the future
  29. use_future(|| async {
  30. let eval = eval(r#"return;"#);
  31. eval.await.unwrap();
  32. EVALS_RETURNED.with_mut(|x| *x += 1);
  33. });
  34. // Return a value from the future
  35. use_future(|| async {
  36. let eval = eval(
  37. r#"
  38. return [1, 2, 3];
  39. "#,
  40. );
  41. assert_eq!(
  42. Vec::<i32>::deserialize(&eval.await.unwrap()).unwrap(),
  43. vec![1, 2, 3]
  44. );
  45. EVALS_RETURNED.with_mut(|x| *x += 1);
  46. });
  47. use_memo(|| {
  48. println!("expected 100 evals received found {}", EVALS_RECEIVED());
  49. println!("expected 2 eval returned found {}", EVALS_RETURNED());
  50. if EVALS_RECEIVED() == 100 && EVALS_RETURNED() == 2 {
  51. window().close();
  52. }
  53. });
  54. VNode::empty()
  55. }