Skip to content

Commit

Permalink
feat!: make callback async
Browse files Browse the repository at this point in the history
BREAKING CHANGE: type of callback changed
  • Loading branch information
c-git committed Jul 16, 2024
1 parent 3517fa1 commit 2061141
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/loop_yield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn common_code() -> Result<(), Box<dyn std::error::Error>> {
let (tx, rx) = futures::channel::oneshot::channel();
fetch(
request,
move |result: Result<reqwest::Response, reqwest::Error>| {
move |result: Result<reqwest::Response, reqwest::Error>| async {
tx.send(result.expect("Expecting Response not Error").status())
.expect("Receiver should still be available");
},
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn common_code() -> Result<(), Box<dyn std::error::Error>> {

fetch(
request,
move |result: Result<reqwest::Response, reqwest::Error>| {
move |result: Result<reqwest::Response, reqwest::Error>| async {
tx.send(result.expect("Expecting Response not Error").status())
.expect("Receiver should still be available");
},
Expand Down
24 changes: 20 additions & 4 deletions src/wrappers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Stores the wrapper functions that can be called from either native or wasm
//! code
#[cfg(not(target_arch = "wasm32"))]
/// Performs a HTTP requests and calls the given callback when done. NB: Needs
/// to use a callback to prevent blocking on the thread that initiates the
/// fetch. Note: Instead of calling get like in the example you can use post,
Expand All @@ -19,7 +20,7 @@
/// let request = client.get("http://httpbin.org/get");
/// let (tx, rx) = futures::channel::oneshot::channel();
///
/// fetch(request, move |result: Result<reqwest::Response, reqwest::Error>| {
/// fetch(request, move |result: Result<reqwest::Response, reqwest::Error>| async {
/// tx.send(result.expect("Expecting Response not Error").status())
/// .expect("Receiver should still be available");
/// });
Expand All @@ -32,13 +33,28 @@
/// # #[cfg(target_arch = "wasm32")]
/// # fn main(){}
/// ```
pub fn fetch<F>(request: reqwest::RequestBuilder, on_done: F)
pub fn fetch<F, O>(request: reqwest::RequestBuilder, on_done: F)
where
F: 'static + Send + FnOnce(reqwest::Result<reqwest::Response>) -> O,
O: futures::Future<Output = ()> + Send,
{
let future = async move {
let result = request.send().await;
on_done(result).await;
};
spawn(future);
}

/// dox
#[cfg(target_arch = "wasm32")]
pub fn fetch<F, O>(request: reqwest::RequestBuilder, on_done: F)
where
F: 'static + Send + FnOnce(reqwest::Result<reqwest::Response>),
F: 'static + FnOnce(reqwest::Result<reqwest::Response>) -> O,
O: futures::Future<Output = ()>,
{
let future = async move {
let result = request.send().await;
on_done(result)
on_done(result).await;
};
spawn(future);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {

fetch(
request,
move |result: Result<reqwest::Response, reqwest::Error>| {
move |result: Result<reqwest::Response, reqwest::Error>| async {
tx.send(result.expect("Expecting Response not Error").status())
.expect("Receiver should still be available");
},
Expand Down

0 comments on commit 2061141

Please sign in to comment.