Function relm4::spawn_future
source · [−]Expand description
Spawns a future on the main thread in the main event loop.
Panics
This function itself doesn’t panic but it might panic if you run futures that
expect the tokio runtime. Use the tokio-rt feature and an AsyncComponent
for this instead.
Examples found in repository
relm4-examples/examples/future.rs (line 50)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
fn update(&mut self, msg: AppMsg, _components: &(), sender: Sender<AppMsg>) -> bool {
self.reset();
match msg {
AppMsg::Request(entry) if !self.waiting => {
self.set_waiting(true);
let client = surf::client().with(surf::middleware::Redirect::new(10));
let fut = async move {
let text = if surf::Url::parse(&entry).is_ok() {
if let Ok(mut req) = client.send(surf::get(entry)).await {
if let Ok(resp) = req.body_string().await {
resp
} else {
"Couldn't get response body".to_string()
}
} else {
"Couldn't send request".to_string()
}
} else {
"Couldn't parse entry".to_string()
};
sender.send(AppMsg::Response(text)).unwrap();
};
spawn_future(fut);
}
AppMsg::Response(text) => {
self.set_text(text);
self.set_waiting(false);
}
_ => { /* Do nothing while waiting for a response */ }
}
true
}