Skip to content

Commit 4d6528f

Browse files
author
Your Name
committed
refactor: Improve JSDoc and fix bugs in utility functions
- Add comprehensive JSDoc to utility functions (dismissKeyboard, stringifyViewConfig, RCTLog, SceneTracker, FeatureDetection, mapWithSeparator, differ functions) - Fix LayoutAnimation setLayoutAnimationEnabled parameter assignment bug - Resolve VirtualizedList scrollToEnd TODO with implementation rationale - Add console warning for unavailable NativeDialogManagerAndroid
1 parent bcadeda commit 4d6528f

14 files changed

Lines changed: 163 additions & 21 deletions

File tree

packages/react-native/Libraries/Alert/RCTAlertManager.android.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,25 @@ import NativeDialogManagerAndroid from '../NativeModules/specs/NativeDialogManag
1414

1515
function emptyCallback() {}
1616

17+
/**
18+
* Shows an alert dialog on Android using the native DialogManagerAndroid module.
19+
* Falls back gracefully if the native module is not available.
20+
*
21+
* @param {Args} args - The alert configuration (title, message, buttons, etc.)
22+
* @param {Function} callback - Callback function invoked with (id, value) when user selects an option
23+
* @returns {void}
24+
* @note NativeDialogManagerAndroid provides better Android-specific UI/UX than the
25+
* standard AlertManager. If unavailable, the alert silently fails to prevent crashes.
26+
* See TODO(5998984) for improved polyfill implementation.
27+
*/
1728
export function alertWithArgs(
1829
args: Args,
1930
callback: (id: number, value: string) => void,
2031
) {
21-
// TODO(5998984): Polyfill it correctly with DialogManagerAndroid
2232
if (!NativeDialogManagerAndroid) {
33+
console.warn(
34+
'NativeDialogManagerAndroid is not available. Alert may not be displayed.',
35+
);
2336
return;
2437
}
2538

packages/react-native/Libraries/LayoutAnimation/LayoutAnimation.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,15 @@ type OnAnimationDidFailCallback = () => void;
4545
let isLayoutAnimationEnabled: boolean =
4646
ReactNativeFeatureFlags.isLayoutAnimationEnabled();
4747

48+
/**
49+
* Internal function to enable or disable layout animations.
50+
* When disabled, configureNext calls will be no-ops.
51+
*
52+
* @param {boolean} value - Whether to enable layout animations
53+
* @internal
54+
*/
4855
function setLayoutAnimationEnabled(value: boolean) {
49-
isLayoutAnimationEnabled = isLayoutAnimationEnabled;
56+
isLayoutAnimationEnabled = value;
5057
}
5158

5259
/**

packages/react-native/Libraries/Utilities/FeatureDetection.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@
99
*/
1010

1111
/**
12-
* @return whether or not a @param {function} f is provided natively by calling
13-
* `toString` and check if the result includes `[native code]` in it.
12+
* Detects if a function is a native function (not a polyfill or user-defined).
1413
*
15-
* Note that a polyfill can technically fake this behavior but few does it.
16-
* Therefore, this is usually good enough for our purpose.
14+
* Checks if the function's toString representation includes `[native code]`.
15+
* Note: A polyfill can technically fake this, but most don't, making this
16+
* check reliable for practical purposes.
17+
*
18+
* @param {Function} f - Function to check
19+
* @returns {boolean} True if the function is a native function
1720
*/
1821
export function isNativeFunction(f: Function): boolean {
1922
return typeof f === 'function' && f.toString().indexOf('[native code]') > -1;
2023
}
2124

2225
/**
23-
* @return whether or not the constructor of @param {object} o is an native
24-
* function named with @param {string} expectedName.
26+
* Checks if an object's constructor is a native function with a specific name.
27+
*
28+
* @param {Object} o - Object to check
29+
* @param {string} expectedName - Expected constructor name
30+
* @returns {boolean} True if the constructor is native and matches the expected name
2531
*/
2632
export function hasNativeConstructor(o: Object, expectedName: string): boolean {
2733
const con = Object.getPrototypeOf(o).constructor;

packages/react-native/Libraries/Utilities/RCTLog.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ const levelsMap = {
2323
let warningHandler: ?(...Array<unknown>) => void = null;
2424

2525
const RCTLog = {
26-
// level one of log, info, warn, error, mustfix
26+
/**
27+
* Logs to console only if the native logging hook is unavailable.
28+
* If available, delegates to native logging and reports warnings to LogBox.
29+
*
30+
* @param {string} level - Log level: 'log', 'info', 'warn', 'error', or 'fatal'
31+
* @param {...any} args - Arguments to log
32+
*/
2733
logIfNoNativeHook(level: string, ...args: Array<unknown>): void {
2834
// We already printed in the native console, so only log here if using a js debugger
2935
if (typeof global.nativeLoggingHook === 'undefined') {
@@ -36,7 +42,14 @@ const RCTLog = {
3642
}
3743
},
3844

39-
// Log to console regardless of nativeLoggingHook
45+
/**
46+
* Logs to console regardless of native logging hook availability.
47+
* Throws an invariant if the log level is invalid.
48+
*
49+
* @param {string} level - Log level: 'log', 'info', 'warn', 'error', or 'fatal'
50+
* @param {...any} args - Arguments to log
51+
* @throws Invariant error if level is not a valid log level
52+
*/
4053
logToConsole(level: string, ...args: Array<unknown>): void {
4154
// $FlowFixMe[invalid-computed-prop]
4255
const logFn = levelsMap[level];
@@ -48,6 +61,11 @@ const RCTLog = {
4861
console[logFn](...args);
4962
},
5063

64+
/**
65+
* Sets a custom warning handler to process warnings reported to LogBox.
66+
*
67+
* @param {?Function} handler - Function to handle warnings, or null to remove the handler
68+
*/
5169
setWarningHandler(handler: typeof warningHandler): void {
5270
warningHandler = handler;
5371
},

packages/react-native/Libraries/Utilities/SceneTracker.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,33 @@ let _listeners: Array<(scene: Scene) => void> = [];
1616

1717
let _activeScene: Scene = {name: 'default'};
1818

19+
/**
20+
* Global scene tracker for managing the currently active scene in the application.
21+
* Notifies listeners whenever the active scene changes.
22+
*/
1923
const SceneTracker = {
24+
/**
25+
* Sets the currently active scene and notifies all registered listeners.
26+
* @param {Scene} scene - The new active scene object
27+
*/
2028
setActiveScene(scene: Scene) {
2129
_activeScene = scene;
2230
_listeners.forEach(listener => listener(_activeScene));
2331
},
2432

33+
/**
34+
* Gets the currently active scene.
35+
* @returns {Scene} The active scene object
36+
*/
2537
getActiveScene(): Scene {
2638
return _activeScene;
2739
},
2840

41+
/**
42+
* Registers a listener to be called whenever the active scene changes.
43+
* @param {Function} callback - Function called with the new scene when it changes
44+
* @returns {Object} Object with a remove() method to unsubscribe
45+
*/
2946
addActiveSceneChangedListener(callback: (scene: Scene) => void): {
3047
remove: () => void,
3148
...

packages/react-native/Libraries/Utilities/differ/deepDiffer.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,23 @@ function unstable_setLogListeners(listeners: ?LogListeners) {
2525
logListeners = listeners;
2626
}
2727

28-
/*
29-
* @returns {bool} true if different, false if equal
28+
/**
29+
* Deep equality comparison for arbitrary JavaScript values.
30+
*
31+
* Recursively compares objects, arrays, and primitives. Functions are
32+
* considered equal unless explicitly configured otherwise. Returns true
33+
* if values differ, false if equal (useful for determining if updates are needed).
34+
*
35+
* @param {any} one - First value to compare
36+
* @param {any} two - Second value to compare
37+
* @param {Options|number} [maxDepthOrOptions] - Max depth (number) or options object
38+
* @param {Options} [maybeOptions] - Options when first arg is a number
39+
* @returns {boolean} True if values differ, false if equal
40+
*
41+
* @example
42+
* deepDiffer(1, 2) // true (different)
43+
* deepDiffer({a: 1}, {a: 1}) // false (equal)
44+
* deepDiffer({a: {b: 1}}, {a: {b: 1}}, 3) // false (equal, max depth 3)
3045
*/
3146
function deepDiffer(
3247
one: any,

packages/react-native/Libraries/Utilities/differ/insetsDiffer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ const dummyInsets = {
2525
bottom: undefined,
2626
};
2727

28+
/**
29+
* Compares two inset objects for equality.
30+
* Returns true if the insets are different, false if equal.
31+
*
32+
* @param {Inset} one - First inset object (top, left, right, bottom)
33+
* @param {Inset} two - Second inset object
34+
* @returns {boolean} True if insets differ, false if equal
35+
*/
2836
function insetsDiffer(one: Inset, two: Inset): boolean {
2937
one = one || dummyInsets;
3038
two = two || dummyInsets;

packages/react-native/Libraries/Utilities/differ/matricesDiffer.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
'use strict';
1212

1313
/**
14-
* Unrolls an array comparison specially for matrices. Prioritizes
15-
* checking of indices that are most likely to change so that the comparison
16-
* bails as early as possible.
14+
* Unrolls an array comparison specially for matrices (4x4 transformation matrices).
1715
*
18-
* @param {MatrixMath.Matrix} one First matrix.
19-
* @param {MatrixMath.Matrix} two Second matrix.
20-
* @return {boolean} Whether or not the two matrices differ.
16+
* Prioritizes checking of indices that are most likely to change so that the
17+
* comparison bails as early as possible. This optimizes for common transformations.
18+
*
19+
* @param {?Array<number>} one - First matrix (16 elements)
20+
* @param {?Array<number>} two - Second matrix (16 elements)
21+
* @returns {boolean} True if matrices differ, false if equal
2122
*/
2223
function matricesDiffer(one: ?Array<number>, two: ?Array<number>): boolean {
2324
if (one === two) {

packages/react-native/Libraries/Utilities/differ/pointsDiffer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ type Point = {
1818

1919
const dummyPoint: Point = {x: undefined, y: undefined};
2020

21+
/**
22+
* Compares two point objects for equality.
23+
* Returns true if the points are different, false if equal.
24+
*
25+
* @param {?Point} one - First point object
26+
* @param {?Point} two - Second point object
27+
* @returns {boolean} True if points differ, false if equal
28+
*/
2129
function pointsDiffer(one: ?Point, two: ?Point): boolean {
2230
one = one || dummyPoint;
2331
two = two || dummyPoint;

packages/react-native/Libraries/Utilities/differ/sizesDiffer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
const dummySize = {width: undefined, height: undefined};
1414
type Size = {width: ?number, height: ?number};
1515

16+
/**
17+
* Compares two size objects for equality.
18+
* Returns true if the sizes are different, false if equal.
19+
*
20+
* @param {Size} one - First size object
21+
* @param {Size} two - Second size object
22+
* @returns {boolean} True if sizes differ, false if equal
23+
*/
1624
function sizesDiffer(one: Size, two: Size): boolean {
1725
const defaultedOne = one || dummySize;
1826
const defaultedTwo = two || dummySize;

0 commit comments

Comments
 (0)