Skip to content

typespec_client_core: New web_runtime to support wasm32-unknown-unknown #2770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
15 changes: 14 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
hmac = { version = "0.12" }
litemap = "0.7.4"
log = "0.4"
Expand Down
1 change: 1 addition & 0 deletions sdk/typespec/typespec_client_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ 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"] }

[dev-dependencies]
tokio.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub fn get_retry_after(headers: &Headers, now: DateTimeFn) -> Option<Duration> {
///
/// `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.
///
Expand Down
16 changes: 15 additions & 1 deletion sdk/typespec/typespec_client_core/src/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

//! 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.
///
/// # Arguments
Expand All @@ -27,3 +31,13 @@ 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) {
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;
}
}
Loading