Skip to content

Commit

Permalink
book: Move to async-channel for reqwest demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofer-Julian committed Oct 22, 2023
1 parent 5f21f71 commit 2c37e84
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
26 changes: 13 additions & 13 deletions book/listings/main_event_loop/8/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use glib::{clone, MainContext, Priority};
use glib::{clone, MainContext};
use gtk::glib;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};
Expand Down Expand Up @@ -27,28 +27,28 @@ fn build_ui(app: &Application) {
.build();

// ANCHOR: callback
let (sender, receiver) = MainContext::channel(Priority::default());
let (sender, receiver) = async_channel::bounded(1);
// Connect to "clicked" signal of `button`
button.connect_clicked(move |_| {
let main_context = MainContext::default();
// The main loop executes the asynchronous block
main_context.spawn_local(clone!(@strong sender => async move {
let response = reqwest::get("https://www.gtk-rs.org").await;
sender
.send(response)
.expect("Could not send through channel");
sender.send(response).await.expect("The channel needs to be open.");
}));
});

// The main loop executes the closure as soon as it receives the message
receiver.attach(None, move |response| {
if let Ok(response) = response {
println!("Status {}", response.status());
} else {
println!("Could not make a `GET` request.");
let main_context = MainContext::default();
// The main loop executes the asynchronous block
main_context.spawn_local(clone!(@weak button => async move {
while let Ok(response) = receiver.recv().await {
if let Ok(response) = response {
println!("Status {}", response.status());
} else {
println!("Could not make a `GET` request.");
}
}
glib::ControlFlow::Continue
});
}));
// ANCHOR_END: callback

// Create a window
Expand Down
26 changes: 13 additions & 13 deletions book/listings/main_event_loop/9/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use glib::{clone, MainContext, Priority};
use glib::{clone, MainContext};
use gtk::glib;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};
Expand Down Expand Up @@ -33,26 +33,26 @@ fn build_ui(app: &Application) {
.build();

// ANCHOR: callback
let (sender, receiver) = MainContext::channel(Priority::default());
let (sender, receiver) = async_channel::bounded(1);
// Connect to "clicked" signal of `button`
button.connect_clicked(move |_| {
RUNTIME.spawn(clone!(@strong sender => async move {
let response = reqwest::get("https://www.gtk-rs.org").await;
sender
.send(response)
.expect("Could not send through channel");
sender.send(response).await.expect("The channel needs to be open.");
}));
});

// The main loop executes the closure as soon as it receives the message
receiver.attach(None, move |response| {
if let Ok(response) = response {
println!("Status {}", response.status());
} else {
println!("Could not make a `GET` request.");
let main_context = MainContext::default();
// The main loop executes the asynchronous block
main_context.spawn_local(clone!(@weak button => async move {
while let Ok(response) = receiver.recv().await {
if let Ok(response) = response {
println!("Status {}", response.status());
} else {
println!("Could not make a `GET` request.");
}
}
glib::ControlFlow::Continue
});
}));
// ANCHOR_END: callback

// Create a window
Expand Down

0 comments on commit 2c37e84

Please sign in to comment.