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

Remove debounced dependency #2138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Remove debounced dependency #2138

wants to merge 1 commit into from

Conversation

bim9262
Copy link
Collaborator

@bim9262 bim9262 commented Feb 17, 2025

No description provided.

@@ -149,7 +145,13 @@ impl PrivacyMonitor for Monitor<'_> {
break;
}
},
_ = self.updates.next() => break,
_ = self.stream.next() => {
Copy link
Collaborator Author

@bim9262 bim9262 Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any thoughts on adding our own stream extensions that could include this pattern (currently used in three blocks)?

impl<T: ?Sized> StreamExt for T where T: Stream {}

#[async_trait]
pub trait StreamExt: Stream {
    async fn consume_for(&mut self, duration: Duration)
    where
        Self: Unpin,
    {
        let _ = self.next().await;
        let _ = tokio::time::timeout(duration, async {
            loop {
                let _ = self.next().await;
            }
        })
        .await;
    }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MaxVerevkin if you like the idea, but have a better function name, I'm not too fond of consume_for.
wait_for_next_then_discard_for_duration seems a bit too verbose.

Copy link
Collaborator

@MaxVerevkin MaxVerevkin Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your implementation is not cancel-safe, which may be a problem. Perhaps something like (this does not have a timeout and expects successive items to be available immediately, not sure if it is good enough. on a plus side it does not have a delay 😄 )

pub trait StreamExtDebounced: futures::StreamExt {
    fn next_debounced(&mut self) -> impl Future<Output = Option<Self::Item>>;
}

impl<T: futures::StreamExt + Unpin> StreamExtDebounced for T {
    async fn next_debounced(&mut self) -> Option<Self::Item> {
        let mut result = self.next().await?;
        let mut noop_ctx = std::task::Context::from_waker(std::task::Waker::noop());
        loop {
            match self.poll_next_unpin(&mut noop_ctx) {
                std::task::Poll::Ready(Some(x)) => result = x,
                std::task::Poll::Ready(None) | std::task::Poll::Pending => return Some(result),
            }
        }
    }
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::task::Waker::noop() is now conveniently stable :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will wait until #2143 is merged so that use std::future::Future; doesn't need to be imported too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants