Skip to content

Commit

Permalink
refactor: move convenience impl into toolbox
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Feb 17, 2023
1 parent b0b1cf2 commit bfb894d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
21 changes: 3 additions & 18 deletions ethers-providers/src/rpc/pubsub.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{JsonRpcClient, Middleware, Provider, TransactionStream};
use crate::{JsonRpcClient, Middleware, Provider};

use ethers_core::types::{TxHash, U256};
use ethers_core::types::U256;

use futures_util::stream::Stream;
use pin_project::{pin_project, pinned_drop};
Expand Down Expand Up @@ -35,7 +35,7 @@ pub struct SubscriptionStream<'a, P: PubsubClient, R: DeserializeOwned> {

loaded_elements: VecDeque<R>,

provider: &'a Provider<P>,
pub(crate) provider: &'a Provider<P>,

#[pin]
rx: P::NotificationStream,
Expand Down Expand Up @@ -114,18 +114,3 @@ where
let _ = (*self.provider).as_ref().unsubscribe(self.id);
}
}

impl<'a, P> SubscriptionStream<'a, P, TxHash>
where
P: PubsubClient,
{
/// Returns a stream that yields the `Transaction`s for the transaction hashes this stream
/// yields.
///
/// This internally calls `Provider::get_transaction` with every new transaction.
/// No more than n futures will be buffered at any point in time, and less than n may also be
/// buffered depending on the state of each future.
pub fn transactions_unordered(self, n: usize) -> TransactionStream<'a, P, Self> {
TransactionStream::new(self.provider, self, n)
}
}
20 changes: 19 additions & 1 deletion ethers-providers/src/stream/tx_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use futures_util::{

use ethers_core::types::{Transaction, TxHash};

use crate::{FilterWatcher, JsonRpcClient, Middleware, Provider, ProviderError};
use crate::{
FilterWatcher, JsonRpcClient, Middleware, Provider, ProviderError, PubsubClient,
SubscriptionStream,
};

/// Errors `TransactionStream` can throw
#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -146,6 +149,21 @@ where
}
}

impl<'a, P> SubscriptionStream<'a, P, TxHash>
where
P: PubsubClient,
{
/// Returns a stream that yields the `Transaction`s for the transaction hashes this stream
/// yields.
///
/// This internally calls `Provider::get_transaction` with every new transaction.
/// No more than n futures will be buffered at any point in time, and less than n may also be
/// buffered depending on the state of each future.
pub fn transactions_unordered(self, n: usize) -> TransactionStream<'a, P, Self> {
TransactionStream::new(self.provider, self, n)
}
}

#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))]
mod tests {
Expand Down

0 comments on commit bfb894d

Please sign in to comment.