mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-28 20:25:33 +08:00
revert D2631410
Differential Revision: D2655673 fb-gh-sync-id: 115247373767690e63a0d6ce812a578d26b47289
This commit is contained in:
committed by
facebook-github-bot-5
parent
e406dccaf9
commit
39ec693866
@@ -25,7 +25,7 @@ var generateRequestId = function() {
|
||||
*/
|
||||
class RCTNetworking {
|
||||
|
||||
static sendRequest(method, url, headers, data, useIncrementalUpdates) {
|
||||
static sendRequest(method, url, headers, data, callback) {
|
||||
var requestId = generateRequestId();
|
||||
RCTNetworkingNative.sendRequest(
|
||||
method,
|
||||
@@ -33,7 +33,7 @@ class RCTNetworking {
|
||||
requestId,
|
||||
headers,
|
||||
data,
|
||||
useIncrementalUpdates);
|
||||
callback);
|
||||
return requestId;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule RCTNetworking
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
module.exports = require('NativeModules').Networking;
|
||||
@@ -26,6 +26,14 @@ function convertHeadersMapToArray(headers: Object): Array<Header> {
|
||||
}
|
||||
|
||||
class XMLHttpRequest extends XMLHttpRequestBase {
|
||||
|
||||
_requestId: ?number;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._requestId = null;
|
||||
}
|
||||
|
||||
sendImpl(method: ?string, url: ?string, headers: Object, data: any): void {
|
||||
var body;
|
||||
if (typeof data === 'string') {
|
||||
@@ -41,15 +49,17 @@ class XMLHttpRequest extends XMLHttpRequestBase {
|
||||
body = data;
|
||||
}
|
||||
|
||||
var useIncrementalUpdates = this.onreadystatechange ? true : false;
|
||||
var requestId = RCTNetworking.sendRequest(
|
||||
this._requestId = RCTNetworking.sendRequest(
|
||||
method,
|
||||
url,
|
||||
convertHeadersMapToArray(headers),
|
||||
body,
|
||||
useIncrementalUpdates
|
||||
this.callback.bind(this)
|
||||
);
|
||||
this.didCreateRequest(requestId);
|
||||
}
|
||||
|
||||
abortImpl(): void {
|
||||
this._requestId && RCTNetworking.abortRequest(this._requestId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,18 +12,95 @@
|
||||
'use strict';
|
||||
|
||||
var FormData = require('FormData');
|
||||
var RCTNetworking = require('RCTNetworking');
|
||||
var RCTNetworking = require('NativeModules').Networking;
|
||||
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
||||
|
||||
var XMLHttpRequestBase = require('XMLHttpRequestBase');
|
||||
|
||||
class XMLHttpRequest extends XMLHttpRequestBase {
|
||||
|
||||
_requestId: ?number;
|
||||
_subscriptions: [any];
|
||||
upload: {
|
||||
onprogress?: (event: Object) => void;
|
||||
};
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
// iOS supports upload
|
||||
this._requestId = null;
|
||||
this._subscriptions = [];
|
||||
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};
|
||||
@@ -38,9 +115,17 @@ 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;
|
||||
|
||||
@@ -11,9 +11,6 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var RCTNetworking = require('RCTNetworking');
|
||||
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
||||
|
||||
/**
|
||||
* Shared base for platform-specific XMLHttpRequest implementations.
|
||||
*/
|
||||
@@ -33,13 +30,6 @@ class XMLHttpRequestBase {
|
||||
responseText: ?string;
|
||||
status: number;
|
||||
|
||||
upload: ?{
|
||||
onprogress?: (event: Object) => void;
|
||||
};
|
||||
|
||||
_requestId: ?number;
|
||||
_subscriptions: [any];
|
||||
|
||||
_method: ?string;
|
||||
_url: ?string;
|
||||
_headers: Object;
|
||||
@@ -70,81 +60,9 @@ class XMLHttpRequestBase {
|
||||
this.responseText = '';
|
||||
this.status = 0;
|
||||
|
||||
this._requestId = null;
|
||||
|
||||
this._headers = {};
|
||||
this._sent = false;
|
||||
this._lowerCaseResponseHeaders = {};
|
||||
|
||||
this._clearSubscriptions();
|
||||
}
|
||||
|
||||
didCreateRequest(requestId: number): void {
|
||||
this._requestId = requestId;
|
||||
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
||||
'didSendNetworkData',
|
||||
(args) => this._didUploadProgress.call(this, ...args)
|
||||
));
|
||||
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
||||
'didReceiveNetworkResponse',
|
||||
(args) => this._didReceiveResponse.call(this, ...args)
|
||||
));
|
||||
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
||||
'didReceiveNetworkData',
|
||||
(args) => this._didReceiveData.call(this, ...args)
|
||||
));
|
||||
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
||||
'didCompleteNetworkResponse',
|
||||
(args) => this._didCompleteResponse.call(this, ...args)
|
||||
));
|
||||
}
|
||||
|
||||
_didUploadProgress(requestId: number, progress: number, total: number): void {
|
||||
if (requestId === this._requestId && this.upload && 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 {
|
||||
(this._subscriptions || []).forEach(sub => {
|
||||
sub.remove();
|
||||
});
|
||||
this._subscriptions = [];
|
||||
}
|
||||
|
||||
getAllResponseHeaders(): ?string {
|
||||
@@ -190,6 +108,10 @@ class XMLHttpRequestBase {
|
||||
throw new Error('Subclass must define sendImpl method');
|
||||
}
|
||||
|
||||
abortImpl(): void {
|
||||
throw new Error('Subclass must define abortImpl method');
|
||||
}
|
||||
|
||||
send(data: any): void {
|
||||
if (this.readyState !== this.OPENED) {
|
||||
throw new Error('Request has not been opened');
|
||||
@@ -203,10 +125,7 @@ class XMLHttpRequestBase {
|
||||
|
||||
abort(): void {
|
||||
this._aborted = true;
|
||||
if (this._requestId) {
|
||||
console.log('calling abort', this._requestId);
|
||||
RCTNetworking.abortRequest(this._requestId);
|
||||
}
|
||||
this.abortImpl();
|
||||
// only call onreadystatechange if there is something to abort,
|
||||
// below logic is per spec
|
||||
if (!(this.readyState === this.UNSENT ||
|
||||
@@ -219,6 +138,16 @@ class XMLHttpRequestBase {
|
||||
this._reset();
|
||||
}
|
||||
|
||||
callback(status: number, responseHeaders: ?Object, responseText: string): void {
|
||||
if (this._aborted) {
|
||||
return;
|
||||
}
|
||||
this.status = status;
|
||||
this.setResponseHeaders(responseHeaders || {});
|
||||
this.responseText = responseText;
|
||||
this.setReadyState(this.DONE);
|
||||
}
|
||||
|
||||
setResponseHeaders(responseHeaders: ?Object): void {
|
||||
this.responseHeaders = responseHeaders || null;
|
||||
var headers = responseHeaders || {};
|
||||
|
||||
Reference in New Issue
Block a user