embassy icon indicating copy to clipboard operation
embassy copied to clipboard

Add `Watch` sync primitive (similar to `tokio::sync::watch`)

Open peterkrull opened this issue 1 year ago • 1 comments

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 seen
  • get: Await the value while marking it as seen

All of the above also have try_xxx methods.

The doc comment and tests contain examples.

peterkrull avatar Feb 13 '24 14:02 peterkrull

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.

peterkrull avatar Mar 01 '24 23:03 peterkrull