use crate::rpc::{Error, HandleSubscription, Result};
use core::marker::PhantomData;
use serde::de::DeserializeOwned;
use std::sync::mpsc::Receiver;
#[derive(Debug)]
pub struct TungsteniteSubscriptionWrapper<Notification> {
receiver: Receiver<String>,
_phantom: PhantomData<Notification>,
}
impl<Notification> TungsteniteSubscriptionWrapper<Notification> {
pub fn new(receiver: Receiver<String>) -> Self {
Self { receiver, _phantom: Default::default() }
}
}
#[maybe_async::maybe_async(?Send)]
impl<Notification: DeserializeOwned> HandleSubscription<Notification>
for TungsteniteSubscriptionWrapper<Notification>
{
async fn next(&mut self) -> Option<Result<Notification>> {
let notification = match self.receiver.recv() {
Ok(notif) => notif,
Err(_e) => return None,
};
Some(serde_json::from_str(¬ification).map_err(|_| Error::ExtrinsicFailed(notification)))
}
async fn unsubscribe(self) -> Result<()> {
Ok(())
}
}