Skip to content

Commit 7efcfe4

Browse files
authored
Use direct global binding for fetch in place of web_sys fetch bindings (#474)
This avoids the need to detect whether the module is running in the main browser thread or in a WebWorker. This matches how `setInterval` and related functions are handled by the gloo-timers crate. See rustwasm/wasm-bindgen#3863
1 parent dc0e61e commit 7efcfe4

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

Cargo.lock

+21-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/net/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ http = [
7878
'web-sys/Response',
7979
'web-sys/ResponseInit',
8080
'web-sys/ResponseType',
81-
'web-sys/Window',
8281
'web-sys/RequestCache',
8382
'web-sys/RequestCredentials',
8483
'web-sys/ObserverCallback',
@@ -88,7 +87,6 @@ http = [
8887
'web-sys/ReadableStream',
8988
'web-sys/Blob',
9089
'web-sys/FormData',
91-
'web-sys/WorkerGlobalScope',
9290
]
9391
# Enables the EventSource API
9492
eventsource = [

crates/net/src/http/request.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::http::{Headers, QueryParams, Response};
22
use crate::{js_to_error, Error};
33
use http::Method;
4-
use js_sys::{ArrayBuffer, Reflect, Uint8Array};
4+
use js_sys::{ArrayBuffer, Uint8Array};
55
use std::convert::{From, TryFrom, TryInto};
66
use std::fmt;
77
use std::str::FromStr;
8-
use wasm_bindgen::{JsCast, JsValue};
8+
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};
99
use wasm_bindgen_futures::JsFuture;
1010
use web_sys::{
1111
AbortSignal, FormData, ObserverCallback, ReadableStream, ReferrerPolicy, RequestCache,
@@ -16,6 +16,17 @@ use web_sys::{
1616
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
1717
use serde::de::DeserializeOwned;
1818

19+
#[wasm_bindgen]
20+
extern "C" {
21+
// Create a separate binding for `fetch` as a global, rather than using the
22+
// existing Window/WorkerGlobalScope bindings defined by web_sys, for
23+
// greater efficiency.
24+
//
25+
// https://github.com/rustwasm/wasm-bindgen/discussions/3863
26+
#[wasm_bindgen(js_name = "fetch")]
27+
fn fetch_with_request(request: &web_sys::Request) -> js_sys::Promise;
28+
}
29+
1930
/// A wrapper round `web_sys::Request`: an http request to be used with the `fetch` API.
2031
pub struct RequestBuilder {
2132
options: web_sys::RequestInit,
@@ -320,23 +331,7 @@ impl Request {
320331
/// Executes the request.
321332
pub async fn send(self) -> Result<Response, Error> {
322333
let request = self.0;
323-
let global = js_sys::global();
324-
let maybe_window =
325-
Reflect::get(&global, &JsValue::from_str("Window")).map_err(js_to_error)?;
326-
let promise = if !maybe_window.is_undefined() {
327-
let window = global.dyn_into::<web_sys::Window>().unwrap();
328-
window.fetch_with_request(&request)
329-
} else {
330-
let maybe_worker = Reflect::get(&global, &JsValue::from_str("WorkerGlobalScope"))
331-
.map_err(js_to_error)?;
332-
if !maybe_worker.is_undefined() {
333-
let worker = global.dyn_into::<web_sys::WorkerGlobalScope>().unwrap();
334-
worker.fetch_with_request(&request)
335-
} else {
336-
panic!("Unsupported JavaScript global context");
337-
}
338-
};
339-
334+
let promise = fetch_with_request(&request);
340335
let response = JsFuture::from(promise).await.map_err(js_to_error)?;
341336
response
342337
.dyn_into::<web_sys::Response>()

0 commit comments

Comments
 (0)