diff --git a/Cargo.toml b/Cargo.toml index aa6516f6..90df2a5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/crates/account_sdk/Cargo.toml b/crates/account_sdk/Cargo.toml index 01753ed1..bcc6d415 100644 --- a/crates/account_sdk/Cargo.toml +++ b/crates/account_sdk/Cargo.toml @@ -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" diff --git a/crates/account_sdk/src/transaction_waiter.rs b/crates/account_sdk/src/transaction_waiter.rs index 9eeca5d0..dce9cdcd 100644 --- a/crates/account_sdk/src/transaction_waiter.rs +++ b/crates/account_sdk/src/transaction_waiter.rs @@ -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::() { + 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(); } } }