mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-27 22:54:46 +08:00
Send HEADERS_RECEIVED and LOADING events on Android
Summary: Send part of the response body every 100 ms if the client has set onreadystatechange. This is done by using the same events as the iOS code and removing the callback that Android previously used. Reconsolidate iOS and Android implementations. Closes #3772 public Reviewed By: mkonicek Differential Revision: D2647005 fb-gh-sync-id: d006e566867fa47d5f8dff71219cb390bcb8e15a
This commit is contained in:
committed by
facebook-github-bot-3
parent
a99fabdfea
commit
4d4c48d32b
@@ -12,95 +12,18 @@
|
||||
'use strict';
|
||||
|
||||
var FormData = require('FormData');
|
||||
var RCTNetworking = require('NativeModules').Networking;
|
||||
var RCTNetworking = require('RCTNetworking');
|
||||
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
||||
|
||||
var XMLHttpRequestBase = require('XMLHttpRequestBase');
|
||||
|
||||
class XMLHttpRequest extends XMLHttpRequestBase {
|
||||
|
||||
_requestId: ?number;
|
||||
_subscriptions: [any];
|
||||
upload: {
|
||||
onprogress?: (event: Object) => void;
|
||||
};
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._requestId = null;
|
||||
this._subscriptions = [];
|
||||
// iOS supports upload
|
||||
this.upload = {};
|
||||
}
|
||||
|
||||
_didCreateRequest(requestId: number): void {
|
||||
this._requestId = requestId;
|
||||
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
||||
'didSendNetworkData',
|
||||
(args) => this._didUploadProgress.call(this, args[0], args[1], args[2])
|
||||
));
|
||||
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
||||
'didReceiveNetworkResponse',
|
||||
(args) => this._didReceiveResponse.call(this, args[0], args[1], args[2])
|
||||
));
|
||||
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
||||
'didReceiveNetworkData',
|
||||
(args) => this._didReceiveData.call(this, args[0], args[1])
|
||||
));
|
||||
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
||||
'didCompleteNetworkResponse',
|
||||
(args) => this._didCompleteResponse.call(this, args[0], args[1])
|
||||
));
|
||||
}
|
||||
|
||||
_didUploadProgress(requestId: number, progress: number, total: number): void {
|
||||
if (requestId === this._requestId && this.upload.onprogress) {
|
||||
var event = {
|
||||
lengthComputable: true,
|
||||
loaded: progress,
|
||||
total,
|
||||
};
|
||||
this.upload.onprogress(event);
|
||||
}
|
||||
}
|
||||
|
||||
_didReceiveResponse(requestId: number, status: number, responseHeaders: ?Object): void {
|
||||
if (requestId === this._requestId) {
|
||||
this.status = status;
|
||||
this.setResponseHeaders(responseHeaders);
|
||||
this.setReadyState(this.HEADERS_RECEIVED);
|
||||
}
|
||||
}
|
||||
|
||||
_didReceiveData(requestId: number, responseText: string): void {
|
||||
if (requestId === this._requestId) {
|
||||
if (!this.responseText) {
|
||||
this.responseText = responseText;
|
||||
} else {
|
||||
this.responseText += responseText;
|
||||
}
|
||||
this.setReadyState(this.LOADING);
|
||||
}
|
||||
}
|
||||
|
||||
_didCompleteResponse(requestId: number, error: string): void {
|
||||
if (requestId === this._requestId) {
|
||||
if (error) {
|
||||
this.responseText = error;
|
||||
}
|
||||
this._clearSubscriptions();
|
||||
this._requestId = null;
|
||||
this.setReadyState(this.DONE);
|
||||
}
|
||||
}
|
||||
|
||||
_clearSubscriptions(): void {
|
||||
for (var i = 0; i < this._subscriptions.length; i++) {
|
||||
var sub = this._subscriptions[i];
|
||||
sub.remove();
|
||||
}
|
||||
this._subscriptions = [];
|
||||
}
|
||||
|
||||
sendImpl(method: ?string, url: ?string, headers: Object, data: any): void {
|
||||
if (typeof data === 'string') {
|
||||
data = {string: data};
|
||||
@@ -115,17 +38,9 @@ class XMLHttpRequest extends XMLHttpRequestBase {
|
||||
headers,
|
||||
incrementalUpdates: this.onreadystatechange ? true : false,
|
||||
},
|
||||
this._didCreateRequest.bind(this)
|
||||
this.didCreateRequest.bind(this)
|
||||
);
|
||||
}
|
||||
|
||||
abortImpl(): void {
|
||||
if (this._requestId) {
|
||||
RCTNetworking.cancelRequest(this._requestId);
|
||||
this._clearSubscriptions();
|
||||
this._requestId = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = XMLHttpRequest;
|
||||
|
||||
Reference in New Issue
Block a user