Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 1beaa65

Browse files
authored
Merge pull request #1078 from MutinyWallet/windowless-sleep
Use gloo-timers for windowless sleep
2 parents 6d899cc + 40dda80 commit 1beaa65

3 files changed

Lines changed: 7 additions & 11 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mutiny-core/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,18 @@ anyhow = "1.0"
7171
[dev-dependencies]
7272
wasm-bindgen-test = "0.3.33"
7373
mockall = "0.11.2"
74+
web-sys = { version = "0.3.65", features = ["console"] }
75+
js-sys = "0.3.65"
7476

7577
[features]
7678
default = []
7779
ignored_tests = []
7880

7981
[target.'cfg(target_arch = "wasm32")'.dependencies]
8082
wasm-bindgen-futures = { version = "0.4.38" }
81-
web-sys = { version = "0.3.65", features = ["console"] }
82-
js-sys = "0.3.65"
8383
gloo-net = { version = "0.4.0" }
8484
web-time = "1.1"
85+
gloo-timers = { version = "0.3.0", features = ["futures"] }
8586
getrandom = { version = "0.2", features = ["js"] }
8687
# add nip07 feature for wasm32
8788
nostr = { version = "0.29.0", default-features = false, features = ["nip04", "nip05", "nip07", "nip47", "nip57"] }

mutiny-core/src/utils.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,14 @@ pub(crate) fn min_lightning_amount(network: Network) -> u64 {
3232
}
3333

3434
pub async fn sleep(millis: i32) {
35+
let duration = Duration::from_millis(millis as u64);
3536
#[cfg(target_arch = "wasm32")]
3637
{
37-
let mut cb = |resolve: js_sys::Function, _reject: js_sys::Function| {
38-
web_sys::window()
39-
.unwrap()
40-
.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, millis)
41-
.unwrap();
42-
};
43-
let p = js_sys::Promise::new(&mut cb);
44-
wasm_bindgen_futures::JsFuture::from(p).await.unwrap();
38+
gloo_timers::future::sleep(duration).await
4539
}
4640
#[cfg(not(target_arch = "wasm32"))]
4741
{
48-
tokio::time::sleep(Duration::from_millis(millis.try_into().unwrap())).await;
42+
tokio::time::sleep(duration).await;
4943
}
5044
}
5145
pub fn now() -> Duration {

0 commit comments

Comments
 (0)