Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement time::repeat and simplify Subscription::run_with #2747

Merged
merged 2 commits into from
Jan 24, 2025

Conversation

hecrj
Copy link
Member

@hecrj hecrj commented Jan 24, 2025

Let's say you have an async function to obtain the current weather:

async fn fetch_weather() -> Result<Weather, Error> {
    // ...
}

The new time::repeat function can be leveraged to turn this async function into a Subscription that runs it at a set interval:

use iced::time;

enum Message {
    WeatherFetched(Result<Weather, Error>),
}

fn subscription(state: &State) -> Subscription<Message> {
    time::repeat(fetch_weather, time::Duration::from_secs(5))
        .map(Message::WeatherFetched)
}

@hecrj hecrj added feature New feature or request addition labels Jan 24, 2025
@hecrj hecrj added this to the 0.14 milestone Jan 24, 2025
@hecrj hecrj force-pushed the time-repeat-subscription branch from 38003c8 to 3a07c63 Compare January 24, 2025 15:45
@hecrj hecrj enabled auto-merge January 24, 2025 17:48
@hecrj hecrj merged commit 5eedf57 into master Jan 24, 2025
30 checks passed
@hecrj hecrj deleted the time-repeat-subscription branch January 24, 2025 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant