utils.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #![allow(unused)] // for whatever reason, the compiler is not recognizing the use of these functions
  2. use dioxus::prelude::*;
  3. use dioxus_core::Element;
  4. pub fn check_app_exits(app: fn() -> Element) {
  5. use dioxus_desktop::tao::window::WindowBuilder;
  6. use dioxus_desktop::Config;
  7. // This is a deadman's switch to ensure that the app exits
  8. let should_panic = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
  9. let should_panic_clone = should_panic.clone();
  10. std::thread::spawn(move || {
  11. std::thread::sleep(std::time::Duration::from_secs(60));
  12. if should_panic_clone.load(std::sync::atomic::Ordering::SeqCst) {
  13. eprintln!("App did not exit in time");
  14. std::process::exit(exitcode::SOFTWARE);
  15. }
  16. });
  17. LaunchBuilder::desktop()
  18. .with_cfg(Config::new().with_window(WindowBuilder::new().with_visible(true)))
  19. .launch(app);
  20. // Stop deadman's switch
  21. should_panic.store(false, std::sync::atomic::Ordering::SeqCst);
  22. }
  23. pub static EXPECTED_EVENTS: GlobalSignal<usize> = Signal::global(|| 0);
  24. pub fn mock_event(id: &'static str, value: &'static str) {
  25. mock_event_with_extra(id, value, "");
  26. }
  27. pub fn mock_event_with_extra(id: &'static str, value: &'static str, extra: &'static str) {
  28. use_hook(move || {
  29. EXPECTED_EVENTS.with_mut(|x| *x += 1);
  30. spawn(async move {
  31. tokio::time::sleep(std::time::Duration::from_millis(5000)).await;
  32. let js = format!(
  33. r#"
  34. let event = {value};
  35. let element = document.getElementById('{id}');
  36. {extra}
  37. element.dispatchEvent(event);
  38. "#
  39. );
  40. eval(&js).await.unwrap();
  41. });
  42. })
  43. }