*: Enable libp2p to run via wasm32-unknown-unknown in the browser (#2320)

Changes needed to get libp2p to run via `wasm32-unknown-unknown` in the browser
(both main thread and inside web workers).

Replaces wasm-timer with futures-timer and instant.

Co-authored-by: Oliver Wangler <oliver@wngr.de>
This commit is contained in:
Max Inden
2021-10-30 12:41:30 +02:00
committed by GitHub
parent b8f0e44b27
commit ff5d455ccf
50 changed files with 365 additions and 86 deletions

View File

@ -310,8 +310,8 @@ fn handle_outbound_event(
expiring_registrations.extend(registrations.iter().cloned().map(|registration| {
async move {
// if the timer errors we consider it expired
let _ =
wasm_timer::Delay::new(Duration::from_secs(registration.ttl as u64)).await;
let _ = futures_timer::Delay::new(Duration::from_secs(registration.ttl as u64))
.await;
(registration.record.peer_id(), registration.namespace)
}

View File

@ -381,14 +381,8 @@ impl Registrations {
self.registrations
.insert(registration_id, registration.clone());
let next_expiry = wasm_timer::Delay::new(Duration::from_secs(ttl as u64))
.map(move |result| {
if result.is_err() {
log::warn!("Timer for registration {} has unexpectedly errored, treating it as expired", registration_id.0);
}
registration_id
})
let next_expiry = futures_timer::Delay::new(Duration::from_secs(ttl as u64))
.map(move |_| registration_id)
.boxed();
self.next_expiry.push(next_expiry);
@ -496,8 +490,8 @@ pub struct CookieNamespaceMismatch;
#[cfg(test)]
mod tests {
use instant::SystemTime;
use std::option::Option::None;
use std::time::SystemTime;
use libp2p_core::{identity, PeerRecord};

View File

@ -27,6 +27,7 @@
use futures::future::{self, BoxFuture, Fuse, FusedFuture};
use futures::FutureExt;
use instant::Instant;
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use libp2p_swarm::protocols_handler::{InboundUpgradeSend, OutboundUpgradeSend};
use libp2p_swarm::{
@ -38,7 +39,7 @@ use std::fmt;
use std::future::Future;
use std::hash::Hash;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
use std::time::Duration;
use void::Void;
/// Handles a substream throughout its lifetime.