提交 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 {
where
R: Request,
{
// Default will work for `()` response
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(),
})
}
let method = request.as_method();
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)]
struct RequestMessagePrivate {
struct RequestMessagePrivate<'a, R: Serialize> {
id: u32,
method: &'static str,
#[serde(flatten)]
message: Value,
request: &'a R,
}
let id;
......@@ -370,13 +342,13 @@ impl Channel {
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(
serde_json::to_vec(&RequestMessagePrivate {
id,
method,
message,
request: &request,
})
.unwrap(),
);
......@@ -419,16 +391,37 @@ impl Channel {
)
.await?;
match result {
let data = match result {
Ok(data) => {
debug!("request succeeded [method:{}, id:{}]", method, id);
Ok(data)
data
}
Err(ResponseError { 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 {
where
R: Request,
{
// Default will work for `()` response
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)))
}
let method = request.as_method();
/// 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)]
struct RequestMessagePrivate {
struct RequestMessagePrivate<'a, R> {
id: u32,
method: &'static str,
#[serde(flatten)]
message: Value,
request: &'a R,
}
let id;
......@@ -364,13 +315,13 @@ impl PayloadChannel {
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(
serde_json::to_vec(&RequestMessagePrivate {
id,
method,
message,
request: &request,
})
.unwrap(),
);
......@@ -427,31 +378,41 @@ impl PayloadChannel {
)
.await?;
match result {
let data = match result {
Ok(data) => {
debug!("request succeeded [method:{}, id:{}]", method, id);
Ok(data)
data
}
Err(ResponseError { 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
async fn notify_internal(
pub(crate) async fn notify<N>(
&self,
event: &'static str,
notification: Value,
notification: N,
payload: Bytes,
) -> Result<(), NotificationError> {
) -> Result<(), NotificationError>
where
N: Notification,
{
let event = notification.as_event();
#[derive(Debug, Serialize)]
struct NotificationMessagePrivate {
struct NotificationMessagePrivate<'a, N: Serialize> {
event: &'static str,
#[serde(flatten)]
notification: Value,
notification: &'a N,
}
debug!("notify() [event:{}]", event);
......@@ -463,7 +424,7 @@ impl PayloadChannel {
let bytes = Bytes::from(
serde_json::to_vec(&NotificationMessagePrivate {
event,
notification,
notification: &notification,
})
.unwrap(),
);
......@@ -481,4 +442,18 @@ impl PayloadChannel {
.await
.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 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论