Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving the external API. #108

Merged
merged 2 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ impl<T: Metadata, S: Middleware<T>> MetaIoHandler<T, S> {
}).boxed()
}

fn handle_call(&self, call: Call, meta: T) -> BoxFuture<Option<Output>, ()> {
/// Handle single call asynchronously.
pub fn handle_call(&self, call: Call, meta: T) -> BoxFuture<Option<Output>, ()> {
match call {
Call::MethodCall(method) => {
let params = method.params.unwrap_or(Params::None);
Expand Down
12 changes: 12 additions & 0 deletions core/src/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ pub enum Call {

}

impl From<MethodCall> for Call {
fn from(mc: MethodCall) -> Self {
Call::MethodCall(mc)
}
}

impl From<Notification> for Call {
fn from(n: Notification) -> Self {
Call::Notification(n)
}
}

impl Serialize for Call {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
Expand Down
1 change: 1 addition & 0 deletions macros/src/auto_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ macro_rules! build_rpc_trait {
)
}),
($unsubscribe, move |base, id| {
use $crate::jsonrpc_core::futures::Future;
Self::$unsub_method(base, id).map($crate::to_value).boxed()
}),
);
Expand Down
2 changes: 1 addition & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extern crate jsonrpc_core;
pub extern crate jsonrpc_core;
extern crate jsonrpc_pubsub;
extern crate serde;

Expand Down
10 changes: 8 additions & 2 deletions macros/src/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ impl<T> Subscriber<T> {
}

pub fn assign_id(self, id: SubscriptionId) -> Result<Sink<T>, ()> {
let sink = self.subscriber.assign_id(id)?;
let sink = self.subscriber.assign_id(id.clone())?;
Ok(Sink {
id: id,
sink: sink,
_data: PhantomData,
})
Expand All @@ -35,12 +36,17 @@ impl<T> Subscriber<T> {

pub struct Sink<T> {
sink: pubsub::Sink,
id: SubscriptionId,
_data: PhantomData<T>,
}

impl<T: serde::Serialize> Sink<T> {
pub fn send(&self, val: T) -> pubsub::SinkResult {
let id = self.id.clone().into();
let val = to_value(val);
self.sink.send(core::Params::Array(vec![val]))
self.sink.send(core::Params::Map(vec![
("subscription".to_owned(), id),
("result".to_owned(), val),
].into_iter().collect()))
}
}
10 changes: 10 additions & 0 deletions pubsub/src/subscription.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Subscription primitives.

use std::fmt;
use std::collections::HashMap;
use std::sync::Arc;
use parking_lot::Mutex;
Expand All @@ -19,6 +20,15 @@ pub struct Session {
on_drop: Mutex<Vec<Box<Fn() + Send>>>,
}

impl fmt::Debug for Session {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("pubsub::Session")
.field("active_subscriptions", &self.active_subscriptions.lock().len())
.field("transport", &self.transport)
.finish()
}
}

impl Session {
/// Creates new session given transport raw send capabilities.
/// Session should be created as part of metadata, `sender` should be returned by transport.
Expand Down