Skip to content

Commit

Permalink
refactor(wasm): move wasm_bindgen support to js folder
Browse files Browse the repository at this point in the history
Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com>

refactor(wasm): add wasm mod

Signed-off-by: Brooks Townsend <brooksmtownsend@gmail.com>
  • Loading branch information
brooksmtownsend committed Oct 21, 2024
1 parent 646b1f8 commit 5184c74
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 61 deletions.
8 changes: 4 additions & 4 deletions src/wasm/body.rs → src/wasm/js/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
let js_req = web_sys::Request::new_with_str_and_init("", &init)
.expect("could not create JS request");
let text_promise = js_req.text().expect("could not get text promise");
let text = crate::wasm::promise::<JsValue>(text_promise)
let text = crate::wasm::js::promise::<JsValue>(text_promise)
.await
.expect("could not get request body as text");

Expand All @@ -247,7 +247,7 @@ mod tests {
let js_req = web_sys::Request::new_with_str_and_init("", &init)
.expect("could not create JS request");
let text_promise = js_req.text().expect("could not get text promise");
let text = crate::wasm::promise::<JsValue>(text_promise)
let text = crate::wasm::js::promise::<JsValue>(text_promise)
.await
.expect("could not get request body as text");

Expand All @@ -273,7 +273,7 @@ mod tests {
let array_buffer_promise = js_req
.array_buffer()
.expect("could not get array_buffer promise");
let array_buffer = crate::wasm::promise::<JsValue>(array_buffer_promise)
let array_buffer = crate::wasm::js::promise::<JsValue>(array_buffer_promise)
.await
.expect("could not get request body as array buffer");

Expand Down Expand Up @@ -301,7 +301,7 @@ mod tests {
let array_buffer_promise = js_req
.array_buffer()
.expect("could not get array_buffer promise");
let array_buffer = crate::wasm::promise::<JsValue>(array_buffer_promise)
let array_buffer = crate::wasm::js::promise::<JsValue>(array_buffer_promise)
.await
.expect("could not get request body as array buffer");

Expand Down
File renamed without changes.
53 changes: 53 additions & 0 deletions src/wasm/js/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use wasm_bindgen::JsCast;
use web_sys::{AbortController, AbortSignal};

mod body;
mod client;
/// TODO
#[cfg(feature = "multipart")]
pub mod multipart;
mod request;
mod response;

pub use self::body::Body;
pub use self::client::{Client, ClientBuilder};
pub use self::request::{Request, RequestBuilder};
pub use self::response::Response;

async fn promise<T>(promise: js_sys::Promise) -> Result<T, crate::error::BoxError>
where
T: JsCast,
{
use wasm_bindgen_futures::JsFuture;

let js_val = JsFuture::from(promise).await.map_err(crate::error::wasm)?;

js_val
.dyn_into::<T>()
.map_err(|_js_val| "promise resolved to unexpected type".into())
}

/// A guard that cancels a fetch request when dropped.
struct AbortGuard {
ctrl: AbortController,
}

impl AbortGuard {
fn new() -> crate::Result<Self> {
Ok(AbortGuard {
ctrl: AbortController::new()
.map_err(crate::error::wasm)
.map_err(crate::error::builder)?,
})
}

fn signal(&self) -> AbortSignal {
self.ctrl.signal()
}
}

impl Drop for AbortGuard {
fn drop(&mut self) {
self.ctrl.abort();
}
}
6 changes: 3 additions & 3 deletions src/wasm/multipart.rs → src/wasm/js/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ mod tests {

let form_data_promise = js_req.form_data().expect("could not get form_data promise");

let form_data = crate::wasm::promise::<FormData>(form_data_promise)
let form_data = crate::wasm::js::promise::<FormData>(form_data_promise)
.await
.expect("could not get body as form data");

Expand All @@ -387,7 +387,7 @@ mod tests {
assert_eq!(text_file.type_(), text_file_type);

let text_promise = text_file.text();
let text = crate::wasm::promise::<JsValue>(text_promise)
let text = crate::wasm::js::promise::<JsValue>(text_promise)
.await
.expect("could not get text body as text");
assert_eq!(
Expand All @@ -408,7 +408,7 @@ mod tests {
assert_eq!(string, string_content);

let binary_array_buffer_promise = binary_file.array_buffer();
let array_buffer = crate::wasm::promise::<JsValue>(binary_array_buffer_promise)
let array_buffer = crate::wasm::js::promise::<JsValue>(binary_array_buffer_promise)
.await
.expect("could not get request body as array buffer");

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/wasm/response.rs → src/wasm/js/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use http::{HeaderMap, StatusCode};
use js_sys::Uint8Array;
use url::Url;

use crate::wasm::AbortGuard;
use crate::wasm::js::AbortGuard;

#[cfg(feature = "stream")]
use wasm_bindgen::JsCast;
Expand Down
55 changes: 2 additions & 53 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,2 @@
use wasm_bindgen::JsCast;
use web_sys::{AbortController, AbortSignal};

mod body;
mod client;
/// TODO
#[cfg(feature = "multipart")]
pub mod multipart;
mod request;
mod response;

pub use self::body::Body;
pub use self::client::{Client, ClientBuilder};
pub use self::request::{Request, RequestBuilder};
pub use self::response::Response;

async fn promise<T>(promise: js_sys::Promise) -> Result<T, crate::error::BoxError>
where
T: JsCast,
{
use wasm_bindgen_futures::JsFuture;

let js_val = JsFuture::from(promise).await.map_err(crate::error::wasm)?;

js_val
.dyn_into::<T>()
.map_err(|_js_val| "promise resolved to unexpected type".into())
}

/// A guard that cancels a fetch request when dropped.
struct AbortGuard {
ctrl: AbortController,
}

impl AbortGuard {
fn new() -> crate::Result<Self> {
Ok(AbortGuard {
ctrl: AbortController::new()
.map_err(crate::error::wasm)
.map_err(crate::error::builder)?,
})
}

fn signal(&self) -> AbortSignal {
self.ctrl.signal()
}
}

impl Drop for AbortGuard {
fn drop(&mut self) {
self.ctrl.abort();
}
}
pub mod js;
pub use js::*;

0 comments on commit 5184c74

Please sign in to comment.