1
0

utils.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. dioxus::LaunchBuilder::desktop()
  18. .with_cfg(Config::new().with_window(WindowBuilder::new().with_visible(false)))
  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. // We need to wait for edits to be applied before we can send the event
  32. // Sometimes (windows...) this takes a while
  33. // we should really be running this check when mounted
  34. tokio::time::sleep(std::time::Duration::from_millis(10000)).await;
  35. let js = format!(
  36. r#"
  37. let event = {value};
  38. let element = document.getElementById('{id}');
  39. {extra}
  40. element.dispatchEvent(event);
  41. "#
  42. );
  43. document::eval(&js).await.unwrap();
  44. });
  45. })
  46. }