Skip to content

Commit 00af4fb

Browse files
committed
fix: remove dependency on broken wasm_timer crate
`wasm_timer` doesn't work outside the browser, which prevents it being used in tests. We can replace it with `gloo-timers` wich uses the standard Javascript `setTimeout`. Fixes: #896, #1443.
1 parent 441626b commit 00af4fb

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

Cargo.lock

Lines changed: 13 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/matrix-sdk-common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ serde_json = { workspace = true }
2929
async-lock = "2.5.0"
3030
futures-util = { version = "0.3.21", default-features = false, features = ["channel"] }
3131
wasm-bindgen-futures = { version = "0.4.33", optional = true }
32-
wasm-timer = "0.2.5"
32+
gloo-timers = { version = "0.2.6", features = ["futures"] }
3333

3434
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
3535
tokio = { version = "1.24.2", default-features = false, features = ["rt", "sync", "time"] }

crates/matrix-sdk-common/src/timeout.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
use std::{error::Error, fmt, time::Duration};
22

33
use futures_core::Future;
4+
45
#[cfg(not(target_arch = "wasm32"))]
56
use tokio::time::timeout as tokio_timeout;
7+
8+
#[cfg(target_arch = "wasm32")]
9+
use gloo_timers::future::TimeoutFuture;
10+
611
#[cfg(target_arch = "wasm32")]
7-
use wasm_timer::ext::TryFutureExt;
12+
use futures_util::future::{select, Either};
13+
814

915
/// Error type notifying that a timeout has elapsed.
1016
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -25,21 +31,23 @@ impl Error for ElapsedError {}
2531
/// an error.
2632
pub async fn timeout<F, T>(future: F, duration: Duration) -> Result<T, ElapsedError>
2733
where
28-
F: Future<Output = T>,
34+
F: Future<Output = T> + Unpin,
2935
{
3036
#[cfg(not(target_arch = "wasm32"))]
3137
return tokio_timeout(duration, future).await.map_err(|_| ElapsedError(()));
3238

3339
#[cfg(target_arch = "wasm32")]
3440
{
35-
let try_future = async { Ok::<T, std::io::Error>(future.await) };
36-
try_future.timeout(duration).await.map_err(|_| ElapsedError(()))
41+
let timeout_future = TimeoutFuture::new(u32::try_from(duration.as_millis()).expect("Overlong duration"));
42+
43+
match select(future, timeout_future).await {
44+
Either::Left((res, _)) => Ok(res),
45+
Either::Right((_, _)) => Err(ElapsedError(())),
46+
}
3747
}
3848
}
3949

40-
// TODO: Enable tests for wasm32 and debug why
41-
// `with_timeout` test fails https://github.com/matrix-org/matrix-rust-sdk/issues/896
42-
#[cfg(all(test, not(target_arch = "wasm32")))]
50+
#[cfg(all(test))]
4351
pub(crate) mod tests {
4452
use std::{future, time::Duration};
4553

crates/matrix-sdk/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ optional = true
118118

119119
[target.'cfg(target_arch = "wasm32")'.dependencies]
120120
async-once-cell = "0.4.2"
121-
wasm-timer = "0.2.5"
122121

123122
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
124123
backoff = { version = "0.4.0", features = ["tokio"] }

crates/matrix-sdk/src/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl Client {
184184

185185
async fn sleep() {
186186
#[cfg(target_arch = "wasm32")]
187-
let _ = wasm_timer::Delay::new(Duration::from_secs(1)).await;
187+
let _ = gloo_timers::future::TimeoutFuture::new(1_000).await;
188188

189189
#[cfg(not(target_arch = "wasm32"))]
190190
tokio::time::sleep(Duration::from_secs(1)).await;

0 commit comments

Comments
 (0)