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

Added support for connection close detection #1

Merged
merged 1 commit into from
Nov 15, 2016

Conversation

jonahbron
Copy link
Contributor

My application needs to detect when the request has closed, this adds support for that.

@mpetazzoni
Copy link
Owner

mpetazzoni commented Nov 14, 2016

Thanks for the contribution. Is that not already covered by the load, abort and error events though?

Maybe _onStreamLoaded() should set the state to CLOSED.

@jonahbron
Copy link
Contributor Author

jonahbron commented Nov 15, 2016

@mpetazzoni I added listeners to all of the events, and none of them seemed to trigger when the request closed normally. If you have a different idea of how you'd like that event triggered, I'd be happy to modify my PR.

@mpetazzoni
Copy link
Owner

Interesting. What browser are you testing with? It looks like IE versions older than IE10 don't support the new ProgressEvent interface:

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#Browser_compatibility

If that's the case, then we can certainly support the onreadystatechange event, but if so then we need proper support since it would be the only kind of event fired. The handler for it should thus correctly dispatch to the other handlers as needed based on the values of this.xhr.readyState and this.xhr.status.

@jonahbron
Copy link
Contributor Author

I tested with Chrome. Everything else works fine, and I did get other readystatechange events, just none for the close event.

@jonahbron
Copy link
Contributor Author

the CONNECTING and OPEN ready states trigger the event (lines 171 and 104, respectively), and CLOSED does trigger if the stream fails (line 93). This change only makes sure it's triggered if the connection is closed successfully.

@jonahbron
Copy link
Contributor Author

If you think it makes more sense, I could move the _setReadyState call to the end of _onStreamLoaded, so that CLOSED is triggered after the last chunk is loaded. But it made sense to me to base the CLOSED ready state on the XHR closed ready state.

@jonahbron
Copy link
Contributor Author

jonahbron commented Nov 15, 2016

And just to be clear, I'm not adding the readystatechange event to the library. It's already happening (line 85) for other ready state changes. I'm merely adding a trigger for a scenario that's not currently supported.

@mpetazzoni
Copy link
Owner

Makes sense. Thanks for the explanation. Looks good then!

@mpetazzoni mpetazzoni merged commit 2de6e0c into mpetazzoni:master Nov 15, 2016
@mpetazzoni mpetazzoni self-assigned this Nov 15, 2016
@jonahbron
Copy link
Contributor Author

Awesome, thanks!

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

Successfully merging this pull request may close these issues.

None yet

2 participants