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

Commit 1f60f70

Browse files
futurepaulbenthecarman
authored andcommitted
windowless sleep
1 parent 077c0a8 commit 1f60f70

File tree

4 files changed

+11
-18
lines changed

4 files changed

+11
-18
lines changed

mutiny-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ js-sys = { version = "0.3.60" }
7474
gloo-net = { version = "0.2.4" }
7575
instant = { version = "0.1", features = ["wasm-bindgen"] }
7676
getrandom = { version = "0.2", features = ["js"] }
77+
windowless_sleep = { git = "https://github.com/futurepaul/windowless_sleep", commit = "e924649" }
7778

7879
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
7980
tokio = { version = "1", features = ["rt"] }

mutiny-core/src/utils.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,13 @@ pub(crate) fn min_lightning_amount(network: Network) -> u64 {
2424
pub async fn sleep(millis: i32) {
2525
#[cfg(target_arch = "wasm32")]
2626
{
27-
let mut cb = |resolve: js_sys::Function, _reject: js_sys::Function| {
28-
web_sys::window()
29-
.unwrap()
30-
.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, millis)
31-
.unwrap();
32-
};
33-
let p = js_sys::Promise::new(&mut cb);
34-
wasm_bindgen_futures::JsFuture::from(p).await.unwrap();
27+
windowless_sleep::sleep(millis).await;
3528
}
3629
#[cfg(not(target_arch = "wasm32"))]
3730
{
3831
tokio::time::sleep(Duration::from_millis(millis.try_into().unwrap())).await;
3932
}
4033
}
41-
4234
pub fn now() -> Duration {
4335
#[cfg(target_arch = "wasm32")]
4436
return instant::SystemTime::now()

mutiny-wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ bip39 = { version = "2.0.0" }
4141
getrandom = { version = "0.2", features = ["js"] }
4242
futures = "0.3.25"
4343
urlencoding = "2.1.2"
44+
windowless_sleep = { git = "https://github.com/futurepaul/windowless_sleep", commit = "e924649" }
4445

4546
# The `console_error_panic_hook` crate provides better debugging of panics by
4647
# logging them with `console.error`. This is great for development, but requires

mutiny-wasm/src/utils.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@ pub fn now() -> Duration {
2727
.unwrap()
2828
}
2929

30-
#[allow(dead_code)]
3130
pub async fn sleep(millis: i32) {
32-
let mut cb = |resolve: js_sys::Function, _reject: js_sys::Function| {
33-
web_sys::window()
34-
.unwrap()
35-
.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, millis)
36-
.unwrap();
37-
};
38-
let p = js_sys::Promise::new(&mut cb);
39-
wasm_bindgen_futures::JsFuture::from(p).await.unwrap();
31+
#[cfg(target_arch = "wasm32")]
32+
{
33+
windowless_sleep::sleep(millis).await;
34+
}
35+
#[cfg(not(target_arch = "wasm32"))]
36+
{
37+
tokio::time::sleep(Duration::from_millis(millis.try_into().unwrap())).await;
38+
}
4039
}
4140

4241
#[cfg(test)]

0 commit comments

Comments
 (0)