mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-07 02:08:34 +08:00
Update RCTNetworking, RCTNetInfo and RCTLocationManager to use new events system
Summary: Updated networking and geolocation to use the new events system. Reviewed By: bestander Differential Revision: D3346129 fbshipit-source-id: 957716e54d7af8c4a6783f684098e92e92f19654
This commit is contained in:
committed by
Facebook Github Bot 1
parent
62f53ffaa7
commit
b71db11554
@@ -12,39 +12,66 @@
|
||||
|
||||
// Do not require the native RCTNetworking module directly! Use this wrapper module instead.
|
||||
// It will add the necessary requestId, so that you don't have to generate it yourself.
|
||||
var RCTNetworkingNative = require('NativeModules').Networking;
|
||||
const FormData = require('FormData');
|
||||
const NativeEventEmitter = require('NativeEventEmitter');
|
||||
const RCTNetworkingNative = require('NativeModules').Networking;
|
||||
|
||||
var _requestId = 1;
|
||||
var generateRequestId = function() {
|
||||
type Header = [string, string];
|
||||
|
||||
function convertHeadersMapToArray(headers: Object): Array<Header> {
|
||||
const headerArray = [];
|
||||
for (const name in headers) {
|
||||
headerArray.push([name, headers[name]]);
|
||||
}
|
||||
return headerArray;
|
||||
}
|
||||
|
||||
let _requestId = 1;
|
||||
function generateRequestId() {
|
||||
return _requestId++;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This class is a wrapper around the native RCTNetworking module. It adds a necessary unique
|
||||
* requestId to each network request that can be used to abort that request later on.
|
||||
*/
|
||||
class RCTNetworking {
|
||||
class RCTNetworking extends NativeEventEmitter {
|
||||
|
||||
static sendRequest(method, url, headers, data, useIncrementalUpdates, timeout) {
|
||||
var requestId = generateRequestId();
|
||||
constructor() {
|
||||
super(RCTNetworkingNative);
|
||||
}
|
||||
|
||||
sendRequest(method, url, headers, data, incrementalUpdates, timeout, callback) {
|
||||
if (typeof data === 'string') {
|
||||
data = {string: data};
|
||||
} else if (data instanceof FormData) {
|
||||
data = {
|
||||
formData: data.getParts().map((part) => {
|
||||
part.headers = convertHeadersMapToArray(part.headers);
|
||||
return part;
|
||||
}),
|
||||
};
|
||||
}
|
||||
const requestId = generateRequestId();
|
||||
RCTNetworkingNative.sendRequest(
|
||||
method,
|
||||
url,
|
||||
requestId,
|
||||
headers,
|
||||
convertHeadersMapToArray(headers),
|
||||
data,
|
||||
useIncrementalUpdates,
|
||||
timeout);
|
||||
return requestId;
|
||||
incrementalUpdates,
|
||||
timeout
|
||||
);
|
||||
callback(requestId);
|
||||
}
|
||||
|
||||
static abortRequest(requestId) {
|
||||
abortRequest(requestId) {
|
||||
RCTNetworkingNative.abortRequest(requestId);
|
||||
}
|
||||
|
||||
static clearCookies(callback) {
|
||||
clearCookies(callback) {
|
||||
RCTNetworkingNative.clearCookies(callback);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RCTNetworking;
|
||||
module.exports = new RCTNetworking();
|
||||
|
||||
Reference in New Issue
Block a user