Skip to content

Commit

Permalink
Fix wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
zakstucke committed Jun 19, 2024
1 parent 06f2d23 commit d4eddc9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .zetch.lock

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

2 changes: 2 additions & 0 deletions rust/bitbazaar/misc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ mod in_ci;
mod is_tcp_port_listening;
mod periodic_updater;
mod retry_backoff;
mod sleep_compat;

pub use binary_search::*;
pub use flexi_logger::*;
pub use in_ci::in_ci;
pub use is_tcp_port_listening::is_tcp_port_listening;
pub use periodic_updater::*;
pub use retry_backoff::*;
pub use sleep_compat::*;
4 changes: 3 additions & 1 deletion rust/bitbazaar/misc/retry_backoff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::time::Duration;
use futures::Future;
use itertools::Either;

use crate::misc::sleep_compat;

/// Will attempt execute the fn's returned future according to the entered spec.
///
/// # Arguments
Expand Down Expand Up @@ -31,7 +33,7 @@ pub async fn retry_backoff<R, E, Fut: Future<Output = Result<R, E>>>(
.enumerate()
{
if delay.as_nanos() > 0 {
tokio::time::sleep(*delay).await;
sleep_compat(*delay).await;
}
match fallible().await {
Ok(r) => return Ok(r),
Expand Down
10 changes: 10 additions & 0 deletions rust/bitbazaar/misc/sleep_compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// Sleep for a duration, compatible with both WASM and native targets.
///
/// Wasm: uses gloo_timers::future::TimeoutFuture
/// Native: uses tokio::time::sleep
pub async fn sleep_compat(duration: std::time::Duration) {
#[cfg(target_arch = "wasm32")]
gloo_timers::future::TimeoutFuture::new(duration.as_millis()).await;
#[cfg(not(target_arch = "wasm32"))]
tokio::time::sleep(duration).await;
}
7 changes: 3 additions & 4 deletions rust/bitbazaar/threads/batch_futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use futures::{
Future, FutureExt, StreamExt,
};

use crate::misc::sleep_compat;

macro_rules! batch_futures_flat_impl {
($limit:expr, $fut_cbs:expr, |$result:ident| $call_cb:expr) => {{
let mut return_index = 0;
Expand Down Expand Up @@ -184,10 +186,7 @@ macro_rules! batch_futures_descendants_impl {
// Doing 100ms and the error!() log so we know if this isn't true and our understanding is incorrect. 100ms is slow enough to not cause performance issues.
seen_stream_empty = true;

#[cfg(target_arch = "wasm32")]
gloo_timers::future::TimeoutFuture::new(100).await;
#[cfg(not(target_arch = "wasm32"))]
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
sleep_compat(std::time::Duration::from_millis(100)).await;
}
}
#[allow(unreachable_code)]
Expand Down

0 comments on commit d4eddc9

Please sign in to comment.