2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* 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.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
|
|
|
* @providesModule XMLHttpRequest
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-06-09 19:25:24 +00:00
|
|
|
var FormData = require('FormData');
|
2015-06-18 16:41:38 +00:00
|
|
|
var RCTNetworking = require('NativeModules').Networking;
|
2015-06-05 22:23:30 +00:00
|
|
|
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
var XMLHttpRequestBase = require('XMLHttpRequestBase');
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
class XMLHttpRequest extends XMLHttpRequestBase {
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2015-06-05 22:23:30 +00:00
|
|
|
_requestId: ?number;
|
|
|
|
_subscriptions: [any];
|
2015-07-21 05:44:51 +00:00
|
|
|
upload: {
|
|
|
|
onprogress?: (event: Object) => void;
|
|
|
|
};
|
2015-06-05 22:23:30 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this._requestId = null;
|
|
|
|
this._subscriptions = [];
|
2015-07-21 05:44:51 +00:00
|
|
|
this.upload = {};
|
2015-06-05 22:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_didCreateRequest(requestId: number): void {
|
|
|
|
this._requestId = requestId;
|
2015-07-21 05:44:51 +00:00
|
|
|
this._subscriptions.push(RCTDeviceEventEmitter.addListener(
|
2015-07-23 10:55:12 +00:00
|
|
|
'didSendNetworkData',
|
2015-07-21 05:44:51 +00:00
|
|
|
(args) => this._didUploadProgress.call(this, args[0], args[1], args[2])
|
|
|
|
));
|
2015-06-05 22:23:30 +00:00
|
|
|
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',
|
2015-06-06 01:47:32 +00:00
|
|
|
(args) => this._didCompleteResponse.call(this, args[0], args[1])
|
2015-06-05 22:23:30 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-07-21 05:44:51 +00:00
|
|
|
_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:23:30 +00:00
|
|
|
_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) {
|
2015-06-06 23:44:58 +00:00
|
|
|
if (error) {
|
|
|
|
this.responseText = error;
|
|
|
|
}
|
2015-06-05 22:23:30 +00:00
|
|
|
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 = [];
|
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
sendImpl(method: ?string, url: ?string, headers: Object, data: any): void {
|
2015-06-09 19:25:24 +00:00
|
|
|
if (typeof data === 'string') {
|
2015-08-01 01:54:25 +00:00
|
|
|
data = {string: data};
|
2015-08-14 12:13:40 +00:00
|
|
|
} else if (data instanceof FormData) {
|
2015-08-01 01:54:25 +00:00
|
|
|
data = {formData: data.getParts()};
|
2015-06-09 19:25:24 +00:00
|
|
|
}
|
2015-06-18 16:41:38 +00:00
|
|
|
RCTNetworking.sendRequest(
|
2015-04-10 08:33:10 +00:00
|
|
|
{
|
2015-06-09 19:25:24 +00:00
|
|
|
method,
|
|
|
|
url,
|
2015-08-01 01:54:25 +00:00
|
|
|
data,
|
2015-06-09 19:25:24 +00:00
|
|
|
headers,
|
2015-06-09 19:25:33 +00:00
|
|
|
incrementalUpdates: this.onreadystatechange ? true : false,
|
2015-04-10 08:33:10 +00:00
|
|
|
},
|
2015-06-05 22:23:30 +00:00
|
|
|
this._didCreateRequest.bind(this)
|
2015-01-30 01:10:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-25 02:34:12 +00:00
|
|
|
abortImpl(): void {
|
2015-06-05 22:23:30 +00:00
|
|
|
if (this._requestId) {
|
2015-06-18 16:41:38 +00:00
|
|
|
RCTNetworking.cancelRequest(this._requestId);
|
2015-06-05 22:23:30 +00:00
|
|
|
this._clearSubscriptions();
|
|
|
|
this._requestId = null;
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = XMLHttpRequest;
|