|
4 | 4 | * This source code is licensed under the MIT license found in the
|
5 | 5 | * LICENSE file in the root directory of this source tree.
|
6 | 6 | *
|
7 |
| - * @format |
8 | 7 | * @flow strict-local
|
| 8 | + * @format |
9 | 9 | */
|
10 | 10 |
|
11 | 11 | 'use strict';
|
12 | 12 |
|
13 |
| -import NativeEventEmitter from '../EventEmitter/NativeEventEmitter'; |
| 13 | +import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter'; |
14 | 14 | import NativeNetworkingIOS from './NativeNetworkingIOS';
|
15 |
| -import type {NativeResponseType} from './XMLHttpRequest'; |
16 |
| -import convertRequestBody from './convertRequestBody'; |
17 |
| -import type {RequestBody} from './convertRequestBody'; |
| 15 | +import {type NativeResponseType} from './XMLHttpRequest'; |
| 16 | +import convertRequestBody, {type RequestBody} from './convertRequestBody'; |
| 17 | +import {type EventSubscription} from '../vendor/emitter/EventEmitter'; |
18 | 18 |
|
19 |
| -// FIXME: use typed events |
20 |
| -class RCTNetworking extends NativeEventEmitter<$FlowFixMe> { |
21 |
| - constructor() { |
22 |
| - const disableCallsIntoModule = |
23 |
| - typeof global.__disableRCTNetworkingExtraneousModuleCalls === 'function' |
24 |
| - ? global.__disableRCTNetworkingExtraneousModuleCalls() |
25 |
| - : false; |
| 19 | +type RCTNetworkingEventDefinitions = $ReadOnly<{ |
| 20 | + didSendNetworkData: [ |
| 21 | + [ |
| 22 | + number, // requestId |
| 23 | + number, // progress |
| 24 | + number, // total |
| 25 | + ], |
| 26 | + ], |
| 27 | + didReceiveNetworkResponse: [ |
| 28 | + [ |
| 29 | + number, // requestId |
| 30 | + number, // status |
| 31 | + ?{[string]: string}, // responseHeaders |
| 32 | + ?string, // responseURL |
| 33 | + ], |
| 34 | + ], |
| 35 | + didReceiveNetworkData: [ |
| 36 | + [ |
| 37 | + number, // requestId |
| 38 | + string, // response |
| 39 | + ], |
| 40 | + ], |
| 41 | + didReceiveNetworkIncrementalData: [ |
| 42 | + [ |
| 43 | + number, // requestId |
| 44 | + string, // responseText |
| 45 | + number, // progress |
| 46 | + number, // total |
| 47 | + ], |
| 48 | + ], |
| 49 | + didReceiveNetworkDataProgress: [ |
| 50 | + [ |
| 51 | + number, // requestId |
| 52 | + number, // loaded |
| 53 | + number, // total |
| 54 | + ], |
| 55 | + ], |
| 56 | + didCompleteNetworkResponse: [ |
| 57 | + [ |
| 58 | + number, // requestId |
| 59 | + string, // error |
| 60 | + boolean, // timeOutError |
| 61 | + ], |
| 62 | + ], |
| 63 | +}>; |
26 | 64 |
|
27 |
| - super(NativeNetworkingIOS, { |
28 |
| - __SECRET_DISABLE_CALLS_INTO_MODULE_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: disableCallsIntoModule, |
29 |
| - }); |
30 |
| - } |
| 65 | +const RCTNetworking = { |
| 66 | + addListener<K: $Keys<RCTNetworkingEventDefinitions>>( |
| 67 | + eventType: K, |
| 68 | + listener: (...$ElementType<RCTNetworkingEventDefinitions, K>) => mixed, |
| 69 | + context?: mixed, |
| 70 | + ): EventSubscription { |
| 71 | + return RCTDeviceEventEmitter.addListener(eventType, listener, context); |
| 72 | + }, |
31 | 73 |
|
32 | 74 | sendRequest(
|
33 | 75 | method: string,
|
@@ -55,15 +97,15 @@ class RCTNetworking extends NativeEventEmitter<$FlowFixMe> {
|
55 | 97 | },
|
56 | 98 | callback,
|
57 | 99 | );
|
58 |
| - } |
| 100 | + }, |
59 | 101 |
|
60 | 102 | abortRequest(requestId: number) {
|
61 | 103 | NativeNetworkingIOS.abortRequest(requestId);
|
62 |
| - } |
| 104 | + }, |
63 | 105 |
|
64 | 106 | clearCookies(callback: (result: boolean) => void) {
|
65 | 107 | NativeNetworkingIOS.clearCookies(callback);
|
66 |
| - } |
67 |
| -} |
| 108 | + }, |
| 109 | +}; |
68 | 110 |
|
69 |
| -module.exports = (new RCTNetworking(): RCTNetworking); |
| 111 | +module.exports = RCTNetworking; |
0 commit comments