Skip to content

Commit

Permalink
Merge pull request #1078 from MutinyWallet/windowless-sleep
Browse files Browse the repository at this point in the history
Use gloo-timers for windowless sleep
  • Loading branch information
benthecarman authored Apr 11, 2024
2 parents 6d899cc + 40dda80 commit 1beaa65
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,18 @@ 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 = []
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" }
web-time = "1.1"
gloo-timers = { version = "0.3.0", features = ["futures"] }
getrandom = { version = "0.2", features = ["js"] }
# add nip07 feature for wasm32
nostr = { version = "0.29.0", default-features = false, features = ["nip04", "nip05", "nip07", "nip47", "nip57"] }
Expand Down
12 changes: 3 additions & 9 deletions mutiny-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,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 {
Expand Down

0 comments on commit 1beaa65

Please sign in to comment.