Skip to content

Commit

Permalink
Use gloo-timers for windowless sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Mar 24, 2024
1 parent d2781a8 commit a71f75d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
19 changes: 16 additions & 3 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 @@ -70,16 +70,17 @@ 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" }
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
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 @@ -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 {
Expand Down

0 comments on commit a71f75d

Please sign in to comment.