Skip to content

Subscription Close detection mechanism / event? #1068

Answered by AadamZ5
AadamZ5 asked this question in Q&A
Discussion options

You must be logged in to vote

Possible answer for now

I've implemented a quick Stream wrapper called DropStream<T>.

use futures::{
    channel::oneshot::{Receiver, Sender},
    Stream,
};
use std::pin::Pin;

/// Stream wrapper that sends a message to a oneshot reciever upon being dropped.
#[derive(Debug)]
pub struct DropStream<T>
where
    T: Stream + ?Sized,
{
    pub stream: Pin<Box<T>>,
    pub tx: Option<Sender<()>>,
}

impl<T> DropStream<T>
where
    T: Stream + ?Sized,
{
    pub fn new(stream: Pin<Box<T>>) -> (DropStream<T>, Receiver<()>) {
        let (tx, rx) = futures::channel::oneshot::channel();

        let myself = Self {
            stream,
            tx: Some(tx),
        };

        return (myself, rx);

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@ilslv
Comment options

Answer selected by ilslv
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants