Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions packages/react-native/Libraries/Network/RCTNetworking.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
* @flow
*/

import type {EventSubscription} from '../vendor/emitter/EventEmitter';
import type {RequestBody} from './convertRequestBody';
import type {RCTNetworkingEventDefinitions} from './RCTNetworking.js';
import type {NativeResponseType} from './XMLHttpRequest';

// Do not require the native RCTNetworking module directly! Use this wrapper module instead.
Expand All @@ -35,19 +37,25 @@ function generateRequestId(): number {
return _requestId++;
}

const emitter = new NativeEventEmitter<$FlowFixMe>(
// T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior
// If you want to use the native module on other platforms, please remove this condition and test its behavior
Platform.OS !== 'ios' ? null : NativeNetworkingAndroid,
);

/**
* This class is a wrapper around the native RCTNetworking module. It adds a necessary unique
* This object 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.
*/
// FIXME: use typed events
class RCTNetworking extends NativeEventEmitter<$FlowFixMe> {
constructor() {
super(
// T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior
// If you want to use the native module on other platforms, please remove this condition and test its behavior
Platform.OS !== 'ios' ? null : NativeNetworkingAndroid,
);
}
const RCTNetworking = {
addListener<K: $Keys<RCTNetworkingEventDefinitions>>(
eventType: K,
listener: (...$ElementType<RCTNetworkingEventDefinitions, K>) => mixed,
context?: mixed,
): EventSubscription {
// $FlowFixMe[incompatible-call]
return emitter.addListener(eventType, listener, context);
},

sendRequest(
method: string,
Expand Down Expand Up @@ -81,15 +89,15 @@ class RCTNetworking extends NativeEventEmitter<$FlowFixMe> {
withCredentials,
);
callback(requestId);
}
},

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

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

export default (new RCTNetworking(): RCTNetworking);
export default RCTNetworking;
47 changes: 1 addition & 46 deletions packages/react-native/Libraries/Network/RCTNetworking.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,9 @@ import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
import {type EventSubscription} from '../vendor/emitter/EventEmitter';
import convertRequestBody, {type RequestBody} from './convertRequestBody';
import NativeNetworkingIOS from './NativeNetworkingIOS';
import {type RCTNetworkingEventDefinitions} from './RCTNetworking.js';
import {type NativeResponseType} from './XMLHttpRequest';

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {EventSubscription} from '../vendor/emitter/EventEmitter';
import type {RequestBody} from './convertRequestBody';
import type {NativeResponseType} from './XMLHttpRequest';

type RCTNetworkingEventDefinitions = $ReadOnly<{
export type RCTNetworkingEventDefinitions = $ReadOnly<{
didSendNetworkData: [
[
number, // requestId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6669,7 +6669,7 @@ declare export default typeof NativeNetworkingIOS;
`;

exports[`public API should not change unintentionally Libraries/Network/RCTNetworking.js.flow 1`] = `
"type RCTNetworkingEventDefinitions = $ReadOnly<{
"export type RCTNetworkingEventDefinitions = $ReadOnly<{
didSendNetworkData: [[number, number, number]],
didReceiveNetworkResponse: [[number, number, ?{ [string]: string }, ?string]],
didReceiveNetworkData: [[number, string]],
Expand Down
Loading