mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 17:21:34 +00:00
*: 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:
@ -26,6 +26,8 @@ use crate::upgrade::SendWrapper;
|
||||
|
||||
use futures::prelude::*;
|
||||
use futures::stream::FuturesUnordered;
|
||||
use futures_timer::Delay;
|
||||
use instant::Instant;
|
||||
use libp2p_core::{
|
||||
connection::{
|
||||
ConnectionHandler, ConnectionHandlerEvent, IntoConnectionHandler, Substream,
|
||||
@ -36,7 +38,6 @@ use libp2p_core::{
|
||||
Connected, Multiaddr,
|
||||
};
|
||||
use std::{error, fmt, pin::Pin, task::Context, task::Poll, time::Duration};
|
||||
use wasm_timer::{Delay, Instant};
|
||||
|
||||
/// Prototype for a `NodeHandlerWrapper`.
|
||||
pub struct NodeHandlerWrapperBuilder<TIntoProtoHandler> {
|
||||
@ -159,7 +160,7 @@ where
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
match self.timeout.poll_unpin(cx) {
|
||||
Poll::Ready(Ok(_)) => {
|
||||
Poll::Ready(()) => {
|
||||
return Poll::Ready((
|
||||
self.user_data
|
||||
.take()
|
||||
@ -167,14 +168,7 @@ where
|
||||
Err(ProtocolsHandlerUpgrErr::Timeout),
|
||||
))
|
||||
}
|
||||
Poll::Ready(Err(_)) => {
|
||||
return Poll::Ready((
|
||||
self.user_data
|
||||
.take()
|
||||
.expect("Future not to be polled again once ready."),
|
||||
Err(ProtocolsHandlerUpgrErr::Timer),
|
||||
))
|
||||
}
|
||||
|
||||
Poll::Pending => {}
|
||||
}
|
||||
|
||||
@ -362,10 +356,16 @@ where
|
||||
(Shutdown::Later(timer, deadline), KeepAlive::Until(t)) => {
|
||||
if *deadline != t {
|
||||
*deadline = t;
|
||||
timer.reset_at(t)
|
||||
if let Some(dur) = deadline.checked_duration_since(Instant::now()) {
|
||||
timer.reset(dur)
|
||||
}
|
||||
}
|
||||
}
|
||||
(_, KeepAlive::Until(t)) => {
|
||||
if let Some(dur) = t.checked_duration_since(Instant::now()) {
|
||||
self.shutdown = Shutdown::Later(Delay::new(dur), t)
|
||||
}
|
||||
}
|
||||
(_, KeepAlive::Until(t)) => self.shutdown = Shutdown::Later(Delay::new_at(t), t),
|
||||
(_, KeepAlive::No) => self.shutdown = Shutdown::Asap,
|
||||
(_, KeepAlive::Yes) => self.shutdown = Shutdown::None,
|
||||
};
|
||||
|
@ -22,10 +22,9 @@ use crate::protocols_handler::{
|
||||
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol,
|
||||
};
|
||||
use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
|
||||
|
||||
use instant::Instant;
|
||||
use smallvec::SmallVec;
|
||||
use std::{error, fmt::Debug, task::Context, task::Poll, time::Duration};
|
||||
use wasm_timer::Instant;
|
||||
|
||||
/// A `ProtocolsHandler` that opens a new substream for each request.
|
||||
// TODO: Debug
|
||||
|
Reference in New Issue
Block a user