Skip to content

Commit

Permalink
feat(server): add SubscriptionMessage::new (#1176)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Aug 10, 2023
1 parent 098766d commit 3bd2d20
Showing 1 changed file with 13 additions and 0 deletions.
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

0 comments on commit 3bd2d20

Please sign in to comment.