Skip to content

Commit

Permalink
update wasm sleep mechanics and remove features
Browse files Browse the repository at this point in the history
  • Loading branch information
piniom committed Mar 11, 2024
1 parent 8938c95 commit 82d90e7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ url = "2"
wasm-bindgen = "0.2"
webauthn-rs-proto = "0.4"
account-sdk = { path = "crates/account_sdk" }
tokio = { version = "1", features = ["macros", "time"] }
11 changes: 6 additions & 5 deletions crates/account_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ sha2.workspace = true
starknet.workspace = true
starknet-crypto.workspace = true
thiserror.workspace = true
tokio = { version = "1", features = ["macros", "time"]}
async-std = { version = "1.12.0"}
tokio.workspace = true
toml.workspace = true
u256-literal.workspace = true
url.workspace = true
wasm-bindgen.workspace = true
webauthn-rs-proto.workspace = true

[features]
tokio-runtime = []
async-std-runtime = []

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.69"
web-sys = "0.3.69"
wasm-bindgen-futures = "0.4.42"
37 changes: 28 additions & 9 deletions crates/account_sdk/src/transaction_waiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,37 @@ pub struct TransactionWaiter<'a, P: Provider> {
enum Sleeper {}

impl Sleeper {
pub async fn sleep(interval: Duration) {
#[cfg(feature = "tokio-runtime")]
pub async fn sleep(delay: Duration) {
#[cfg(not(target_arch = "wasm32"))]
{
tokio::time::sleep(interval).await;
tokio::time::sleep(delay).await;
}
#[cfg(feature = "async-std-runtime")]
#[cfg(target_arch = "wasm32")]
{
async_std::task::sleep(interval).await;
}
#[cfg(not(any(feature = "tokio-runtime", feature = "async-std-runtime")))]
{
compile_error!("At least one of the features 'tokio-runtime' or 'async-std-runtime' must be enabled");
use wasm_bindgen::JsCast;
use web_sys::WorkerGlobalScope;
let mut cb = |resolve: js_sys::Function, _reject: js_sys::Function| {
// if we are in a worker, use the global worker's scope
// otherwise, use the window's scope
if let Ok(worker_scope) = js_sys::global().dyn_into::<WorkerGlobalScope>() {
worker_scope
.set_timeout_with_callback_and_timeout_and_arguments_0(
&resolve,
delay.as_millis() as i32,
)
.expect("should register `setTimeout`");
} else {
web_sys::window()
.unwrap()
.set_timeout_with_callback_and_timeout_and_arguments_0(
&resolve,
delay.as_millis() as i32,
)
.expect("should register `setTimeout`");
}
};
let p = js_sys::Promise::new(&mut cb);
wasm_bindgen_futures::JsFuture::from(p).await.unwrap();
}
}
}
Expand Down

0 comments on commit 82d90e7

Please sign in to comment.