-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ontimeout and onerror handler when using XMLHttpReque…
…st for Android and iOS Summary:Currently React-Native does not have `ontimeout` and `onerror` handlers for [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest). This is an extension to [No timeout on XMLHttpRequest](#4648). With addition to two handlers, both Android and iOS can now handle `ontimeout` if request times out and `onerror` when there is general network error. **Test plan** Code has been tested on both Android and iOS with [Charles](https://www.charlesproxy.com/) by setting a breakpoint on the request which fires `ontimeout` when the request waits beyond `timeout` time and `onerror` when there is network error. **Usage** JavaScript - ``` var request = new XMLHttpRequest(); function onLoad() { console.log(request.status); }; function onTimeout() { console.log('Timeout'); }; function onError() { console.log('General network error'); }; request.onload = onLoad; request.ontimeout = onTimeout; request.onerr Closes #6841 Differential Revision: D3178859 Pulled By: lexs fb-gh-sync-id: 30674570653e92ab5f7e74bd925dd5640fc862b6 fbshipit-source-id: 30674570653e92ab5f7e74bd925dd5640fc862b6
- Loading branch information
grgmo
authored and
Facebook Github Bot 9
committed
Apr 15, 2016
1 parent
967dbd0
commit d09cd62
Showing
8 changed files
with
248 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* The examples provided by Facebook are for non-commercial testing and | ||
* evaluation purposes only. | ||
* | ||
* Facebook reserves all rights not expressly granted. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL | ||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
var React = require('react'); | ||
var ReactNative = require('react-native'); | ||
var { | ||
StyleSheet, | ||
Text, | ||
TouchableHighlight, | ||
View, | ||
} = ReactNative; | ||
|
||
class XHRExampleOnTimeOut extends React.Component { | ||
state: any; | ||
xhr: XMLHttpRequest; | ||
|
||
constructor(props: any) { | ||
super(props); | ||
this.state = { | ||
status: '', | ||
loading: false | ||
}; | ||
} | ||
|
||
loadTimeOutRequest() { | ||
this.xhr && this.xhr.abort(); | ||
|
||
var xhr = this.xhr || new XMLHttpRequest(); | ||
|
||
xhr.onerror = ()=> { | ||
console.log('Status ', xhr.status); | ||
console.log('Error ', xhr.responseText); | ||
}; | ||
|
||
xhr.ontimeout = () => { | ||
this.setState({ | ||
status: xhr.responseText, | ||
loading: false | ||
}); | ||
}; | ||
|
||
xhr.onload = () => { | ||
console.log('Status ', xhr.status); | ||
console.log('Response ', xhr.responseText); | ||
}; | ||
|
||
xhr.open('GET', 'https://httpbin.org/delay/5'); // request to take 5 seconds to load | ||
xhr.timeout = 2000; // request times out in 2 seconds | ||
xhr.send(); | ||
this.xhr = xhr; | ||
|
||
this.setState({loading: true}); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.xhr && this.xhr.abort(); | ||
} | ||
|
||
render() { | ||
var button = this.state.loading ? ( | ||
<View style={styles.wrapper}> | ||
<View style={styles.button}> | ||
<Text>Loading...</Text> | ||
</View> | ||
</View> | ||
) : ( | ||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={this.loadTimeOutRequest.bind(this)}> | ||
<View style={styles.button}> | ||
<Text>Make Time Out Request</Text> | ||
</View> | ||
</TouchableHighlight> | ||
); | ||
|
||
return ( | ||
<View> | ||
{button} | ||
<Text>{this.state.status}</Text> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
var styles = StyleSheet.create({ | ||
wrapper: { | ||
borderRadius: 5, | ||
marginBottom: 5, | ||
}, | ||
button: { | ||
backgroundColor: '#eeeeee', | ||
padding: 8, | ||
}, | ||
}); | ||
|
||
module.exports = XHRExampleOnTimeOut; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
jest | ||
.autoMockOff() | ||
.dontMock('XMLHttpRequestBase'); | ||
|
||
const XMLHttpRequestBase = require('XMLHttpRequestBase'); | ||
|
||
describe('XMLHttpRequestBase', function(){ | ||
var xhr; | ||
|
||
beforeEach(() => { | ||
xhr = new XMLHttpRequestBase(); | ||
xhr.ontimeout = jest.fn(); | ||
xhr.onerror = jest.fn(); | ||
xhr.onload = jest.fn(); | ||
xhr.didCreateRequest(1); | ||
}); | ||
|
||
afterEach(() => { | ||
xhr = null; | ||
}); | ||
|
||
it('should call ontimeout function when the request times out', function(){ | ||
xhr._didCompleteResponse(1, 'Timeout', true); | ||
|
||
expect(xhr.ontimeout).toBeCalledWith(null); | ||
expect(xhr.onerror).not.toBeCalled(); | ||
expect(xhr.onload).not.toBeCalled(); | ||
}); | ||
|
||
it('should call onerror function when the request times out', function(){ | ||
xhr._didCompleteResponse(1, 'Generic error'); | ||
|
||
expect(xhr.onerror).toBeCalledWith(null); | ||
expect(xhr.ontimeout).not.toBeCalled(); | ||
expect(xhr.onload).not.toBeCalled(); | ||
}); | ||
|
||
it('should call onload function when there is no error', function(){ | ||
xhr._didCompleteResponse(1, null); | ||
|
||
expect(xhr.onload).toBeCalledWith(null); | ||
expect(xhr.onerror).not.toBeCalled(); | ||
expect(xhr.ontimeout).not.toBeCalled(); | ||
}); | ||
|
||
}); |
Oops, something went wrong.