Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(swarm): Add wasm-bindgen executor #3115

Merged
merged 53 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
ce2cb5c
Initial prototype commit
umgefahren Nov 8, 2022
6cb302f
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 8, 2022
b69dc35
Update swarm/src/connection/pool.rs
umgefahren Nov 9, 2022
12e0a32
Moving tokio and async std executors
umgefahren Nov 9, 2022
ad2ad9b
Implement other changes
umgefahren Nov 9, 2022
92da6a2
Formatted
umgefahren Nov 9, 2022
e7253d1
Update swarm/src/connection/pool.rs
umgefahren Nov 10, 2022
3a24540
Update swarm/src/lib.rs
umgefahren Nov 10, 2022
98dc7ce
Implemented changes
umgefahren Nov 10, 2022
38b21d7
Merge branch 'executor-aware-swarm' of github.com:umgefahren/rust-lib…
umgefahren Nov 10, 2022
804b7d6
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 10, 2022
8fd5e56
Fix tests
umgefahren Nov 10, 2022
ee9354c
Modified CI
umgefahren Nov 10, 2022
5381a2c
Corrected some clippy issues
umgefahren Nov 10, 2022
dc5f1b7
Update core/Cargo.toml
umgefahren Nov 10, 2022
b82db0b
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 11, 2022
5365389
Implemented suggested changes
umgefahren Nov 11, 2022
50ee927
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 12, 2022
e055d2a
Apply suggestions from code review
umgefahren Nov 12, 2022
5feb2e5
Implemented requested changes
umgefahren Nov 12, 2022
e6761c6
Use new api
umgefahren Nov 13, 2022
20c5a09
Update swarm/src/lib.rs
umgefahren Nov 13, 2022
eb7836d
Update swarm/src/lib.rs
umgefahren Nov 13, 2022
361ec51
Update swarm/src/lib.rs
umgefahren Nov 13, 2022
da955ba
Implemented more ergonomic api
umgefahren Nov 13, 2022
7d8cbe3
Move executor
umgefahren Nov 13, 2022
15607ba
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 13, 2022
f0493ed
Correct bad merge
umgefahren Nov 13, 2022
91676a9
Do fmt
umgefahren Nov 13, 2022
d99b802
Added changelog entries
umgefahren Nov 13, 2022
36e1a67
Add wasm executor
umgefahren Nov 13, 2022
8a0f632
Run cargo fmt
umgefahren Nov 13, 2022
05deb81
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 13, 2022
6f15098
Merge branch 'master' into wasm-bindgen-executor
umgefahren Nov 13, 2022
69fa671
Apply suggestions from code review
umgefahren Nov 13, 2022
f27fdf8
Implement small changes
umgefahren Nov 13, 2022
742f555
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 13, 2022
f61686d
Apply suggestions from code review
umgefahren Nov 14, 2022
d193da3
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 14, 2022
30c5020
Addressed Max's concerns.
umgefahren Nov 14, 2022
3a00d9a
Merge executor-aware-swarm into wasm-bindgen-executor
umgefahren Nov 14, 2022
5b79eae
Merge branch 'master' into wasm-bindgen-executor
umgefahren Nov 16, 2022
2a76289
Addressed cargo fmt issues
umgefahren Nov 17, 2022
7eb3c4e
Merge branch 'master' into wasm-bindgen-executor
umgefahren Nov 17, 2022
a9258f3
Improved documentation
umgefahren Nov 17, 2022
e928aa2
Merge branch 'master' into wasm-bindgen-executor
umgefahren Nov 18, 2022
ac44d27
Update swarm/src/executor.rs
thomaseizinger Nov 22, 2022
aa97ecb
Merge branch 'master' into wasm-bindgen-executor
thomaseizinger Nov 23, 2022
d12a964
Merge branch 'master' into wasm-bindgen-executor
mergify[bot] Nov 23, 2022
18de8e5
Merge branch 'master' into wasm-bindgen-executor
mxinden Nov 23, 2022
6f8e8a8
Merge branch 'master' into wasm-bindgen-executor
mxinden Nov 23, 2022
81749db
Merge branch 'master' into wasm-bindgen-executor
mxinden Nov 23, 2022
cf80bca
Merge branch 'master' into wasm-bindgen-executor
mergify[bot] Nov 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
umgefahren marked this conversation as resolved.
Show resolved Hide resolved
}
}
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