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

feat(server): add SubscriptionMessage::new #1176

Merged
merged 1 commit into from
Aug 10, 2023
Merged
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
13 changes: 13 additions & 0 deletions core/src/server/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use super::helpers::{MethodResponse, MethodSink};
use crate::server::error::{DisconnectError, PendingSubscriptionAcceptError, SendTimeoutError, TrySendError};
use crate::server::rpc_module::ConnectionId;
use crate::{traits::IdProvider, Error, StringError};
use jsonrpsee_types::SubscriptionPayload;
use jsonrpsee_types::{
response::SubscriptionError, ErrorObjectOwned, Id, ResponsePayload, SubscriptionId, SubscriptionResponse,
};
Expand Down Expand Up @@ -132,6 +133,18 @@ impl SubscriptionMessage {
serde_json::to_string(t).map(|json| SubscriptionMessage(SubscriptionMessageInner::NeedsData(json)))
}

/// Create a subscription message this is more efficient than [`SubscriptionMessage::from_json`]
/// because it only allocates once.
///
/// Fails if the json `result` couldn't be serialized.
pub fn new(method: &str, subscription: SubscriptionId, result: &impl Serialize) -> Result<Self, serde_json::Error> {
let json = serde_json::to_string(&SubscriptionResponse::new(
method.into(),
SubscriptionPayload { subscription, result },
))?;
Ok(Self::from_complete_message(json))
}

pub(crate) fn from_complete_message(msg: String) -> Self {
SubscriptionMessage(SubscriptionMessageInner::Complete(msg))
}
Expand Down