mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
a81b7d18fa
Summary: Refactors `RCTNeworking.ios.js` so that event registration does not get passed along to `NativeNetworkingIOS`. Instead, we go straight to `RCTDeviceEventEmitter` (which is what `NativeEventEmitter` ultimately does when `nativeModule` is not supplied). This optimization reduces overhead of making network requests, and it is made possible because `NativeNetworkingIOS` does not actually do any meaningful work when `startObserving` is invoked. Changelog: [iOS][Removed] - Removed event methods except `addListener` from `Networking` Reviewed By: lunaleaps Differential Revision: D26137965 fbshipit-source-id: b6e0288689459ddb8ecf8d34dce6250d3b0ecb59
112 lines
2.5 KiB
JavaScript
112 lines
2.5 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow strict-local
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
|
|
import NativeNetworkingIOS from './NativeNetworkingIOS';
|
|
import {type NativeResponseType} from './XMLHttpRequest';
|
|
import convertRequestBody, {type RequestBody} from './convertRequestBody';
|
|
import {type EventSubscription} from '../vendor/emitter/EventEmitter';
|
|
|
|
type RCTNetworkingEventDefinitions = $ReadOnly<{
|
|
didSendNetworkData: [
|
|
[
|
|
number, // requestId
|
|
number, // progress
|
|
number, // total
|
|
],
|
|
],
|
|
didReceiveNetworkResponse: [
|
|
[
|
|
number, // requestId
|
|
number, // status
|
|
?{[string]: string}, // responseHeaders
|
|
?string, // responseURL
|
|
],
|
|
],
|
|
didReceiveNetworkData: [
|
|
[
|
|
number, // requestId
|
|
string, // response
|
|
],
|
|
],
|
|
didReceiveNetworkIncrementalData: [
|
|
[
|
|
number, // requestId
|
|
string, // responseText
|
|
number, // progress
|
|
number, // total
|
|
],
|
|
],
|
|
didReceiveNetworkDataProgress: [
|
|
[
|
|
number, // requestId
|
|
number, // loaded
|
|
number, // total
|
|
],
|
|
],
|
|
didCompleteNetworkResponse: [
|
|
[
|
|
number, // requestId
|
|
string, // error
|
|
boolean, // timeOutError
|
|
],
|
|
],
|
|
}>;
|
|
|
|
const RCTNetworking = {
|
|
addListener<K: $Keys<RCTNetworkingEventDefinitions>>(
|
|
eventType: K,
|
|
listener: (...$ElementType<RCTNetworkingEventDefinitions, K>) => mixed,
|
|
context?: mixed,
|
|
): EventSubscription {
|
|
return RCTDeviceEventEmitter.addListener(eventType, listener, context);
|
|
},
|
|
|
|
sendRequest(
|
|
method: string,
|
|
trackingName: string,
|
|
url: string,
|
|
headers: {...},
|
|
data: RequestBody,
|
|
responseType: NativeResponseType,
|
|
incrementalUpdates: boolean,
|
|
timeout: number,
|
|
callback: (requestId: number) => void,
|
|
withCredentials: boolean,
|
|
) {
|
|
const body = convertRequestBody(data);
|
|
NativeNetworkingIOS.sendRequest(
|
|
{
|
|
method,
|
|
url,
|
|
data: {...body, trackingName},
|
|
headers,
|
|
responseType,
|
|
incrementalUpdates,
|
|
timeout,
|
|
withCredentials,
|
|
},
|
|
callback,
|
|
);
|
|
},
|
|
|
|
abortRequest(requestId: number) {
|
|
NativeNetworkingIOS.abortRequest(requestId);
|
|
},
|
|
|
|
clearCookies(callback: (result: boolean) => void) {
|
|
NativeNetworkingIOS.clearCookies(callback);
|
|
},
|
|
};
|
|
|
|
module.exports = RCTNetworking;
|