Skip to content

Commit

Permalink
RN: Cleanup RCTNetworking (iOS)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yungsters authored and facebook-github-bot committed Feb 1, 2021
1 parent 88c4349 commit a81b7d1
Showing 1 changed file with 63 additions and 21 deletions.
84 changes: 63 additions & 21 deletions Libraries/Network/RCTNetworking.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,72 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
* @format
*/

'use strict';

import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
import NativeNetworkingIOS from './NativeNetworkingIOS';
import type {NativeResponseType} from './XMLHttpRequest';
import convertRequestBody from './convertRequestBody';
import type {RequestBody} from './convertRequestBody';
import {type NativeResponseType} from './XMLHttpRequest';
import convertRequestBody, {type RequestBody} from './convertRequestBody';
import {type EventSubscription} from '../vendor/emitter/EventEmitter';

// FIXME: use typed events
class RCTNetworking extends NativeEventEmitter<$FlowFixMe> {
constructor() {
const disableCallsIntoModule =
typeof global.__disableRCTNetworkingExtraneousModuleCalls === 'function'
? global.__disableRCTNetworkingExtraneousModuleCalls()
: false;
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
],
],
}>;

super(NativeNetworkingIOS, {
__SECRET_DISABLE_CALLS_INTO_MODULE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: disableCallsIntoModule,
});
}
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,
Expand Down Expand Up @@ -55,15 +97,15 @@ class RCTNetworking extends NativeEventEmitter<$FlowFixMe> {
},
callback,
);
}
},

abortRequest(requestId: number) {
NativeNetworkingIOS.abortRequest(requestId);
}
},

clearCookies(callback: (result: boolean) => void) {
NativeNetworkingIOS.clearCookies(callback);
}
}
},
};

module.exports = (new RCTNetworking(): RCTNetworking);
module.exports = RCTNetworking;

0 comments on commit a81b7d1

Please sign in to comment.