提交 45bb7d7f authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Make request and notification methods non-generic: it doesn't cause significant…

Make request and notification methods non-generic: it doesn't cause significant binary bloat, so should be fine
上级 b5977243
...@@ -313,42 +313,14 @@ impl Channel { ...@@ -313,42 +313,14 @@ impl Channel {
where where
R: Request, R: Request,
{ {
// Default will work for `()` response let method = request.as_method();
let data = self
.request_internal(request.as_method(), serde_json::to_value(request).unwrap())
.await?
.unwrap_or_default();
serde_json::from_value(data).map_err(|error| RequestError::FailedToParse {
error: error.to_string(),
})
}
pub(crate) fn subscribe_to_notifications<F>(
&self,
target_id: SubscriptionTarget,
callback: F,
) -> Option<SubscriptionHandler>
where
F: Fn(Value) + Send + Sync + 'static,
{
self.inner
.event_handlers_weak
.upgrade()
.map(|event_handlers| event_handlers.add(target_id, Box::new(callback)))
}
/// Non-generic method to avoid significant duplication in final binary
async fn request_internal(
&self,
method: &'static str,
message: Value,
) -> Result<Option<Value>, RequestError> {
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct RequestMessagePrivate { struct RequestMessagePrivate<'a, R: Serialize> {
id: u32, id: u32,
method: &'static str, method: &'static str,
#[serde(flatten)] #[serde(flatten)]
message: Value, request: &'a R,
} }
let id; let id;
...@@ -370,13 +342,13 @@ impl Channel { ...@@ -370,13 +342,13 @@ impl Channel {
requests_container.handlers.insert(id, result_sender); requests_container.handlers.insert(id, result_sender);
} }
debug!("request() [method:{}, id:{}]: {}", method, id, message); debug!("request() [method:{}, id:{}]: {:?}", method, id, request);
let bytes = Bytes::from( let bytes = Bytes::from(
serde_json::to_vec(&RequestMessagePrivate { serde_json::to_vec(&RequestMessagePrivate {
id, id,
method, method,
message, request: &request,
}) })
.unwrap(), .unwrap(),
); );
...@@ -419,16 +391,37 @@ impl Channel { ...@@ -419,16 +391,37 @@ impl Channel {
) )
.await?; .await?;
match result { let data = match result {
Ok(data) => { Ok(data) => {
debug!("request succeeded [method:{}, id:{}]", method, id); debug!("request succeeded [method:{}, id:{}]", method, id);
Ok(data) data
} }
Err(ResponseError { reason }) => { Err(ResponseError { reason }) => {
debug!("request failed [method:{}, id:{}]: {}", method, id, reason); debug!("request failed [method:{}, id:{}]: {}", method, id, reason);
Err(RequestError::Response { reason }) return Err(RequestError::Response { reason });
} }
} };
// Default will work for `()` response
serde_json::from_value(data.unwrap_or_default()).map_err(|error| {
RequestError::FailedToParse {
error: error.to_string(),
}
})
}
pub(crate) fn subscribe_to_notifications<F>(
&self,
target_id: SubscriptionTarget,
callback: F,
) -> Option<SubscriptionHandler>
where
F: Fn(Value) + Send + Sync + 'static,
{
self.inner
.event_handlers_weak
.upgrade()
.map(|event_handlers| event_handlers.add(target_id, Box::new(callback)))
} }
} }
...@@ -286,63 +286,14 @@ impl PayloadChannel { ...@@ -286,63 +286,14 @@ impl PayloadChannel {
where where
R: Request, R: Request,
{ {
// Default will work for `()` response let method = request.as_method();
let data = self
.request_internal(
request.as_method(),
serde_json::to_value(request).unwrap(),
payload,
)
.await?
.unwrap_or_default();
serde_json::from_value(data).map_err(|error| RequestError::FailedToParse {
error: error.to_string(),
})
}
pub(crate) async fn notify<N>(
&self,
notification: N,
payload: Bytes,
) -> Result<(), NotificationError>
where
N: Notification,
{
self.notify_internal(
notification.as_event(),
serde_json::to_value(notification).unwrap(),
payload,
)
.await
}
pub(crate) fn subscribe_to_notifications<F>(
&self,
target_id: SubscriptionTarget,
callback: F,
) -> Option<SubscriptionHandler>
where
F: Fn(NotificationMessage) + Send + Sync + 'static,
{
self.inner
.event_handlers_weak
.upgrade()
.map(|event_handlers| event_handlers.add(target_id, Box::new(callback)))
}
/// Non-generic method to avoid significant duplication in final binary
async fn request_internal(
&self,
method: &'static str,
message: Value,
payload: Bytes,
) -> Result<Option<Value>, RequestError> {
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct RequestMessagePrivate { struct RequestMessagePrivate<'a, R> {
id: u32, id: u32,
method: &'static str, method: &'static str,
#[serde(flatten)] #[serde(flatten)]
message: Value, request: &'a R,
} }
let id; let id;
...@@ -364,13 +315,13 @@ impl PayloadChannel { ...@@ -364,13 +315,13 @@ impl PayloadChannel {
requests_container.handlers.insert(id, result_sender); requests_container.handlers.insert(id, result_sender);
} }
debug!("request() [method:{}, id:{}]: {}", method, id, message); debug!("request() [method:{}, id:{}]: {:?}", method, id, request);
let bytes = Bytes::from( let bytes = Bytes::from(
serde_json::to_vec(&RequestMessagePrivate { serde_json::to_vec(&RequestMessagePrivate {
id, id,
method, method,
message, request: &request,
}) })
.unwrap(), .unwrap(),
); );
...@@ -427,31 +378,41 @@ impl PayloadChannel { ...@@ -427,31 +378,41 @@ impl PayloadChannel {
) )
.await?; .await?;
match result { let data = match result {
Ok(data) => { Ok(data) => {
debug!("request succeeded [method:{}, id:{}]", method, id); debug!("request succeeded [method:{}, id:{}]", method, id);
Ok(data) data
} }
Err(ResponseError { reason }) => { Err(ResponseError { reason }) => {
debug!("request failed [method:{}, id:{}]: {}", method, id, reason); debug!("request failed [method:{}, id:{}]: {}", method, id, reason);
Err(RequestError::Response { reason }) return Err(RequestError::Response { reason });
} }
} };
// Default will work for `()` response
serde_json::from_value(data.unwrap_or_default()).map_err(|error| {
RequestError::FailedToParse {
error: error.to_string(),
}
})
} }
/// Non-generic method to avoid significant duplication in final binary pub(crate) async fn notify<N>(
async fn notify_internal(
&self, &self,
event: &'static str, notification: N,
notification: Value,
payload: Bytes, payload: Bytes,
) -> Result<(), NotificationError> { ) -> Result<(), NotificationError>
where
N: Notification,
{
let event = notification.as_event();
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct NotificationMessagePrivate { struct NotificationMessagePrivate<'a, N: Serialize> {
event: &'static str, event: &'static str,
#[serde(flatten)] #[serde(flatten)]
notification: Value, notification: &'a N,
} }
debug!("notify() [event:{}]", event); debug!("notify() [event:{}]", event);
...@@ -463,7 +424,7 @@ impl PayloadChannel { ...@@ -463,7 +424,7 @@ impl PayloadChannel {
let bytes = Bytes::from( let bytes = Bytes::from(
serde_json::to_vec(&NotificationMessagePrivate { serde_json::to_vec(&NotificationMessagePrivate {
event, event,
notification, notification: &notification,
}) })
.unwrap(), .unwrap(),
); );
...@@ -481,4 +442,18 @@ impl PayloadChannel { ...@@ -481,4 +442,18 @@ impl PayloadChannel {
.await .await
.map_err(|_| NotificationError::ChannelClosed {}) .map_err(|_| NotificationError::ChannelClosed {})
} }
pub(crate) fn subscribe_to_notifications<F>(
&self,
target_id: SubscriptionTarget,
callback: F,
) -> Option<SubscriptionHandler>
where
F: Fn(NotificationMessage) + Send + Sync + 'static,
{
self.inner
.event_handlers_weak
.upgrade()
.map(|event_handlers| event_handlers.add(target_id, Box::new(callback)))
}
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论