embassy
embassy copied to clipboard
Add `Watch` sync primitive (similar to `tokio::sync::watch`)
This PR adds something similar to tokio watch. It is a single-slot channel similar to Signal, but it can have multiple receivers, all of which get to have a clone of the value. So also similar to a PubSubChannel, but with a single-slot queue. Primary methods of the receivers are:
changed: Await changes to the value, marking it as seen.peek: Await the value without marking it as seenget: Await the value while marking it as seen
All of the above also have try_xxx methods.
The doc comment and tests contain examples.
I am considering removing the peek methods for the receivers. Their use case is dubious, and not marking a message as seen can make stuff hard to reason about for the user. I have also reintroduce predicate methods, which allows the user to await (or try) a change which also meets a certain criteria, defined by a closure Fn(&T) -> bool, such that the value is not needlessly cloned to do such a comparison.