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

Async actions #28

Closed
hecrj opened this issue Oct 23, 2019 · 0 comments · Fixed by #62
Closed

Async actions #28

hecrj opened this issue Oct 23, 2019 · 0 comments · Fixed by #62
Assignees
Labels
feature New feature or request help wanted Extra attention is needed
Milestone

Comments

@hecrj
Copy link
Member

hecrj commented Oct 23, 2019

Most applications need to perform work in the background, without freezing the UI while they work. The current architecture can be easily extended to achieve this.

We can let users return an asynchronous action (i.e. a future) producing a Message in Application::update. The runtime will spawn each returned action in a thread pool and, once it finishes, feed the produced message back to update. This is also how Elm does it.

When it comes to implementing this, winit already supports user-specific events that can be sent from another thread. Accommodating iced_winit for this functionality should not be too complicated.

Here is an example of how the end-user API could look:

impl Application for WeatherReport {
    fn update(&mut self, message: Message) -> Command<Message> {
        match message {
            Message::Refresh => {
                self.state = State::Loading;

                Command::from_future(
                    Weather::request(self.location),
                    Message::ReportReceived,
                )
            }
            Message::ReportReceived(Ok(report)) => {
                self.state = State::Show(report);

                Command::none()
            },
            Message::ReportReceived(Err(error)) => {
                self.state = State::Error(error);

                Command::none()
            }
        }
    }
}
@hecrj hecrj added the feature New feature or request label Oct 23, 2019
@hecrj hecrj added this to the 0.2.0 milestone Oct 23, 2019
@hecrj hecrj added the help wanted Extra attention is needed label Oct 23, 2019
@hecrj hecrj self-assigned this Nov 7, 2019
@hecrj hecrj modified the milestones: 0.2.0, 0.1.0 Nov 17, 2019
@hecrj hecrj closed this as completed in #62 Nov 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant