Skip to content

Commit 96e45ae

Browse files
committed
feat: further wasm preparations
1 parent e7547d2 commit 96e45ae

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,6 @@ missing_debug_implementations = "warn"
9595
# do. To enable for a crate set `#![cfg_attr(iroh_docsrs,
9696
# feature(doc_cfg))]` in the crate.
9797
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(iroh_docsrs)"] }
98+
99+
[build-dependencies]
100+
cfg_aliases = "0.2.1"

src/actor.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use bytes::Bytes;
1313
use futures_util::FutureExt;
1414
use iroh_blobs::Hash;
1515
use irpc::channel::mpsc;
16+
use n0_future::task::JoinSet;
1617
use serde::{Deserialize, Serialize};
17-
use tokio::{sync::oneshot, task::JoinSet};
18+
use tokio::sync::oneshot;
1819
use tracing::{debug, error, error_span, trace, warn};
1920

2021
use crate::{
@@ -226,7 +227,10 @@ struct OpenReplica {
226227
#[derive(Debug, Clone)]
227228
pub struct SyncHandle {
228229
tx: async_channel::Sender<Action>,
229-
join_handle: Arc<Option<JoinHandle<()>>>,
230+
#[cfg(wasm_browser)]
231+
join_handle: Arc<Option<n0_future::task::JoinHandle<()>>>,
232+
#[cfg(not(wasm_browser))]
233+
join_handle: Arc<Option<std::thread::JoinHandle<()>>>,
230234
metrics: Arc<Metrics>,
231235
}
232236

@@ -270,13 +274,18 @@ impl SyncHandle {
270274
tasks: Default::default(),
271275
metrics: metrics.clone(),
272276
};
277+
278+
#[cfg(wasm_browser)]
279+
let join_handle = n0_future::task::spawn(actor.run_async());
280+
281+
#[cfg(not(wasm_browser))]
273282
let join_handle = std::thread::Builder::new()
274283
.name("sync-actor".to_string())
275284
.spawn(move || {
276285
let span = error_span!("sync", %me);
277286
let _enter = span.enter();
278287

279-
if let Err(err) = actor.run() {
288+
if let Err(err) = actor.run_in_thread() {
280289
error!("Sync actor closed with error: {err:?}");
281290
}
282291
})
@@ -600,6 +609,8 @@ impl Drop for SyncHandle {
600609
// when called from inside a tokio runtime.
601610
self.tx.send_blocking(Action::Shutdown { reply: None }).ok();
602611
let handle = handle.take().expect("this can only run once");
612+
613+
#[cfg(not(wasm_browser))]
603614
if let Err(err) = handle.join() {
604615
warn!(?err, "Failed to join sync actor");
605616
}
@@ -617,7 +628,11 @@ struct Actor {
617628
}
618629

619630
impl Actor {
620-
fn run(self) -> Result<()> {
631+
async fn run_in_task(self) {
632+
self.run_async().await
633+
}
634+
635+
fn run_in_thread(self) -> Result<()> {
621636
let rt = tokio::runtime::Builder::new_current_thread()
622637
.enable_time()
623638
.build()?;
@@ -628,7 +643,7 @@ impl Actor {
628643

629644
async fn run_async(mut self) {
630645
let reply = loop {
631-
let timeout = tokio::time::sleep(MAX_COMMIT_DELAY);
646+
let timeout = n0_future::time::sleep(MAX_COMMIT_DELAY);
632647
tokio::pin!(timeout);
633648
let action = tokio::select! {
634649
_ = &mut timeout => {

0 commit comments

Comments
 (0)