Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/snoyberg/async-refresh-rs
Refresh a value asynchronously after a given sleep period
https://github.com/snoyberg/async-refresh-rs
Last synced: 26 days ago
JSON representation
Refresh a value asynchronously after a given sleep period
- Host: GitHub
- URL: https://github.com/snoyberg/async-refresh-rs
- Owner: snoyberg
- Created: 2021-07-14T11:07:21.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-09-02T02:34:08.000Z (about 3 years ago)
- Last Synced: 2024-10-06T05:25:25.815Z (about 1 month ago)
- Language: Rust
- Size: 11.7 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# async-refresh
[![Rust](https://github.com/snoyberg/async-refresh-rs/actions/workflows/rust.yml/badge.svg)](https://github.com/snoyberg/async-refresh-rs/actions/workflows/rust.yml)
Create values that refresh automatically after a given duration. Refreshing happens asynchronously in a separate task. Values are available via an `Arc`. The refresh task will automatically exit when the value is no longer referenced by any other part of the program.
```rust
use std::convert::Infallible;use async_refresh::Refreshed;
use tokio::time::Duration;#[tokio::main]
async fn main() {
let refreshed_time: Refreshed = Refreshed::builder()
.duration(Duration::from_millis(100))
.error(|err| {
eprintln!("Error while updating time: {:?}", err);
})
.success(|new_val| {
eprintln!("Got a new time: {}", new_val);
})
.exit(|| {
eprintln!("No longer refreshing");
})
.build(|is_refresh| async move {
let now = std::time::SystemTime::now();
format!("now == {:?}, is_refresh == {}", now, is_refresh)
})
.await;
println!(
"Created a new Refreshed, value is: {}",
refreshed_time.get()
);
tokio::time::sleep(Duration::from_secs(1)).await;
println!(
"Dropping the Refreshed value, current time: {}",
refreshed_time.get()
);
std::mem::drop(refreshed_time);
tokio::time::sleep(Duration::from_secs(1)).await;
}
```The above code will produce output the looks like:
```ignore
Created a new Refreshed, value is: now == SystemTime { intervals: 132750230547424477 }, is_refresh == false
Got a new time: now == SystemTime { intervals: 132750230548504204 }, is_refresh == true
Got a new time: now == SystemTime { intervals: 132750230549594570 }, is_refresh == true
Got a new time: now == SystemTime { intervals: 132750230550668472 }, is_refresh == true
Got a new time: now == SystemTime { intervals: 132750230551768836 }, is_refresh == true
Got a new time: now == SystemTime { intervals: 132750230552849966 }, is_refresh == true
Got a new time: now == SystemTime { intervals: 132750230553943810 }, is_refresh == true
Got a new time: now == SystemTime { intervals: 132750230555062564 }, is_refresh == true
Got a new time: now == SystemTime { intervals: 132750230556144744 }, is_refresh == true
Got a new time: now == SystemTime { intervals: 132750230557223529 }, is_refresh == true
Dropping the Refreshed value, current time: now == SystemTime { intervals: 132750230557223529 }, is_refresh == true
No longer refreshing
```The basic workflow with this crate is:
* Create a new `Builder` value using `builder()`
* Provide parameters on what to do when updating
* Use the `build` or `try_build` methods to produce a new `Refreshed` value that will regularly be called asynchronously
* Use the `get` method on the `Refreshed` to get the current value inside an `Arc`Internally, `Refreshed` uses an `Arc`, so cloning is safe and cheap. When the last reference to that `Arc` is dropped, the refresh loop will automatically terminate.
Note that if your refresh function or future panics, your program will continue to function, but the value will no longer be updated. It is strongly recommended to use non-panicking actions.