-
Notifications
You must be signed in to change notification settings - Fork 147
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
[WIP] Add mid-level bindings for XmlHttpRequest #44
Conversation
dfa5158
to
88498c9
Compare
|
||
impl XmlHttpRequest { | ||
/// Initialize an XmlHttpRequest. | ||
pub fn new() -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we assume this is safe because all browsers that support webassembly implement XmlHttpRequest, or check whether the constructor is defined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an API predates WebAssembly, then yes we can safely assume it exists.
In addition, XMLHttpRequest goes all the way back to IE7! So we can safely assume it exists regardless.
Personally, I would just use unwrap_throw
, but I don't have strong feelings about it.
7872288
to
24eab87
Compare
Yes, let's definitely build on top of |
Just an update to say this PR isn't abandoned, I hope to find time to work on documenting and testing more soon. I am also waiting on gloo-events (I tried using it but gave up on panics in tests) to finish shaping the API. |
I think this is now ready for a bit more feedback. Here are a few doubts/questions I have regarding this implementation:
|
TODO: we can test abort events easily. |
It looks like the event handlers question is being discussed on #51, I'll read the conversation soon to adapt this. |
Seems good to me. Unlike somethign like
In these cases, I like to use
I don't have strong opinions, but this item in the API guidelines would suggest either two different methods or one method with a custom enum SendBody<B: XhrBody> {
None,
Body(B),
}
impl XmlHttpRequest {
pub fn send<B: XhrBody>(&self, body: SendBody<B>) { ... }
} We could also have the union of these methods: impl XmlHttpRequest {
pub fn send<B: XhrBody>(&self, body: SendBody<B>) { ... }
pub fn send_with_body<B: XhrBody>(&self, body: B) { ... }
pub fn send_without_body(&self) { ... }
} Again, I don't really have strong opinions either way.
Another option is to load some JS snippet to monkey patch the API before the wasm and its JS glue imports it, and then mock up the behavior as needed: // gloo/crates/xhr/tests/mock.js
window.XMLHttpRequest.prototype.open = function(...) { ... };
window.XMLHttpRequest.prototype.send = function(...) { ... };
export function setNextRequestStatus(code) {
// stash some state so that the next XHR we send will return the given HTTP status code..
} // gloo/crates/xhr/tests/web.rs
#[wasm_bindgen(module = "/tests/mock.js")]
extern "C" {
#[wasm_bindgen(js_name = setNextRequestStatus)]
fn set_next_request_status(code: u32);
} |
Thanks for all the advice - there are improvements to make to this PR, I'll try to find the time to work on it asap, it's a bit complicated now as I am moving :) |
) | ||
}) | ||
.collect() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense for this to be an http::HeaderMap
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, this needs to be case insensitive, I realized writing tests for it
/// `load`, `abort` and `error` event listeners. | ||
pub fn send_with_body<B: XhrBody>(&self, body: B) { | ||
body.send(&self.xhr) | ||
.expect_throw("Error sending request. Did you forget to call `open`?") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would've expected all the send_*
methods to return Result
instead of panicking. Could you expand on the reasoning here why panicking is preferred over Result
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went the shortest route for error handling because there was no clear prior art/guideline there, but I agree that in principle a Result
with a type error variant is ideal.
I'm noticing there's quite a large API surface here, and I feel I have quite a bit of feedback I might want to share. Similar to the File API (#76), would it perhaps make sense to create consensus on the API in an RFC first, before moving to an implementation PR? @tomhoule I understand proposing this might feel a bit frustrating, because you've put a lot of work in. But I think if we do this well we might be able to land the implementation faster because we're splitting the design stage from the implementation allowing us to focus on one set of challenges at the time. Also it might create a valuable resource to refer to when we inevitably want to tackle the Fetch / |
sure - there's a lot to figure out, especially around error handling. I took up the PR thinking of getting this merged first and then polishing up, but we can discuss first (as you may have noticed I am very short on open source time at the moment though, sorry in advance if I can't get very involved). Let's close this. Should the discussion continue on #37? |
This is intended to implement the feature proposed in #37
TODO before this can be merged:
It would also be nice to figure out a way to spin up a test http server in the background for browser tests, but I haven't found a straightforward way to do it without running a separate command so far.