Skip to content
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

fix: Break dependency cycle by not using serde-serialize #239

Merged
merged 1 commit into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion crates/console/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ documentation = "https://docs.rs/gloo-console/"
categories = ["api-bindings", "development-tools::profiling", "wasm"]

[dependencies]
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
wasm-bindgen = "0.2"
js-sys = "0.3"
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
[dependencies.web-sys]
version = "0.3"
features = [
Expand Down
2 changes: 1 addition & 1 deletion crates/console/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub mod __macro {
data: impl serde::Serialize,
columns: impl IntoIterator<Item = &'a str>,
) {
let data = JsValue::from_serde(&data).unwrap_throw();
let data = js_sys::JSON::parse(&serde_json::to_string(&data).unwrap_throw()).unwrap_throw();
let columns = columns.into_iter().map(JsValue::from_str).collect();

crate::externs::table_with_data_and_columns(data, columns);
Expand Down
2 changes: 1 addition & 1 deletion crates/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ futures = "0.3"
default = ["json", "websocket", "http"]

# Enables `.json()` on `Response`
json = ["wasm-bindgen/serde-serialize", "serde", "serde_json"]
json = ["serde", "serde_json"]
# Enables the WebSocket API
websocket = [
'web-sys/WebSocket',
Expand Down
13 changes: 10 additions & 3 deletions crates/net/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,16 @@ impl Response {
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub async fn json<T: DeserializeOwned>(&self) -> Result<T, Error> {
let promise = self.response.json().map_err(js_to_error)?;
let json = JsFuture::from(promise).await.map_err(js_to_error)?;

Ok(json.into_serde()?)
let json = JsFuture::from(promise)
.await
.map_err(js_to_error)
.and_then(|json| {
js_sys::JSON::stringify(&json)
.map(String::from)
.map_err(js_to_error)
})?;

Ok(serde_json::from_str(&json)?)
}

/// Reads the response as a String.
Expand Down
20 changes: 11 additions & 9 deletions crates/net/src/websocket/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,17 @@ impl WebSocket {
url: &str,
protocols: &[S],
) -> Result<Self, JsError> {
Self::setup(web_sys::WebSocket::new_with_str_sequence(
url,
&JsValue::from_serde(protocols).map_err(|err| {
js_sys::Error::new(&format!(
"Failed to convert protocols to Javascript value: {}",
err
))
})?,
))
let map_err = |err| {
js_sys::Error::new(&format!(
"Failed to convert protocols to Javascript value: {}",
err
))
};
let json = serde_json::to_string(protocols)
.map_err(|e| map_err(e.to_string()))
.map(|d| js_sys::JSON::parse(&d).unwrap_throw())?;

Self::setup(web_sys::WebSocket::new_with_str_sequence(url, &json))
}

fn setup(ws: Result<web_sys::WebSocket, JsValue>) -> Result<Self, JsError> {
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ homepage = "https://github.com/rustwasm/gloo"
categories = ["api-bindings", "storage", "wasm"]

[dependencies]
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
wasm-bindgen = "0.2"
serde = "1.0"
serde_json = "1.0"
thiserror = "1.0"
Expand Down