Watchable
Enables watching for value changes in both multi-threaded and asynchronous contexts. Similar to tokio::sync::watch, but able to work in non-async codebases.
watchable is a Multi-Producer, Multi-Consumer channel where each consumer
is only guaranteed to receive the most recently written value.
use ;
When running this example, the output will look similar to:
As you can see, the receiving thread doesn't receive every value. Each watcher is guaranteed to be notified when changes occur and is guaranteed to be able to retrieve the most recent value.
Async Support
The Watcher type can be used in async code in multiple ways:
Watcher::into_stream(): Wraps the watcher in a type that implementsfutures::Stream.Watcher::wait_async().await: Pauses execution of the current task until a new value is available to be read.Watcher::read()can be used to retrieve the current value afterwait_async()has returned.
Here is the same example as above, except this time using Watcher::into_stream with Tokio:
use StreamExt;
use ;
async
async
watchable is compatible with all async runtimes.
Open-source Licenses
This project, like all projects from Khonsu Labs, are open-source. This repository is available under the MIT License or the Apache License 2.0.
To learn more about contributing, please see CONTRIBUTING.md.