1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::marker::PhantomData;
use susy_jsonrpc_core as core;
use susy_jsonrpc_pubsub as pubsub;
use serde;
use util::to_value;
use self::core::futures::{self, Future, Sink as FuturesSink, sync};
pub use self::pubsub::SubscriptionId;
#[derive(Debug)]
pub struct Subscriber<T, E = core::Error> {
subscriber: pubsub::Subscriber,
_data: PhantomData<(T, E)>,
}
impl<T, E> Subscriber<T, E> {
pub fn new(subscriber: pubsub::Subscriber) -> Self {
Subscriber {
subscriber,
_data: PhantomData,
}
}
pub fn new_test<M: Into<String>>(method: M) -> (
Self,
pubsub::oneshot::Receiver<Result<SubscriptionId, core::Error>>,
sync::mpsc::Receiver<String>,
) {
let (subscriber, id, subscription) = pubsub::Subscriber::new_test(method);
(Subscriber::new(subscriber), id, subscription)
}
pub fn reject(self, error: core::Error) -> Result<(), ()> {
self.subscriber.reject(error)
}
pub fn reject_async(self, error: core::Error) -> impl Future<Item = (), Error = ()> {
self.subscriber.reject_async(error)
}
pub fn assign_id(self, id: SubscriptionId) -> Result<Sink<T, E>, ()> {
self.subscriber.assign_id(id.clone())
.map(|sink| Sink {
id,
sink,
buffered: None,
_data: PhantomData,
})
}
pub fn assign_id_async(self, id: SubscriptionId) -> impl Future<Item = Sink<T, E>, Error = ()> {
self.subscriber.assign_id_async(id.clone())
.map(|sink| Sink {
id,
sink,
buffered: None,
_data: PhantomData,
})
}
}
#[derive(Debug, Clone)]
pub struct Sink<T, E = core::Error> {
sink: pubsub::Sink,
id: SubscriptionId,
buffered: Option<core::Params>,
_data: PhantomData<(T, E)>,
}
impl<T: serde::Serialize, E: serde::Serialize> Sink<T, E> {
pub fn notify(&self, val: Result<T, E>) -> pubsub::SinkResult {
self.sink.notify(self.val_to_params(val))
}
fn val_to_params(&self, val: Result<T, E>) -> core::Params {
let id = self.id.clone().into();
let val = val.map(to_value).map_err(to_value);
core::Params::Map(vec![
("subscription".to_owned(), id),
match val {
Ok(val) => ("result".to_owned(), val),
Err(err) => ("error".to_owned(), err),
},
].into_iter().collect())
}
fn poll(&mut self) -> futures::Poll<(), pubsub::TransportError> {
if let Some(item) = self.buffered.take() {
let result = self.sink.start_send(item)?;
if let futures::AsyncSink::NotReady(item) = result {
self.buffered = Some(item);
}
}
if self.buffered.is_some() {
Ok(futures::Async::NotReady)
} else {
Ok(futures::Async::Ready(()))
}
}
}
impl<T: serde::Serialize, E: serde::Serialize> futures::sink::Sink for Sink<T, E> {
type SinkItem = Result<T, E>;
type SinkError = pubsub::TransportError;
fn start_send(&mut self, item: Self::SinkItem) -> futures::StartSend<Self::SinkItem, Self::SinkError> {
if self.poll()?.is_not_ready() {
return Ok(futures::AsyncSink::NotReady(item));
}
let val = self.val_to_params(item);
self.buffered = Some(val);
self.poll()?;
Ok(futures::AsyncSink::Ready)
}
fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> {
self.poll()?;
self.sink.poll_complete()
}
fn close(&mut self) -> futures::Poll<(), Self::SinkError> {
self.poll()?;
self.sink.close()
}
}