Skip to content

Support feature detecting if an API is available #398

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
fitzgen opened this issue Jul 5, 2018 · 1 comment
Open

Support feature detecting if an API is available #398

fitzgen opened this issue Jul 5, 2018 · 1 comment

Comments

@fitzgen
Copy link
Member

fitzgen commented Jul 5, 2018

To get cross-browser functionality working, JS code often does things like

if (typeof fetch === "function") {
  // assume `fetch` API exists
} else {
  // fall back to XMLHTTPRequest
}

We should support this kind of feature detection somehow.


Strawman: add a #[wasm_bindgen(feature_detect = function_name)]-style attribute:

  • function_name would be the name of a function with signature fn() -> bool that returns true if the thing was found in JS and false otherwise

  • If the imported thing was not found in JS, we bind to a stub that throws + logs a JS error.

  • enables usage like this:

#[wasm_bindgen]
extern {
    #[wasm_bindgen(feature_detect = is_fetch_available)]
    fn fetch(...) -> ...;
}

if is_fetch_available() {
    fetch(...);
} else {
    // do some fall back...
}

Perhaps this requires an RFC.

@alexcrichton
Copy link
Contributor

This got me thinking! I managed to get rust-random/rand#541 working with what we've got today on crates.io!

I'm not entirely sure how much we want to bake in, from what I've seen everyone does feature detection slightly differently so we may want to just be sure to enable various patterns rather than trying to bless one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants