Skip to content

Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets#7017

Closed
philikon wants to merge 1 commit intofacebook:masterfrom
philikon:xhr_upload
Closed

Make XMLHttpRequest and XMLHttpRequest.upload proper EventTargets#7017
philikon wants to merge 1 commit intofacebook:masterfrom
philikon:xhr_upload

Conversation

@philikon
Copy link
Contributor

So far, XHR only supports a few onfoo event handlers, not the entier EventTarget interface (addEventListener, removeEventListener). It also doesn't support the upload object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.

Fortunately, adding proper EventTarget support is very easy thanks to event-target-shim. We already use it in our WebSocket implementation. It transparently handles the addEventListener('foo', ...) as well as onfoo APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that event.target is set properly. Basically, it's a super easy way to make us conform to the spec.

Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.

Test Plan: Ran through the XHR Example in UIExplorer on both platforms. Made sure upload progress events still work on iOS and timeouts work.

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @sreesharp, @nicklockwood and @grgmo to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Apr 17, 2016
@mkonicek
Copy link
Contributor

Is this one good to go? Should I merge it?

@sreesharp
Copy link
Contributor

Code looks good to me. Hope Flow is not complaining about the XHRBase static attributes.

@philikon
Copy link
Contributor Author

Flow didn't complain. Should be good to go.

@grgmo
Copy link
Contributor

grgmo commented Apr 20, 2016

looks good to me, good to merge

@mkonicek
Copy link
Contributor

@facebook-github-bot shipit

@facebook-github-bot facebook-github-bot added GH Review: accepted Import Started This pull request has been imported. This does not imply the PR has been approved. and removed GH Review: review-needed labels Apr 20, 2016
@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to Phabricator to review.

@mkonicek
Copy link
Contributor

mkonicek commented Apr 20, 2016

@philikon Looks like this breaks fetching QEs in one of our internal apps. Maybe @andreicoman11 knows what's wrong?

@philikon
Copy link
Contributor Author

Looks like this breaks fetching QEs in one of our internal apps.

Weird. In an end-to-end test or in a unit test? If it's a unit test, I could imagine that the unit test uses some internal knowledge of XMLHttpRequestBase to stub out some stuff.

Anyway, I'd love to help in whatever way I can to fix that. You guys know where to find me :)

@mkonicek
Copy link
Contributor

Philipp I've just sent you a message :)

@facebook-github-bot facebook-github-bot added Import Failed and removed Import Started This pull request has been imported. This does not imply the PR has been approved. labels Apr 21, 2016
@philikon
Copy link
Contributor Author

I've just looked over the diff again. There's very little behavioral change to XHR, so I'm surprised there's a new failure.

The biggest change is obviously concerning the EventTarget API. So one possible failure scenario could be something this:

  • existing code does something like xhr.addEventListener && xhr.addEventListener('timeout', timeoutHandler) and sets a timeout (or is on a platform with a default timeout)
  • in the test, the request actually times out
  • the consumer would never have found out before this patch, but now it does, causing the test to fail

The other change is that event handlers now get an event object rather than null. Not that they should care, but who knows...

Wish I could be more helpful. If it's too difficult to track down the problem on your end, @mkonicek, we can also try to take this diff in steps and see which change creates the failure. Would be tedious, but perhaps in the end quicker? Your call.

@TanTan-TT
Copy link

@philikon I have a question after reading RN Android code. RN Android networking code did not handle upload progress event, so your change can not make uploading progress works. Am I right?

@philikon
Copy link
Contributor Author

@philikon I have a question after reading RN Android code. RN Android networking code did not handle upload progress event, so your change can not make uploading progress works. Am I right?

Correct, this doesn't change the supported functionality on either platform.

@mkonicek
Copy link
Contributor

@philikon I'll try installing fb4a in my emulator and running the running the test locally to repro the timeout.

@ghost
Copy link

ghost commented Apr 26, 2016

@philikon updated the pull request.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no-undef: 'EventListener' is not defined.

@ghost
Copy link

ghost commented Apr 26, 2016

@philikon updated the pull request.

@ghost
Copy link

ghost commented Apr 27, 2016

@philikon updated the pull request.

@philikon
Copy link
Contributor Author

(circleci failure unrelated)

@mkonicek
Copy link
Contributor

@facebook-github-bot shipit

@ghost ghost added the Import Started This pull request has been imported. This does not imply the PR has been approved. label Apr 27, 2016
@ghost
Copy link

ghost commented Apr 27, 2016

Thanks for importing. If you are an FB employee go to Phabricator to review.

@ghost ghost added Import Failed and removed Import Started This pull request has been imported. This does not imply the PR has been approved. labels Apr 28, 2016
@ghost
Copy link

ghost commented Apr 28, 2016

@philikon updated the pull request.

@philikon
Copy link
Contributor Author

philikon commented Apr 28, 2016

Rebased on top of D3229435 / d363b1f. Can somebody plz re-import kthxbai :)

@ghost ghost closed this in b5f14ea Apr 28, 2016
@mkonicek
Copy link
Contributor

#finallylanded 👍

@mkonicek
Copy link
Contributor

Thanks for the help Kevin!

ptmt pushed a commit to ptmt/react-native that referenced this pull request May 9, 2016
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.

Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.

Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.

**T
Closes facebook#7017

Reviewed By: fkgozali

Differential Revision: D3202021

Pulled By: martinbigio

fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
@philikon philikon deleted the xhr_upload branch June 8, 2016 00:29
zebulgar pushed a commit to nightingale/react-native that referenced this pull request Jun 18, 2016
Summary:
So far, XHR only supports a few `onfoo` event handlers, not the entier `EventTarget` interface (`addEventListener`, `removeEventListener`). It also doesn't support the `upload` object on Android -- for no good reason. Even if we don't send any events there yet, there's no reason we have to break consuming code that wants to register an event handler there. This PR rectifies all that.

Fortunately, adding proper `EventTarget` support is very easy thanks to `event-target-shim`. We already use it in our WebSocket implementation. It transparently handles the `addEventListener('foo', ...)` as well as `onfoo` APIs, so when you dispatch an event on the event target, the right handlers will be invoked. The event object is wrapped so that `event.target` is set properly. Basically, it's a super easy way to make us conform to the spec.

Also added a bit of polish here and there, using ES2015 class property goodness to consolidate a lot of Flow property definitions with the corresponding property initializers.

**T
Closes facebook#7017

Reviewed By: fkgozali

Differential Revision: D3202021

Pulled By: martinbigio

fb-gh-sync-id: 2b007682074356c75c774fab337672918b6c4355
fbshipit-source-id: 2b007682074356c75c774fab337672918b6c4355
@philikon philikon mentioned this pull request Nov 24, 2016
7 tasks
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants