Watchable
watchable
implements an observable RwLock-like type that is compatible with
both multi-threaded and async code. Inspired by
tokio::sync::watch.
watchable
is an RwLock-like type that allows watching for value changes
using a Multi-Producer, Multi-Consumer approach 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, is 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.