Skip to content

Commit 84ef222

Browse files
committed
Improving the external API. (#108)
* Improving the external API. * Fix output format for subscriptions.
1 parent ad06827 commit 84ef222

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

core/src/io.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ impl<T: Metadata, S: Middleware<T>> MetaIoHandler<T, S> {
192192
}).boxed()
193193
}
194194

195-
fn handle_call(&self, call: Call, meta: T) -> BoxFuture<Option<Output>, ()> {
195+
/// Handle single call asynchronously.
196+
pub fn handle_call(&self, call: Call, meta: T) -> BoxFuture<Option<Output>, ()> {
196197
match call {
197198
Call::MethodCall(method) => {
198199
let params = method.params.unwrap_or(Params::None);

core/src/types/request.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ pub enum Call {
4747

4848
}
4949

50+
impl From<MethodCall> for Call {
51+
fn from(mc: MethodCall) -> Self {
52+
Call::MethodCall(mc)
53+
}
54+
}
55+
56+
impl From<Notification> for Call {
57+
fn from(n: Notification) -> Self {
58+
Call::Notification(n)
59+
}
60+
}
61+
5062
impl Serialize for Call {
5163
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
5264
where S: Serializer {

macros/src/auto_args.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ macro_rules! build_rpc_trait {
233233
)
234234
}),
235235
($unsubscribe, move |base, id| {
236+
use $crate::jsonrpc_core::futures::Future;
236237
Self::$unsub_method(base, id).map($crate::to_value).boxed()
237238
}),
238239
);

macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extern crate jsonrpc_core;
1+
pub extern crate jsonrpc_core;
22
extern crate jsonrpc_pubsub;
33
extern crate serde;
44

macros/src/pubsub.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ impl<T> Subscriber<T> {
2525
}
2626

2727
pub fn assign_id(self, id: SubscriptionId) -> Result<Sink<T>, ()> {
28-
let sink = self.subscriber.assign_id(id)?;
28+
let sink = self.subscriber.assign_id(id.clone())?;
2929
Ok(Sink {
30+
id: id,
3031
sink: sink,
3132
_data: PhantomData,
3233
})
@@ -35,12 +36,17 @@ impl<T> Subscriber<T> {
3536

3637
pub struct Sink<T> {
3738
sink: pubsub::Sink,
39+
id: SubscriptionId,
3840
_data: PhantomData<T>,
3941
}
4042

4143
impl<T: serde::Serialize> Sink<T> {
4244
pub fn send(&self, val: T) -> pubsub::SinkResult {
45+
let id = self.id.clone().into();
4346
let val = to_value(val);
44-
self.sink.send(core::Params::Array(vec![val]))
47+
self.sink.send(core::Params::Map(vec![
48+
("subscription".to_owned(), id),
49+
("result".to_owned(), val),
50+
].into_iter().collect()))
4551
}
4652
}

pubsub/src/subscription.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Subscription primitives.
22
3+
use std::fmt;
34
use std::collections::HashMap;
45
use std::sync::Arc;
56
use parking_lot::Mutex;
@@ -19,6 +20,15 @@ pub struct Session {
1920
on_drop: Mutex<Vec<Box<Fn() + Send>>>,
2021
}
2122

23+
impl fmt::Debug for Session {
24+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
25+
fmt.debug_struct("pubsub::Session")
26+
.field("active_subscriptions", &self.active_subscriptions.lock().len())
27+
.field("transport", &self.transport)
28+
.finish()
29+
}
30+
}
31+
2232
impl Session {
2333
/// Creates new session given transport raw send capabilities.
2434
/// Session should be created as part of metadata, `sender` should be returned by transport.

0 commit comments

Comments
 (0)