mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-14 02:21:21 +00:00
feat(swarm): Add wasm-bindgen
executor (#3115)
This commit is contained in:
@ -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"]
|
||||
|
@ -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 }
|
||||
@ -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"] }
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user