-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
WatchStream: Add additional constructor which causes stream to only yield when value changes #5420
Comments
I would be happy to accept a PR that adds a constructor like that. |
I just raised PR #5432.
I think it would be really helpful if someone could come up with a concrete, real-world example if possible without exposing anything private.
assuming this does not lead to any possible race conditions |
I will try to give a little more info about my use case. struct SnapshotWithUpdates<T, S>
where
S: Stream<Item = (String, T)>,
{
snapshot: HashMap<String, T>,
updates: S,
}
fn snapshot_with_updates<T>(
receivers: HashMap<String, Receiver<T>>,
) -> SnapshotWithUpdates<T, impl Stream<Item = (String, T)>> {
...
}
So in order to do this I want to take the current value from every |
Thanks for the example, definitely helpful. I would probably favor simplifying it like this (does not compile): fn snapshot_with_updates<T>(
receivers: HashMap<String, Receiver<T>>,
) -> (HashMap<String, T>, impl Stream<Item = (String, T)>) {
// ...
} I wonder if we can use anything such as map or merge to help make this more idiomatic? I would also appreciate it if you can take a look at what I proposed in PR #5432 and comment if it really helps or if you can think of anything else we should do? |
The Not sure what you mean about map/merge, to me this sort of operation (the example) seems quite use-case specific 🤔 |
I realised after all this... that it's not quite the behaviour I needed 😅 What we have now is a I found a much simpler way to do this: WatchStream::new(rx).skip(1) Tada! I'm sure someone will find this feature useful though! Thanks for all your work! |
Is your feature request related to a problem? Please describe.
When using
tokio_stream::wrappers::WatchStream
, it always yield the current value even if unchanged:I have a use case where I would like to (synchronously) borrow the current value from the
Receiver
, then construct aWatchStream
of subsequent changes.Describe the solution you'd like
Add a new constructor variant which only yields once a change is observed.
For instance:
Describe alternatives you've considered
Another possibility is to create a
WatchStream
and then callnext()
and await theFuture
. However, in my use case this adds complexity because I want to fetch the initial value synchronously.The text was updated successfully, but these errors were encountered: