From 2ab872d79a5bce5ded4ebf571865cb1f17b72a94 Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 8 Jul 2025 14:03:03 +1000 Subject: [PATCH 01/11] typespec_client_core: Use `gloo_timers::future::sleep` to impl `sleep` when target wasm The `typespec_client_core::sleep::sleep` is using the async_runtime's sleep implementation, which is complicated to support `wasm32`, and will panic now when targeting to `wasm32`. Instead of make the `standard_runtime` to support wasm32 sleep (I wonder if it's feasible), we can instead just introduce another `sleep` implementation to simply use the `gloo_timers::future::sleep`. Also, this PR adds a `cfg_attr` for conditionally remove the `Send` trait from the `async_trait` for the `RetryPolicy`, as the other parts of the code do. Additionally, the `getrandom` crate will enable `wasm_js` features when targetting to `wasm`, which is necessary. --- Cargo.lock | 15 ++++++++++++++- Cargo.toml | 1 + sdk/typespec/typespec_client_core/Cargo.toml | 3 ++- .../src/http/policies/retry/mod.rs | 3 ++- sdk/typespec/typespec_client_core/src/sleep.rs | 6 ++++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 685cb51fa2..0e385c2352 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -1149,6 +1149,18 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "gloo-timers" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbb143cf96099802033e0d4f4963b19fd2e0b728bcf076cd9cf7f6634f092994" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "half" version = "2.6.0" @@ -2833,6 +2845,7 @@ dependencies = [ "dyn-clone", "futures", "getrandom 0.3.3", + "gloo-timers", "pin-project", "quick-xml", "rand 0.9.1", diff --git a/Cargo.toml b/Cargo.toml index f7f7f592e8..0eb5b3d092 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,6 +83,7 @@ fe2o3-amqp-types = { version = "0.14" } flate2 = "1.1.0" futures = "0.3" getrandom = { version = "0.3" } +gloo-timers = { version = "0.3", features = ["futures"] } hmac = { version = "0.12" } litemap = "0.7.4" log = "0.4" diff --git a/sdk/typespec/typespec_client_core/Cargo.toml b/sdk/typespec/typespec_client_core/Cargo.toml index a9396c1403..c3e902d910 100644 --- a/sdk/typespec/typespec_client_core/Cargo.toml +++ b/sdk/typespec/typespec_client_core/Cargo.toml @@ -34,8 +34,9 @@ uuid.workspace = true tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } [target.'cfg(target_family = "wasm")'.dependencies] -getrandom.workspace = true +getrandom = { workspace = true, features = ["wasm_js"] } tokio = { workspace = true, features = ["macros", "rt", "time"] } +gloo-timers.workspace = true [dev-dependencies] tokio.workspace = true diff --git a/sdk/typespec/typespec_client_core/src/http/policies/retry/mod.rs b/sdk/typespec/typespec_client_core/src/http/policies/retry/mod.rs index b4ea0e5098..d7b0e7dec0 100644 --- a/sdk/typespec/typespec_client_core/src/http/policies/retry/mod.rs +++ b/sdk/typespec/typespec_client_core/src/http/policies/retry/mod.rs @@ -76,7 +76,8 @@ pub fn get_retry_after(headers: &Headers, now: DateTimeFn) -> Option { /// /// `wait` can be implemented in more complex cases where a simple test of time /// is not enough. -#[async_trait] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] pub trait RetryPolicy: std::fmt::Debug + Send + Sync { /// Determine if no more retries should be performed. /// diff --git a/sdk/typespec/typespec_client_core/src/sleep.rs b/sdk/typespec/typespec_client_core/src/sleep.rs index 97fccfcd7d..efb7e779cb 100644 --- a/sdk/typespec/typespec_client_core/src/sleep.rs +++ b/sdk/typespec/typespec_client_core/src/sleep.rs @@ -5,6 +5,7 @@ use crate::{async_runtime::get_async_runtime, time::Duration}; +#[cfg(not(target_family = "wasm"))] /// Sleeps for the specified duration using the configured async runtime. /// /// # Arguments @@ -27,3 +28,8 @@ use crate::{async_runtime::get_async_runtime, time::Duration}; pub async fn sleep(duration: Duration) { get_async_runtime().sleep(duration).await } + +#[cfg(target_family = "wasm")] +pub async fn sleep(duration: Duration) { + gloo_timers::future::sleep(duration.try_into().unwrap()).await; +} From c92b92b9f0b0808fddfe634b480e7ccfc3109102 Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 8 Jul 2025 14:10:57 +1000 Subject: [PATCH 02/11] fix warning --- sdk/typespec/typespec_client_core/src/sleep.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/typespec/typespec_client_core/src/sleep.rs b/sdk/typespec/typespec_client_core/src/sleep.rs index efb7e779cb..bbdb000759 100644 --- a/sdk/typespec/typespec_client_core/src/sleep.rs +++ b/sdk/typespec/typespec_client_core/src/sleep.rs @@ -3,7 +3,10 @@ //! Sleep functions. -use crate::{async_runtime::get_async_runtime, time::Duration}; +use crate::time::Duration; + +#[cfg(not(target_family = "wasm"))] +use crate::async_runtime::get_async_runtime; #[cfg(not(target_family = "wasm"))] /// Sleeps for the specified duration using the configured async runtime. From 5b5607ba59ca8ec320b398712ad30d504669d7d7 Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 8 Jul 2025 15:04:55 +1000 Subject: [PATCH 03/11] dont unwrap --- sdk/typespec/typespec_client_core/src/sleep.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sdk/typespec/typespec_client_core/src/sleep.rs b/sdk/typespec/typespec_client_core/src/sleep.rs index bbdb000759..1faec1872e 100644 --- a/sdk/typespec/typespec_client_core/src/sleep.rs +++ b/sdk/typespec/typespec_client_core/src/sleep.rs @@ -34,5 +34,10 @@ pub async fn sleep(duration: Duration) { #[cfg(target_family = "wasm")] pub async fn sleep(duration: Duration) { - gloo_timers::future::sleep(duration.try_into().unwrap()).await; + if let Ok(d) = duration.try_into::() { + gloo_timers::future::sleep(d).await; + } else { + // This means the duration is negative, don't sleep at all. + return; + } } From 6fa709a73b4746f7a8c98a4e09f75a8ca3f71c1e Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 8 Jul 2025 15:07:17 +1000 Subject: [PATCH 04/11] clean up cargo.toml --- Cargo.toml | 2 +- sdk/typespec/typespec_client_core/Cargo.toml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0eb5b3d092..0426f2d876 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,7 +83,7 @@ fe2o3-amqp-types = { version = "0.14" } flate2 = "1.1.0" futures = "0.3" getrandom = { version = "0.3" } -gloo-timers = { version = "0.3", features = ["futures"] } +gloo-timers = { version = "0.3" } hmac = { version = "0.12" } litemap = "0.7.4" log = "0.4" diff --git a/sdk/typespec/typespec_client_core/Cargo.toml b/sdk/typespec/typespec_client_core/Cargo.toml index c3e902d910..3dd47c8772 100644 --- a/sdk/typespec/typespec_client_core/Cargo.toml +++ b/sdk/typespec/typespec_client_core/Cargo.toml @@ -34,9 +34,9 @@ uuid.workspace = true tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } [target.'cfg(target_family = "wasm")'.dependencies] -getrandom = { workspace = true, features = ["wasm_js"] } +getrandom.workspace = true tokio = { workspace = true, features = ["macros", "rt", "time"] } -gloo-timers.workspace = true +gloo-timers = { workspace = true, features = ["futures"] } [dev-dependencies] tokio.workspace = true From 411faafa329fa7442e261a238e3ea78fb3fa5e07 Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 8 Jul 2025 15:09:17 +1000 Subject: [PATCH 05/11] pass build --- sdk/typespec/typespec_client_core/src/sleep.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/typespec/typespec_client_core/src/sleep.rs b/sdk/typespec/typespec_client_core/src/sleep.rs index 1faec1872e..9c822a0398 100644 --- a/sdk/typespec/typespec_client_core/src/sleep.rs +++ b/sdk/typespec/typespec_client_core/src/sleep.rs @@ -34,7 +34,7 @@ pub async fn sleep(duration: Duration) { #[cfg(target_family = "wasm")] pub async fn sleep(duration: Duration) { - if let Ok(d) = duration.try_into::() { + if let Ok(d) = duration.try_into() { gloo_timers::future::sleep(d).await; } else { // This means the duration is negative, don't sleep at all. From 6069a4f7e8bee8e08c92f9fd48aa98c3e1c659fb Mon Sep 17 00:00:00 2001 From: magodo Date: Wed, 9 Jul 2025 15:13:10 +1000 Subject: [PATCH 06/11] Define a web_runtime instead of conditionally implement sleep --- Cargo.lock | 1 + Cargo.toml | 1 + sdk/typespec/typespec_client_core/Cargo.toml | 1 + .../src/async_runtime/mod.rs | 13 +++++++-- .../src/async_runtime/standard_runtime.rs | 2 +- .../src/async_runtime/web_runtime.rs | 29 +++++++++++++++++++ .../typespec_client_core/src/sleep.rs | 15 +--------- 7 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs diff --git a/Cargo.lock b/Cargo.lock index 0e385c2352..97a7253834 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2860,6 +2860,7 @@ dependencies = [ "typespec_macros", "url", "uuid", + "wasm-bindgen-futures", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 0426f2d876..4e3c89a5e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -122,6 +122,7 @@ tracing = "0.1.40" tracing-subscriber = "0.3" url = "2.2" uuid = { version = "1.17", features = ["v4"] } +wasm-bindgen-futures = "0.4" zerofrom = "0.1.5" zip = { version = "4.0.0", default-features = false, features = ["deflate"] } diff --git a/sdk/typespec/typespec_client_core/Cargo.toml b/sdk/typespec/typespec_client_core/Cargo.toml index 3dd47c8772..708e8b1af4 100644 --- a/sdk/typespec/typespec_client_core/Cargo.toml +++ b/sdk/typespec/typespec_client_core/Cargo.toml @@ -37,6 +37,7 @@ tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } getrandom.workspace = true tokio = { workspace = true, features = ["macros", "rt", "time"] } gloo-timers = { workspace = true, features = ["futures"] } +wasm-bindgen-futures.workspace = true [dev-dependencies] tokio.workspace = true diff --git a/sdk/typespec/typespec_client_core/src/async_runtime/mod.rs b/sdk/typespec/typespec_client_core/src/async_runtime/mod.rs index a1e3073bfe..f8e0ef3060 100644 --- a/sdk/typespec/typespec_client_core/src/async_runtime/mod.rs +++ b/sdk/typespec/typespec_client_core/src/async_runtime/mod.rs @@ -41,6 +41,9 @@ mod standard_runtime; #[cfg(feature = "tokio")] mod tokio_runtime; +#[cfg(target_arch = "wasm32")] +mod web_runtime; + #[cfg(test)] mod tests; @@ -107,7 +110,7 @@ pub trait AsyncRuntime: Send + Sync { /// fn spawn(&self, f: TaskFuture) -> SpawnedTask; - fn sleep(&self, duration: Duration) -> Pin + Send + 'static>>; + fn sleep(&self, duration: Duration) -> TaskFuture; } static ASYNC_RUNTIME_IMPLEMENTATION: OnceLock> = OnceLock::new(); @@ -189,12 +192,16 @@ pub fn set_async_runtime(runtime: Arc) -> crate::Result<()> { } fn create_async_runtime() -> Arc { - #[cfg(not(feature = "tokio"))] + #[cfg(target_arch = "wasm32")] { - Arc::new(standard_runtime::StdRuntime) + Arc::new(web_runtime::WebRuntime) as Arc } #[cfg(feature = "tokio")] { Arc::new(tokio_runtime::TokioRuntime) as Arc } + #[cfg(not(any(feature = "tokio", target_arch = "wasm32")))] + { + Arc::new(standard_runtime::StdRuntime) + } } diff --git a/sdk/typespec/typespec_client_core/src/async_runtime/standard_runtime.rs b/sdk/typespec/typespec_client_core/src/async_runtime/standard_runtime.rs index 739d85f1f8..52b0f39900 100644 --- a/sdk/typespec/typespec_client_core/src/async_runtime/standard_runtime.rs +++ b/sdk/typespec/typespec_client_core/src/async_runtime/standard_runtime.rs @@ -150,7 +150,7 @@ impl AsyncRuntime for StdRuntime { /// Uses a simple thread based implementation for sleep. A more efficient /// implementation is available by using the `tokio` crate feature. #[cfg_attr(target_arch = "wasm32", allow(unused_variables))] - fn sleep(&self, duration: Duration) -> Pin + Send + 'static>> { + fn sleep(&self, duration: Duration) -> TaskFuture { #[cfg(target_arch = "wasm32")] { panic!("sleep is not supported on wasm32") diff --git a/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs b/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs new file mode 100644 index 0000000000..80e97ceb9d --- /dev/null +++ b/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +use super::{AsyncRuntime, SpawnedTask, TaskFuture}; +use crate::time::Duration; +use std::pin::Pin; + +/// An [`AsyncRuntime`] using `tokio` based APIs. +pub(crate) struct WebRuntime; + +impl AsyncRuntime for WebRuntime { + fn spawn(&self, f: TaskFuture) -> SpawnedTask { + Box::pin(async { + wasm_bindgen_futures::spawn_local(f); + Ok(()) + }) + } + + fn sleep(&self, duration: Duration) -> TaskFuture { + Box::pin(async move { + if let Ok(d) = duration.try_into() { + gloo_timers::future::sleep(d).await; + } else { + // This means the duration is negative, don't sleep at all. + return; + } + }) + } +} diff --git a/sdk/typespec/typespec_client_core/src/sleep.rs b/sdk/typespec/typespec_client_core/src/sleep.rs index 9c822a0398..302bffc715 100644 --- a/sdk/typespec/typespec_client_core/src/sleep.rs +++ b/sdk/typespec/typespec_client_core/src/sleep.rs @@ -3,12 +3,9 @@ //! Sleep functions. -use crate::time::Duration; - -#[cfg(not(target_family = "wasm"))] use crate::async_runtime::get_async_runtime; +use crate::time::Duration; -#[cfg(not(target_family = "wasm"))] /// Sleeps for the specified duration using the configured async runtime. /// /// # Arguments @@ -31,13 +28,3 @@ use crate::async_runtime::get_async_runtime; pub async fn sleep(duration: Duration) { get_async_runtime().sleep(duration).await } - -#[cfg(target_family = "wasm")] -pub async fn sleep(duration: Duration) { - if let Ok(d) = duration.try_into() { - gloo_timers::future::sleep(d).await; - } else { - // This means the duration is negative, don't sleep at all. - return; - } -} From 55ce372e09398347dadb2c9038553d4692e469e9 Mon Sep 17 00:00:00 2001 From: magodo Date: Wed, 9 Jul 2025 15:14:32 +1000 Subject: [PATCH 07/11] minor --- sdk/typespec/typespec_client_core/src/sleep.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/typespec/typespec_client_core/src/sleep.rs b/sdk/typespec/typespec_client_core/src/sleep.rs index 302bffc715..97fccfcd7d 100644 --- a/sdk/typespec/typespec_client_core/src/sleep.rs +++ b/sdk/typespec/typespec_client_core/src/sleep.rs @@ -3,8 +3,7 @@ //! Sleep functions. -use crate::async_runtime::get_async_runtime; -use crate::time::Duration; +use crate::{async_runtime::get_async_runtime, time::Duration}; /// Sleeps for the specified duration using the configured async runtime. /// From 23692dc490f071f347cc443f5c56ab21ca3ebac8 Mon Sep 17 00:00:00 2001 From: magodo Date: Wed, 9 Jul 2025 15:16:36 +1000 Subject: [PATCH 08/11] clippy --- .../typespec_client_core/src/async_runtime/standard_runtime.rs | 1 + .../typespec_client_core/src/async_runtime/web_runtime.rs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/typespec/typespec_client_core/src/async_runtime/standard_runtime.rs b/sdk/typespec/typespec_client_core/src/async_runtime/standard_runtime.rs index 52b0f39900..2d590b57e5 100644 --- a/sdk/typespec/typespec_client_core/src/async_runtime/standard_runtime.rs +++ b/sdk/typespec/typespec_client_core/src/async_runtime/standard_runtime.rs @@ -14,6 +14,7 @@ use std::{ task::{Context, Poll, Waker}, thread, }; +#[cfg(not(target_arch = "wasm32"))] use std::{future::Future, pin::Pin}; #[cfg(not(target_arch = "wasm32"))] use tracing::debug; diff --git a/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs b/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs index 80e97ceb9d..37b455585f 100644 --- a/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs +++ b/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs @@ -3,7 +3,6 @@ use super::{AsyncRuntime, SpawnedTask, TaskFuture}; use crate::time::Duration; -use std::pin::Pin; /// An [`AsyncRuntime`] using `tokio` based APIs. pub(crate) struct WebRuntime; From 019ef31c5f785f2fbd6efb16099903eb19db15c5 Mon Sep 17 00:00:00 2001 From: magodo Date: Wed, 9 Jul 2025 15:39:35 +1000 Subject: [PATCH 09/11] improve `spawn` --- .../src/async_runtime/web_runtime.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs b/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs index 37b455585f..23106ecafa 100644 --- a/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs +++ b/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs @@ -3,16 +3,21 @@ use super::{AsyncRuntime, SpawnedTask, TaskFuture}; use crate::time::Duration; +use futures::channel::oneshot; /// An [`AsyncRuntime`] using `tokio` based APIs. pub(crate) struct WebRuntime; impl AsyncRuntime for WebRuntime { fn spawn(&self, f: TaskFuture) -> SpawnedTask { - Box::pin(async { - wasm_bindgen_futures::spawn_local(f); - Ok(()) - }) + let (tx, rx) = oneshot::channel(); + + wasm_bindgen_futures::spawn_local(async move { + let result = f.await; + let _ = tx.send(result); + }); + + Box::pin(async { Ok(rx.await?) }) } fn sleep(&self, duration: Duration) -> TaskFuture { From 5ecd47f7bb3c6197a6c8b67993b7356613be112b Mon Sep 17 00:00:00 2001 From: magodo Date: Wed, 9 Jul 2025 15:44:35 +1000 Subject: [PATCH 10/11] correct error handling --- .../typespec_client_core/src/async_runtime/web_runtime.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs b/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs index 23106ecafa..aade1b4775 100644 --- a/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs +++ b/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs @@ -17,7 +17,10 @@ impl AsyncRuntime for WebRuntime { let _ = tx.send(result); }); - Box::pin(async { Ok(rx.await?) }) + Box::pin(async { + rx.await + .map_err(|e| Box::new(e) as Box) + }) } fn sleep(&self, duration: Duration) -> TaskFuture { From 349b3c3e65f0fd5618c0d3b5df2050ce77f4038c Mon Sep 17 00:00:00 2001 From: magodo Date: Thu, 10 Jul 2025 11:46:45 +1000 Subject: [PATCH 11/11] introduce feature wasm-bindgen --- sdk/typespec/typespec_client_core/Cargo.toml | 5 +++-- sdk/typespec/typespec_client_core/src/async_runtime/mod.rs | 6 +++--- .../typespec_client_core/src/async_runtime/web_runtime.rs | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/sdk/typespec/typespec_client_core/Cargo.toml b/sdk/typespec/typespec_client_core/Cargo.toml index 708e8b1af4..37878e6048 100644 --- a/sdk/typespec/typespec_client_core/Cargo.toml +++ b/sdk/typespec/typespec_client_core/Cargo.toml @@ -16,6 +16,7 @@ base64.workspace = true bytes.workspace = true dyn-clone.workspace = true futures.workspace = true +gloo-timers = { workspace = true, optional = true } pin-project.workspace = true quick-xml = { workspace = true, optional = true } rand.workspace = true @@ -29,6 +30,7 @@ typespec = { workspace = true, default-features = false } typespec_macros = { workspace = true, optional = true } url.workspace = true uuid.workspace = true +wasm-bindgen-futures = { workspace = true, optional = true } [target.'cfg(not(target_family = "wasm"))'.dependencies] tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } @@ -36,8 +38,6 @@ tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } [target.'cfg(target_family = "wasm")'.dependencies] getrandom.workspace = true tokio = { workspace = true, features = ["macros", "rt", "time"] } -gloo-timers = { workspace = true, features = ["futures"] } -wasm-bindgen-futures.workspace = true [dev-dependencies] tokio.workspace = true @@ -59,6 +59,7 @@ reqwest_rustls = [ ] # Remove dependency on banned `ring` crate; requires manually configuring crypto provider. test = [] # Enables extra tracing including error bodies that may contain PII. tokio = ["tokio/fs", "tokio/sync", "tokio/time", "tokio/io-util"] +wasm-bindgen = ["dep:wasm-bindgen-futures", "gloo-timers/futures"] xml = ["dep:quick-xml"] [[example]] diff --git a/sdk/typespec/typespec_client_core/src/async_runtime/mod.rs b/sdk/typespec/typespec_client_core/src/async_runtime/mod.rs index f8e0ef3060..97ca216515 100644 --- a/sdk/typespec/typespec_client_core/src/async_runtime/mod.rs +++ b/sdk/typespec/typespec_client_core/src/async_runtime/mod.rs @@ -192,15 +192,15 @@ pub fn set_async_runtime(runtime: Arc) -> crate::Result<()> { } fn create_async_runtime() -> Arc { - #[cfg(target_arch = "wasm32")] + #[cfg(feature = "wasm-bindgen")] { - Arc::new(web_runtime::WebRuntime) as Arc + Arc::new(web_runtime::WasmBindgenRuntime) as Arc } #[cfg(feature = "tokio")] { Arc::new(tokio_runtime::TokioRuntime) as Arc } - #[cfg(not(any(feature = "tokio", target_arch = "wasm32")))] + #[cfg(not(any(feature = "tokio", feature = "wasm-bindgen")))] { Arc::new(standard_runtime::StdRuntime) } diff --git a/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs b/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs index aade1b4775..ca971b3ac0 100644 --- a/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs +++ b/sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs @@ -6,9 +6,9 @@ use crate::time::Duration; use futures::channel::oneshot; /// An [`AsyncRuntime`] using `tokio` based APIs. -pub(crate) struct WebRuntime; +pub(crate) struct WasmBindgenRuntime; -impl AsyncRuntime for WebRuntime { +impl AsyncRuntime for WasmBindgenRuntime { fn spawn(&self, f: TaskFuture) -> SpawnedTask { let (tx, rx) = oneshot::channel();