Skip to content
This repository was archived by the owner on Feb 22, 2026. It is now read-only.

Commit 78c9be6

Browse files
luca992obmarg
authored andcommitted
feat: add Client.stop(subscription_id)
Maintaining a list of subscriptions on a connection to be able to stop them with a list of Subscription items is difficult/complex due to the generics. I couldn't figure out how to box them when I am sending different subscription types. Instead just exposing the subscription id and allowing you to cancel the subscription by id on the client itself allows you to manage subscriptions easily and not have to worry about the request types at all.
1 parent 6ced041 commit 78c9be6

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/client/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ impl Client {
121121
})
122122
}
123123

124+
/// Stops a subscription by id
125+
///
126+
/// # Errors
127+
///
128+
/// Will return `Err` if the connection actor has already been shut down.
129+
pub async fn stop(self, subscription_id: SubscriptionId) -> Result<(), Error> {
130+
self.actor
131+
.send(ConnectionCommand::Cancel(subscription_id))
132+
.await
133+
.map_err(|error| Error::Send(error.to_string()))
134+
}
135+
124136
/// Gracefully closes the connection
125137
///
126138
/// This will stop all running subscriptions and shut down the [`ConnectionActor`] wherever

src/client/subscription.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ impl<Operation> Subscription<Operation>
4141
where
4242
Operation: GraphqlOperation + Send,
4343
{
44-
/// Stops the subscription by sending a Complete message to the server.
44+
/// Returns the identifier for this subscription.
45+
///
46+
/// This can be used with [`crate::Client::stop`] to stop
47+
/// a running subscription without needing access to the `Subscription`
48+
/// itself.
49+
pub fn id(&self) -> SubscriptionId {
50+
self.id
51+
}
52+
53+
/// Stops this subscription
4554
///
4655
/// # Errors
4756
///
48-
/// Will return `Err` if the stop operation fails.
57+
/// Will return `Err` if the connection actor has already been shut down.
4958
pub async fn stop(self) -> Result<(), Error> {
5059
self.actor
5160
.send(ConnectionCommand::Cancel(self.id))

0 commit comments

Comments
 (0)