Skip to content

Commit

Permalink
Add minimal Flow types to remaining files under Libraries/ (facebook#…
Browse files Browse the repository at this point in the history
…45785)

Summary:
Pull Request resolved: facebook#45785

Adds `flow` annotation, and/or adds minimal Flow types to remaining files under `packages/react-native/Libraries/`. As of this diff, 100% of this directory is now parsable by `flow-api-translator` and covered by `public-api-test`.

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D60377082
  • Loading branch information
huntie authored and facebook-github-bot committed Aug 2, 2024
1 parent 6c245f2 commit fa483d2
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/

'use strict';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/

'use strict';

import PooledClass from './PooledClass';

const twoArgumentPooler = PooledClass.twoArgumentPooler;
Expand All @@ -19,11 +21,14 @@ const twoArgumentPooler = PooledClass.twoArgumentPooler;
* @param {number} height Height of bounding rectangle.
* @constructor BoundingDimensions
*/
function BoundingDimensions(width, height) {
// $FlowFixMe[missing-this-annot]
function BoundingDimensions(width: number, height: number) {
this.width = width;
this.height = height;
}

// $FlowFixMe[prop-missing]
// $FlowFixMe[missing-this-annot]
BoundingDimensions.prototype.destructor = function () {
this.width = null;
this.height = null;
Expand All @@ -33,13 +38,16 @@ BoundingDimensions.prototype.destructor = function () {
* @param {HTMLElement} element Element to return `BoundingDimensions` for.
* @return {BoundingDimensions} Bounding dimensions of `element`.
*/
BoundingDimensions.getPooledFromElement = function (element) {
BoundingDimensions.getPooledFromElement = function (
element: HTMLElement,
): typeof BoundingDimensions {
// $FlowFixMe[prop-missing]
return BoundingDimensions.getPooled(
element.offsetWidth,
element.offsetHeight,
);
};

PooledClass.addPoolingTo(BoundingDimensions, twoArgumentPooler);
PooledClass.addPoolingTo(BoundingDimensions as $FlowFixMe, twoArgumentPooler);

module.exports = BoundingDimensions;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/

'use strict';

import PooledClass from './PooledClass';

const twoArgumentPooler = PooledClass.twoArgumentPooler;
Expand All @@ -20,16 +22,19 @@ const twoArgumentPooler = PooledClass.twoArgumentPooler;
* @param {number} windowStartKey Key that window starts at.
* @param {number} windowEndKey Key that window ends at.
*/
function Position(left, top) {
// $FlowFixMe[missing-this-annot]
function Position(left: number, top: number) {
this.left = left;
this.top = top;
}

// $FlowFixMe[prop-missing]
// $FlowFixMe[missing-this-annot]
Position.prototype.destructor = function () {
this.left = null;
this.top = null;
};

PooledClass.addPoolingTo(Position, twoArgumentPooler);
PooledClass.addPoolingTo(Position as $FlowFixMe, twoArgumentPooler);

module.exports = Position;
41 changes: 22 additions & 19 deletions packages/react-native/Libraries/Interaction/TouchHistoryMath.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/

// $FlowFixMe[definition-cycle]
// $FlowFixMe[recursive-definition]
const TouchHistoryMath = {
/**
* This code is optimized and not intended to look beautiful. This allows
Expand All @@ -25,11 +28,11 @@ const TouchHistoryMath = {
* @return {number} value of centroid in specified dimension.
*/
centroidDimension: function (
touchHistory,
touchesChangedAfter,
isXAxis,
ofCurrent,
) {
touchHistory: TouchHistoryMath,
touchesChangedAfter: number,
isXAxis: boolean,
ofCurrent: boolean,
): number {
const touchBank = touchHistory.touchBank;
let total = 0;
let count = 0;
Expand Down Expand Up @@ -82,9 +85,9 @@ const TouchHistoryMath = {
},

currentCentroidXOfTouchesChangedAfter: function (
touchHistory,
touchesChangedAfter,
) {
touchHistory: TouchHistoryMath,
touchesChangedAfter: number,
): number {
return TouchHistoryMath.centroidDimension(
touchHistory,
touchesChangedAfter,
Expand All @@ -94,9 +97,9 @@ const TouchHistoryMath = {
},

currentCentroidYOfTouchesChangedAfter: function (
touchHistory,
touchesChangedAfter,
) {
touchHistory: TouchHistoryMath,
touchesChangedAfter: number,
): number {
return TouchHistoryMath.centroidDimension(
touchHistory,
touchesChangedAfter,
Expand All @@ -106,9 +109,9 @@ const TouchHistoryMath = {
},

previousCentroidXOfTouchesChangedAfter: function (
touchHistory,
touchesChangedAfter,
) {
touchHistory: TouchHistoryMath,
touchesChangedAfter: number,
): number {
return TouchHistoryMath.centroidDimension(
touchHistory,
touchesChangedAfter,
Expand All @@ -118,9 +121,9 @@ const TouchHistoryMath = {
},

previousCentroidYOfTouchesChangedAfter: function (
touchHistory,
touchesChangedAfter,
) {
touchHistory: TouchHistoryMath,
touchesChangedAfter: number,
): number {
return TouchHistoryMath.centroidDimension(
touchHistory,
touchesChangedAfter,
Expand All @@ -129,7 +132,7 @@ const TouchHistoryMath = {
);
},

currentCentroidX: function (touchHistory) {
currentCentroidX: function (touchHistory: TouchHistoryMath): number {
return TouchHistoryMath.centroidDimension(
touchHistory,
0, // touchesChangedAfter
Expand All @@ -138,7 +141,7 @@ const TouchHistoryMath = {
);
},

currentCentroidY: function (touchHistory) {
currentCentroidY: function (touchHistory: TouchHistoryMath): number {
return TouchHistoryMath.centroidDimension(
touchHistory,
0, // touchesChangedAfter
Expand Down
77 changes: 63 additions & 14 deletions packages/react-native/Libraries/Network/XHRInterceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,57 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/

'use strict';

const XMLHttpRequest = require('./XMLHttpRequest');
// $FlowFixMe[method-unbinding]
const originalXHROpen = XMLHttpRequest.prototype.open;
// $FlowFixMe[method-unbinding]
const originalXHRSend = XMLHttpRequest.prototype.send;
// $FlowFixMe[method-unbinding]
const originalXHRSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;

let openCallback;
let sendCallback;
let requestHeaderCallback;
let headerReceivedCallback;
let responseCallback;
type XHRInterceptorOpenCallback = (
method: string,
url: string,
request: XMLHttpRequest,
) => void;

type XHRInterceptorSendCallback = (
data: string,
request: XMLHttpRequest,
) => void;

type XHRInterceptorRequestHeaderCallback = (
header: string,
value: string,
request: XMLHttpRequest,
) => void;

type XHRInterceptorHeaderReceivedCallback = (
responseContentType: string | void,
responseSize: number | void,
allHeaders: string,
request: XMLHttpRequest,
) => void;

type XHRInterceptorResponseCallback = (
status: number,
timeout: number,
response: string,
responseURL: string,
responseType: string,
request: XMLHttpRequest,
) => void;

let openCallback: XHRInterceptorOpenCallback | null;
let sendCallback: XHRInterceptorSendCallback | null;
let requestHeaderCallback: XHRInterceptorRequestHeaderCallback | null;
let headerReceivedCallback: XHRInterceptorHeaderReceivedCallback | null;
let responseCallback: XHRInterceptorResponseCallback | null;

let isInterceptorEnabled = false;

Expand All @@ -33,39 +70,39 @@ const XHRInterceptor = {
/**
* Invoked before XMLHttpRequest.open(...) is called.
*/
setOpenCallback(callback) {
setOpenCallback(callback: XHRInterceptorOpenCallback) {
openCallback = callback;
},

/**
* Invoked before XMLHttpRequest.send(...) is called.
*/
setSendCallback(callback) {
setSendCallback(callback: XHRInterceptorSendCallback) {
sendCallback = callback;
},

/**
* Invoked after xhr's readyState becomes xhr.HEADERS_RECEIVED.
*/
setHeaderReceivedCallback(callback) {
setHeaderReceivedCallback(callback: XHRInterceptorHeaderReceivedCallback) {
headerReceivedCallback = callback;
},

/**
* Invoked after xhr's readyState becomes xhr.DONE.
*/
setResponseCallback(callback) {
setResponseCallback(callback: XHRInterceptorResponseCallback) {
responseCallback = callback;
},

/**
* Invoked before XMLHttpRequest.setRequestHeader(...) is called.
*/
setRequestHeaderCallback(callback) {
setRequestHeaderCallback(callback: XHRInterceptorRequestHeaderCallback) {
requestHeaderCallback = callback;
},

isInterceptorEnabled() {
isInterceptorEnabled(): boolean {
return isInterceptorEnabled;
},

Expand All @@ -75,7 +112,9 @@ const XHRInterceptor = {
}
// Override `open` method for all XHR requests to intercept the request
// method and url, then pass them through the `openCallback`.
XMLHttpRequest.prototype.open = function (method, url) {
// $FlowFixMe[cannot-write]
// $FlowFixMe[missing-this-annot]
XMLHttpRequest.prototype.open = function (method: string, url: string) {
if (openCallback) {
openCallback(method, url, this);
}
Expand All @@ -84,7 +123,12 @@ const XHRInterceptor = {

// Override `setRequestHeader` method for all XHR requests to intercept
// the request headers, then pass them through the `requestHeaderCallback`.
XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
// $FlowFixMe[cannot-write]
// $FlowFixMe[missing-this-annot]
XMLHttpRequest.prototype.setRequestHeader = function (
header: string,
value: string,
) {
if (requestHeaderCallback) {
requestHeaderCallback(header, value, this);
}
Expand All @@ -93,7 +137,9 @@ const XHRInterceptor = {

// Override `send` method of all XHR requests to intercept the data sent,
// register listeners to intercept the response, and invoke the callbacks.
XMLHttpRequest.prototype.send = function (data) {
// $FlowFixMe[cannot-write]
// $FlowFixMe[missing-this-annot]
XMLHttpRequest.prototype.send = function (data: string) {
if (sendCallback) {
sendCallback(data, this);
}
Expand Down Expand Up @@ -151,8 +197,11 @@ const XHRInterceptor = {
return;
}
isInterceptorEnabled = false;
// $FlowFixMe[cannot-write]
XMLHttpRequest.prototype.send = originalXHRSend;
// $FlowFixMe[cannot-write]
XMLHttpRequest.prototype.open = originalXHROpen;
// $FlowFixMe[cannot-write]
XMLHttpRequest.prototype.setRequestHeader = originalXHRSetRequestHeader;
responseCallback = null;
openCallback = null;
Expand Down
5 changes: 4 additions & 1 deletion packages/react-native/Libraries/WebSocket/WebSocketEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/

'use strict';
Expand All @@ -18,7 +19,9 @@
* In case of "message", the `data` property contains the incoming data.
*/
class WebSocketEvent {
constructor(type, eventInitDict) {
type: string;

constructor(type: string, eventInitDict: $FlowFixMe) {
this.type = type.toString();
Object.assign(this, eventInitDict);
}
Expand Down
Loading

0 comments on commit fa483d2

Please sign in to comment.