Skip to content

Commit 154b1ee

Browse files
committed
wasm: Use wasmtimer for all delaying needs
1 parent 5c0a674 commit 154b1ee

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ tracing = "0.1"
7373
tracing-subscriber = "0.3"
7474
test-log = "0.2"
7575
zxcvbn = "3.1.0"
76+
wasmtimer = "0.4"
7677

7778
[package]
7879
name = "magic-wormhole"
@@ -159,6 +160,7 @@ if-addrs = { version = "0.14", optional = true }
159160
[target.'cfg(target_family = "wasm")'.dependencies]
160161
ws_stream_wasm = "0.7.3"
161162
getrandom = { version = "0.2.5", features = ["js"] }
163+
wasmtimer = { workspace = true }
162164

163165
# for some tests
164166
[dev-dependencies]

src/transfer/v1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ mod tar_helper {
864864
}
865865

866866
#[cfg(any(windows, target_arch = "wasm32"))]
867-
pub(crate) fn path2bytes(p: &str) -> Cow<[u8]> {
867+
pub(crate) fn path2bytes(p: &str) -> Cow<'_, [u8]> {
868868
let bytes = p.as_bytes();
869869
if bytes.contains(&b'\\') {
870870
// Normalize to Unix-style path separators

src/transit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ impl TransitConnector {
13071307
.map(move |(i, u)| (i, u, name.clone()))
13081308
})
13091309
.map(|(index, url, name)| async move {
1310-
async_io::Timer::after(std::time::Duration::from_secs(
1310+
crate::util::sleep(std::time::Duration::from_secs(
13111311
index as u64 * 5,
13121312
))
13131313
.await;

src/util.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,31 @@ pub fn hashcash(resource: String, bits: u32) -> String {
123123
#[error("Timed out")]
124124
pub(crate) struct TimeoutError;
125125

126+
/// Utility function to call async timer implementations for WASM and others
127+
pub(crate) async fn sleep(timeout: std::time::Duration) {
128+
#[cfg(target_family = "wasm")]
129+
wasmtimer::tokio::sleep(timeout).await;
130+
#[cfg(not(target_family = "wasm"))]
131+
async_io::Timer::after(timeout).await;
132+
}
133+
126134
/// Utility function to add a timeout to a future
127135
///
128136
/// This behaves the same as async std timeout, but with async-io
129-
#[cfg(not(target_family = "wasm"))]
130137
pub(crate) fn timeout<'a, R, F: std::future::Future<Output = R> + 'a>(
131138
timeout: std::time::Duration,
132139
future: F,
133140
) -> impl Future<Output = Result<R, TimeoutError>> + 'a {
134141
let timeout_future = async move {
135-
async_io::Timer::after(timeout).await;
142+
sleep(timeout).await;
136143
Err(TimeoutError)
137144
};
138145

139146
futures_lite::future::or(async { Ok(future.await) }, timeout_future)
140147
}
141148

142149
/// Utility function to spawn a future. We don't use crate::util::spawn, because not the entirety of smol compiles on WASM
150+
#[allow(dead_code)]
143151
pub(crate) fn spawn<T: Send + 'static>(
144152
future: impl Future<Output = T> + Send + 'static,
145153
) -> async_task::Task<T> {

0 commit comments

Comments
 (0)