{"id":16340498,"url":"https://github.com/snoyberg/async-refresh-rs","last_synced_at":"2025-10-26T00:30:22.072Z","repository":{"id":62438256,"uuid":"385910636","full_name":"snoyberg/async-refresh-rs","owner":"snoyberg","description":"Refresh a value asynchronously after a given sleep period","archived":false,"fork":false,"pushed_at":"2021-09-02T02:34:08.000Z","size":12,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-31T12:48:58.610Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/snoyberg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-14T11:07:21.000Z","updated_at":"2023-06-03T16:45:23.000Z","dependencies_parsed_at":"2022-11-01T22:01:22.999Z","dependency_job_id":null,"html_url":"https://github.com/snoyberg/async-refresh-rs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snoyberg%2Fasync-refresh-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snoyberg%2Fasync-refresh-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snoyberg%2Fasync-refresh-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/snoyberg%2Fasync-refresh-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/snoyberg","download_url":"https://codeload.github.com/snoyberg/async-refresh-rs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238229940,"owners_count":19437723,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T23:57:01.567Z","updated_at":"2025-10-26T00:30:21.692Z","avatar_url":"https://github.com/snoyberg.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# async-refresh\n\n[![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)\n\nCreate 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.\n\n```rust\nuse std::convert::Infallible;\n\nuse async_refresh::Refreshed;\nuse tokio::time::Duration;\n\n#[tokio::main]\nasync fn main() {\n    let refreshed_time: Refreshed\u003cString, Infallible\u003e = Refreshed::builder()\n        .duration(Duration::from_millis(100))\n        .error(|err| {\n            eprintln!(\"Error while updating time: {:?}\", err);\n        })\n        .success(|new_val| {\n            eprintln!(\"Got a new time: {}\", new_val);\n        })\n        .exit(|| {\n            eprintln!(\"No longer refreshing\");\n        })\n        .build(|is_refresh| async move {\n            let now = std::time::SystemTime::now();\n            format!(\"now == {:?}, is_refresh == {}\", now, is_refresh)\n        })\n        .await;\n    println!(\n        \"Created a new Refreshed, value is: {}\",\n        refreshed_time.get()\n    );\n    tokio::time::sleep(Duration::from_secs(1)).await;\n    println!(\n        \"Dropping the Refreshed value, current time: {}\",\n        refreshed_time.get()\n    );\n    std::mem::drop(refreshed_time);\n    tokio::time::sleep(Duration::from_secs(1)).await;\n}\n```\n\nThe above code will produce output the looks like:\n\n```ignore\nCreated a new Refreshed, value is: now == SystemTime { intervals: 132750230547424477 }, is_refresh == false\nGot a new time: now == SystemTime { intervals: 132750230548504204 }, is_refresh == true\nGot a new time: now == SystemTime { intervals: 132750230549594570 }, is_refresh == true\nGot a new time: now == SystemTime { intervals: 132750230550668472 }, is_refresh == true\nGot a new time: now == SystemTime { intervals: 132750230551768836 }, is_refresh == true\nGot a new time: now == SystemTime { intervals: 132750230552849966 }, is_refresh == true\nGot a new time: now == SystemTime { intervals: 132750230553943810 }, is_refresh == true\nGot a new time: now == SystemTime { intervals: 132750230555062564 }, is_refresh == true\nGot a new time: now == SystemTime { intervals: 132750230556144744 }, is_refresh == true\nGot a new time: now == SystemTime { intervals: 132750230557223529 }, is_refresh == true\nDropping the Refreshed value, current time: now == SystemTime { intervals: 132750230557223529 }, is_refresh == true\nNo longer refreshing\n```\n\nThe basic workflow with this crate is:\n\n* Create a new `Builder` value using `builder()`\n* Provide parameters on what to do when updating\n* Use the `build` or `try_build` methods to produce a new `Refreshed` value that will regularly be called asynchronously\n* Use the `get` method on the `Refreshed` to get the current value inside an `Arc`\n\nInternally, `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.\n\nNote 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnoyberg%2Fasync-refresh-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsnoyberg%2Fasync-refresh-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsnoyberg%2Fasync-refresh-rs/lists"}