forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added incremental XMLHttpRequest updates
Summary: @public Previously, our XMLHttpRequest implementation would only update the readyState when the download was fully completed. This diff adds support for receiving incremental data updates as the download happens, which can be monitored by adding the onreadystatechange event handler. As a performance optimization, incremental data updates are only sent if the onreadystatechanged handler has been set in the JS, otherwise it just sends the whole data block once download is complete, as before. Test Plan: * Run the UIExplorer XMLHttpRequest example (in both OSS and Catalyst) to see incremental downloads working. * Run the Movies app to see regular (non-incremental) downloads in action * Run any network-based app in Catalyst shell to verify RKDataManager still works
- Loading branch information
1 parent
bb95400
commit e00b9ac
Showing
6 changed files
with
366 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/** | ||
* 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-native'); | ||
var { | ||
ProgressViewIOS, | ||
StyleSheet, | ||
View, | ||
Text, | ||
TouchableHighlight, | ||
} = React; | ||
|
||
class Downloader extends React.Component { | ||
|
||
xhr: XMLHttpRequest; | ||
cancelled: boolean; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.cancelled = false; | ||
this.state = { | ||
downloading: false, | ||
contentSize: 1, | ||
downloaded: 0, | ||
}; | ||
} | ||
|
||
download() { | ||
this.xhr && this.xhr.abort(); | ||
|
||
var xhr = this.xhr || new XMLHttpRequest(); | ||
xhr.onreadystatechange = () => { | ||
if (xhr.readyState === xhr.HEADERS_RECEIVED) { | ||
var contentSize = parseInt(xhr.getResponseHeader('Content-Length'), 10); | ||
this.setState({ | ||
contentSize: contentSize, | ||
downloaded: 0, | ||
}); | ||
} else if (xhr.readyState === xhr.LOADING) { | ||
this.setState({ | ||
downloaded: xhr.responseText.length, | ||
}); | ||
} else if (xhr.readyState === xhr.DONE) { | ||
this.setState({ | ||
downloading: false, | ||
}); | ||
if (this.cancelled) { | ||
this.cancelled = false; | ||
return; | ||
} | ||
if (xhr.status === 200) { | ||
alert('Download complete!'); | ||
} else if (xhr.status !== 0) { | ||
alert('Error: Server returned HTTP status of ' + xhr.status + ' ' + xhr.responseText); | ||
} else { | ||
alert('Error: ' + xhr.responseText); | ||
} | ||
} | ||
}; | ||
xhr.open('GET', 'http://www.gutenberg.org/cache/epub/100/pg100.txt'); | ||
xhr.send(); | ||
this.xhr = xhr; | ||
|
||
this.setState({downloading: true}); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.cancelled = true; | ||
this.xhr && this.xhr.abort(); | ||
} | ||
|
||
render() { | ||
var button = this.state.downloading ? ( | ||
<View style={styles.wrapper}> | ||
<View style={styles.button}> | ||
<Text>Downloading...</Text> | ||
</View> | ||
</View> | ||
) : ( | ||
<TouchableHighlight | ||
style={styles.wrapper} | ||
onPress={this.download.bind(this)}> | ||
<View style={styles.button}> | ||
<Text>Download 5MB Text File</Text> | ||
</View> | ||
</TouchableHighlight> | ||
); | ||
|
||
return ( | ||
<View> | ||
{button} | ||
<ProgressViewIOS progress={(this.state.downloaded / this.state.contentSize)}/> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
exports.framework = 'React'; | ||
exports.title = 'XMLHttpRequest'; | ||
exports.description = 'XMLHttpRequest'; | ||
exports.examples = [{ | ||
title: 'File Download', | ||
render() { | ||
return <Downloader/>; | ||
} | ||
}]; | ||
|
||
var styles = StyleSheet.create({ | ||
wrapper: { | ||
borderRadius: 5, | ||
marginBottom: 5, | ||
}, | ||
button: { | ||
backgroundColor: '#eeeeee', | ||
padding: 10, | ||
}, | ||
}); |
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
Oops, something went wrong.