From c1a749b01a6f92777fa9450f72d82f89e4a04b65 Mon Sep 17 00:00:00 2001 From: Platon Floria Date: Thu, 10 Aug 2023 20:39:24 +0800 Subject: [PATCH] Add wasm support to transports::Either --- src/transports/either.rs | 76 +++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/src/transports/either.rs b/src/transports/either.rs index d86c2fa8..50d103a2 100644 --- a/src/transports/either.rs +++ b/src/transports/either.rs @@ -1,6 +1,9 @@ //! A strongly-typed transport alternative. use crate::{api, error, rpc, BatchTransport, DuplexTransport, RequestId, Transport}; +#[cfg(feature = "wasm")] +use futures::{future::LocalBoxFuture as BoxFuture, stream::LocalBoxStream as BoxStream}; +#[cfg(not(feature = "wasm"))] use futures::{ future::{BoxFuture, FutureExt}, stream::{BoxStream, StreamExt}, @@ -20,12 +23,21 @@ pub enum Either { Right(B), } +#[cfg(not(feature = "wasm"))] +trait Out: futures::Future> + 'static + Send {} +#[cfg(not(feature = "wasm"))] +impl Out for T where T: futures::Future> + 'static + Send {} +#[cfg(feature = "wasm")] +trait Out: futures::Future> + 'static {} +#[cfg(feature = "wasm")] +impl Out for T where T: futures::Future> + 'static {} + impl Transport for Either where A: Transport, B: Transport, - AOut: futures::Future> + 'static + Send, - BOut: futures::Future> + 'static + Send, + AOut: Out, + BOut: Out, { type Out = BoxFuture<'static, error::Result>; @@ -37,21 +49,36 @@ where } fn send(&self, id: RequestId, request: rpc::Call) -> Self::Out { + #[cfg(not(feature = "wasm"))] match *self { Self::Left(ref a) => a.send(id, request).boxed(), Self::Right(ref b) => b.send(id, request).boxed(), } + #[cfg(feature = "wasm")] + match *self { + Self::Left(ref a) => Box::pin(a.send(id, request)), + Self::Right(ref b) => Box::pin(b.send(id, request)), + } } } +#[cfg(not(feature = "wasm"))] +trait Batch: futures::Future>>> + 'static + Send {} +#[cfg(not(feature = "wasm"))] +impl Batch for T where T: futures::Future>>> + 'static + Send {} +#[cfg(feature = "wasm")] +trait Batch: futures::Future>>> + 'static {} +#[cfg(feature = "wasm")] +impl Batch for T where T: futures::Future>>> + 'static {} + impl BatchTransport for Either where A: BatchTransport, B: BatchTransport, - A::Out: 'static + Send, - B::Out: 'static + Send, - ABatch: futures::Future>>> + 'static + Send, - BBatch: futures::Future>>> + 'static + Send, + A::Out: Out, + B::Out: Out, + ABatch: Batch, + BBatch: Batch, { type Batch = BoxFuture<'static, error::Result>>>; @@ -59,28 +86,51 @@ where where T: IntoIterator, { + #[cfg(not(feature = "wasm"))] match *self { Self::Left(ref a) => a.send_batch(requests).boxed(), Self::Right(ref b) => b.send_batch(requests).boxed(), } + #[cfg(feature = "wasm")] + match *self { + Self::Left(ref a) => Box::pin(a.send_batch(requests)), + Self::Right(ref b) => Box::pin(b.send_batch(requests)), + } } } +#[cfg(not(feature = "wasm"))] +trait NotificationStream: futures::Stream + 'static + Send {} +#[cfg(not(feature = "wasm"))] +impl NotificationStream for T where T: futures::Stream + 'static + Send {} +#[cfg(feature = "wasm")] +trait NotificationStream: futures::Stream + 'static {} +#[cfg(feature = "wasm")] +impl NotificationStream for T where T: futures::Stream + 'static {} + impl DuplexTransport for Either where A: DuplexTransport, B: DuplexTransport, - A::Out: 'static + Send, - B::Out: 'static + Send, - AStream: futures::Stream + 'static + Send, - BStream: futures::Stream + 'static + Send, + A::Out: Out, + B::Out: Out, + AStream: NotificationStream, + BStream: NotificationStream, { type NotificationStream = BoxStream<'static, rpc::Value>; fn subscribe(&self, id: api::SubscriptionId) -> error::Result { - Ok(match *self { - Self::Left(ref a) => a.subscribe(id)?.boxed(), - Self::Right(ref b) => b.subscribe(id)?.boxed(), + Ok({ + #[cfg(not(feature = "wasm"))] + match *self { + Self::Left(ref a) => a.subscribe(id)?.boxed(), + Self::Right(ref b) => b.subscribe(id)?.boxed(), + } + #[cfg(feature = "wasm")] + match *self { + Self::Left(ref a) => Box::pin(a.subscribe(id)?), + Self::Right(ref b) => Box::pin(b.subscribe(id)?), + } }) }