Skip to content

Commit

Permalink
feat(swarm): Add wasm-bindgen executor (#3115)
Browse files Browse the repository at this point in the history
  • Loading branch information
umgefahren authored Nov 24, 2022
1 parent 98336b7 commit cff84f1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ serde = ["libp2p-core/serde", "libp2p-kad?/serde", "libp2p-gossipsub?/serde"]
tcp = ["dep:libp2p-tcp"]
tokio = ["libp2p-swarm/tokio", "libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio", "libp2p-quic?/tokio", "libp2p-webrtc?/tokio"]
uds = ["dep:libp2p-uds"]
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js"]
wasm-bindgen = ["futures-timer/wasm-bindgen", "instant/wasm-bindgen", "getrandom/js", "libp2p-swarm/wasm-bindgen"]
wasm-ext = ["dep:libp2p-wasm-ext"]
wasm-ext-websocket = ["wasm-ext", "libp2p-wasm-ext?/websocket"]
webrtc = ["dep:libp2p-webrtc", "libp2p-webrtc?/pem"]
Expand Down
3 changes: 3 additions & 0 deletions swarm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ rand = "0.8"
smallvec = "1.6.1"
thiserror = "1.0"
void = "1"
wasm-bindgen-futures = { version = "0.4.33", optional = true }
getrandom = { version = "0.2.3", features = ["js"], optional = true } # Explicit dependency to be used in `wasm-bindgen` feature

[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
async-std = { version = "1.6.2", optional = true }
Expand All @@ -33,6 +35,7 @@ tokio = { version = "1.15", features = ["rt"], optional = true }
macros = ["dep:libp2p-swarm-derive"]
tokio = ["dep:tokio"]
async-std = ["dep:async-std"]
wasm-bindgen = ["dep:wasm-bindgen-futures", "dep:getrandom"]

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
Expand Down
11 changes: 11 additions & 0 deletions swarm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,14 @@ impl Executor for AsyncStdExecutor {
let _ = async_std::task::spawn(future);
}
}

#[cfg(feature = "wasm-bindgen")]
#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct WasmBindgenExecutor;

#[cfg(feature = "wasm-bindgen")]
impl Executor for WasmBindgenExecutor {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
wasm_bindgen_futures::spawn_local(future)
}
}
23 changes: 23 additions & 0 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,29 @@ where
builder.build()
}

/// Builds a new `Swarm` with a wasm executor.
/// Background tasks will be executed by the browser on the next micro-tick.
///
/// Spawning a task is similar too:
/// ```typescript
/// function spawn(task: () => Promise<void>) {
/// task()
/// }
/// ```
#[cfg(feature = "wasm-bindgen")]
pub fn with_wasm_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
local_peer_id: PeerId,
) -> Self {
Self::with_executor(
transport,
behaviour,
local_peer_id,
crate::executor::WasmBindgenExecutor,
)
}

/// Builds a new `Swarm` without an executor, instead using the current task.
///
/// ## ⚠️ Performance warning
Expand Down

0 comments on commit cff84f1

Please sign in to comment.