async-refresh
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.
use Infallible;
use Refreshed;
use Duration;
async
The above code will produce output the looks like:
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
Buildervalue usingbuilder() - Provide parameters on what to do when updating
- Use the
buildortry_buildmethods to produce a newRefreshedvalue that will regularly be called asynchronously - Use the
getmethod on theRefreshedto get the current value inside anArc
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.