Skip to content

Make Request and RequestBuilder implement IntoFuture #500

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 78 additions & 7 deletions crates/net/src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use http::Method;
use js_sys::{ArrayBuffer, Uint8Array};
use std::convert::{From, TryFrom, TryInto};
use std::fmt;
use std::future::{Future, IntoFuture};
use std::pin::Pin;
use std::str::FromStr;
use std::task::{Context, Poll};
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use web_sys::{
Expand Down Expand Up @@ -188,17 +191,62 @@ impl RequestBuilder {
self.options.signal(signal);
self
}

/// Builds the request and send it to the server, returning the received response.
pub async fn send(self) -> Result<Response, Error> {
let req: Request = self.try_into()?;
req.send().await
self.await
}
/// Builds the request.
pub fn build(self) -> Result<Request, crate::error::Error> {
self.try_into()
}
}

impl IntoFuture for RequestBuilder {
type Output = Result<Response, Error>;

type IntoFuture = RequestBuilderFuture;

fn into_future(self) -> Self::IntoFuture {
RequestBuilderFuture(match TryInto::<Request>::try_into(self) {
Ok(req) => RequestBuilderFutureInner::Request(req.into_future()),
Err(e) => RequestBuilderFutureInner::Immediate(Some(e)),
})
}
}

#[derive(Debug)]
enum RequestBuilderFutureInner {
Immediate(Option<Error>),
Request(RequestFuture),
}

impl Future for RequestBuilderFutureInner {
type Output = Result<Response, Error>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.get_mut() {
RequestBuilderFutureInner::Immediate(ref mut e) => {
Poll::Ready(Err(e.take().expect("Polled after Ready was returned")))
}
RequestBuilderFutureInner::Request(ref mut httpfuture) => {
Future::poll(Pin::new(httpfuture), cx)
}
}
}
}

#[derive(Debug)]
pub struct RequestBuilderFuture(RequestBuilderFutureInner);

impl Future for RequestBuilderFuture {
type Output = Result<Response, Error>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Future::poll(Pin::new(&mut self.0), cx)
}
}

impl TryFrom<RequestBuilder> for Request {
type Error = crate::error::Error;

Expand Down Expand Up @@ -330,13 +378,36 @@ impl Request {

/// Executes the request.
pub async fn send(self) -> Result<Response, Error> {
self.await
}
}

impl IntoFuture for Request {
type Output = Result<Response, Error>;
type IntoFuture = RequestFuture;

fn into_future(self) -> Self::IntoFuture {
let request = self.0;
let promise = fetch_with_request(&request);
let response = JsFuture::from(promise).await.map_err(js_to_error)?;
response
.dyn_into::<web_sys::Response>()
.map_err(|e| panic!("fetch returned {:?}, not `Response` - this is a bug", e))
.map(Response::from)
RequestFuture(JsFuture::from(promise))
}
}

#[derive(Debug)]
pub struct RequestFuture(JsFuture);

impl Future for RequestFuture {
type Output = Result<Response, Error>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
JsFuture::poll(Pin::new(&mut self.0), cx).map(|v| {
let response = v.map_err(js_to_error)?;

response
.dyn_into::<web_sys::Response>()
.map_err(|e| panic!("fetch returned {:?}, not `Response` - this is a bug", e))
.map(Response::from)
})
}
}

Expand Down
Loading