Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## Unreleased

- Add `RpcSession::handle_incoming_parsed`
and `RpcSession::process_incoming_parsed`

## 0.6.0 - 2024-07-06

- ts client: fix memory leak
Expand Down
17 changes: 16 additions & 1 deletion yerpc/src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl<T: RpcServer> RpcSession<T> {
///
/// Handles incoming requests and notifications,
/// returns a response if any.
///
/// See also [`process_incoming_parsed`].
pub async fn process_incoming(&self, input: &str) -> Option<Message> {
let message: Message = match serde_json::from_str(input) {
Ok(message) => message,
Expand All @@ -66,7 +68,11 @@ impl<T: RpcServer> RpcSession<T> {
)));
}
};

self.process_incoming_parsed(message).await
}
/// Same as [`process_incoming`], but accepts a parsed [`Message`]
/// instead of a string.
pub async fn process_incoming_parsed(&self, message: Message) -> Option<Message> {
match message {
Message::Request(request) => {
let params = request.params.map(Params::into_value).unwrap_or_default();
Expand Down Expand Up @@ -101,11 +107,20 @@ impl<T: RpcServer> RpcSession<T> {
/// Blocks until request handler finishes.
/// Spawn a task if you want to run the request handler
/// concurrently.
///
/// See also [`handle_incoming_parsed`].
pub async fn handle_incoming(&self, input: &str) {
if let Some(response) = self.process_incoming(input).await {
let _ = self.client.tx(response).await;
}
}
/// Same as [`handle_incoming`], but accepts a parsed [`Message`]
/// instead of a string.
pub async fn handle_incoming_parsed(&self, message: Message) {
if let Some(response) = self.process_incoming_parsed(message).await {
let _ = self.client.tx(response).await;
}
}
}

#[derive(Clone)]
Expand Down