// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see .
use crate::error::Result as ClientResult;
use async_std::{
channel::{bounded, Receiver, Sender},
stream::StreamExt,
};
use futures::{FutureExt, Stream};
use pezsp_runtime::DeserializeOwned;
use std::{
fmt::Debug,
pin::Pin,
result::Result as StdResult,
task::{Context, Poll},
};
/// Once channel reaches this capacity, the subscription breaks.
const CHANNEL_CAPACITY: usize = 128;
/// Structure describing a stream.
#[derive(Clone)]
pub struct StreamDescription {
stream_name: String,
chain_name: String,
}
impl StreamDescription {
/// Create a new instance of `StreamDescription`.
pub fn new(stream_name: String, chain_name: String) -> Self {
Self { stream_name, chain_name }
}
/// Get a stream description.
fn get(&self) -> String {
format!("{} stream of {}", self.stream_name, self.chain_name)
}
}
/// Chainable stream that transforms items of type `Result` to items of type `T`.
///
/// If it encounters an item of type `Err`, it returns `Poll::Ready(None)`
/// and terminates the underlying stream.
struct Unwrap>, T, E> {
desc: StreamDescription,
stream: Option,
}
impl>, T, E> Unwrap {
/// Create a new instance of `Unwrap`.
pub fn new(desc: StreamDescription, stream: S) -> Self {
Self { desc, stream: Some(stream) }
}
}
impl> + Unpin, T: DeserializeOwned, E: Debug> Stream
for Unwrap
{
type Item = T;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll