From a71f75dae864a13031d27a8a59f2d3678fffc0fb Mon Sep 17 00:00:00 2001 From: benthecarman Date: Sun, 24 Mar 2024 12:38:54 -0500 Subject: [PATCH] Use gloo-timers for windowless sleep --- Cargo.lock | 19 ++++++++++++++++--- mutiny-core/Cargo.toml | 5 +++-- mutiny-core/src/utils.rs | 12 +++--------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9dcc91113..13d66155b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -209,7 +209,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a349201d80b4aa18d17a34a182bdd7f8ddf845e9e57d2ea130a12e10ef1e3a47" dependencies = [ "futures-util", - "gloo-timers", + "gloo-timers 0.2.6", "tokio", "wasm-bindgen-futures", ] @@ -1094,7 +1094,7 @@ dependencies = [ "fedimint-threshold-crypto", "futures", "getrandom", - "gloo-timers", + "gloo-timers 0.2.6", "hex", "itertools 0.10.5", "js-sys", @@ -1517,7 +1517,7 @@ version = "3.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" dependencies = [ - "gloo-timers", + "gloo-timers 0.2.6", "send_wrapper 0.4.0", ] @@ -1611,6 +1611,18 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "gloo-timers" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbb143cf96099802033e0d4f4963b19fd2e0b728bcf076cd9cf7f6634f092994" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "gloo-utils" version = "0.2.0" @@ -2513,6 +2525,7 @@ dependencies = [ "futures-util", "getrandom", "gloo-net", + "gloo-timers 0.3.0", "hex-conservative", "instant", "itertools 0.11.0", diff --git a/mutiny-core/Cargo.toml b/mutiny-core/Cargo.toml index ec80d6628..8da102880 100644 --- a/mutiny-core/Cargo.toml +++ b/mutiny-core/Cargo.toml @@ -70,6 +70,8 @@ anyhow = "1.0" [dev-dependencies] wasm-bindgen-test = "0.3.33" mockall = "0.11.2" +web-sys = { version = "0.3.65", features = ["console"] } +js-sys = "0.3.65" [features] default = [] @@ -77,9 +79,8 @@ ignored_tests = [] [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen-futures = { version = "0.4.38" } -web-sys = { version = "0.3.65", features = ["console"] } -js-sys = "0.3.65" gloo-net = { version = "0.4.0" } +gloo-timers = { version = "0.3.0", features = ["futures"] } instant = { version = "0.1", features = ["wasm-bindgen"] } getrandom = { version = "0.2", features = ["js"] } # add nip07 feature for wasm32 diff --git a/mutiny-core/src/utils.rs b/mutiny-core/src/utils.rs index 1b8cf4f9d..4cbedb5f2 100644 --- a/mutiny-core/src/utils.rs +++ b/mutiny-core/src/utils.rs @@ -31,20 +31,14 @@ pub(crate) fn min_lightning_amount(network: Network) -> u64 { } pub async fn sleep(millis: i32) { + let duration = Duration::from_millis(millis as u64); #[cfg(target_arch = "wasm32")] { - let mut cb = |resolve: js_sys::Function, _reject: js_sys::Function| { - web_sys::window() - .unwrap() - .set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, millis) - .unwrap(); - }; - let p = js_sys::Promise::new(&mut cb); - wasm_bindgen_futures::JsFuture::from(p).await.unwrap(); + gloo_timers::future::sleep(duration).await } #[cfg(not(target_arch = "wasm32"))] { - tokio::time::sleep(Duration::from_millis(millis.try_into().unwrap())).await; + tokio::time::sleep(duration).await; } } pub fn now() -> Duration {