Animated: Improve Types in Animated{Props,Style} (#46383)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/46383

Minor refactors to the internal types of `AnimatedProps` and `AnimatedStyle` to avoid using `Object` and `any`.

This also migrates private properties prefixed with `_` to instead use `#` (ECMAScript private properties). This has the desired benefit of minimizing thrash in the `public-api-test.js` snapshot, too.

There are no behavior changes associated with this commit.

Changelog:
[Internal]

Reviewed By: javache

Differential Revision: D62351005

fbshipit-source-id: bb69d4db50aef5e0b3193971dac581686e359a95
This commit is contained in:
Tim Yung authored and Facebook GitHub Bot committed 2024-09-09 20:34:57 -07:00
1 parent fe6228512e
commit 734c1505f4
3 files changed
+36 -47

No files matched your search

@@ -8,8 +8,6 @@
* @format
*/
'use strict';
import type {PlatformConfig} from '../AnimatedPlatformConfig';
import {findNodeHandle} from '../../ReactNative/RendererProxy';
@@ -20,12 +18,12 @@ import AnimatedObject from './AnimatedObject';
import AnimatedStyle from './AnimatedStyle';
import invariant from 'invariant';
function createAnimatedProps(
inputProps: Object,
): [$ReadOnlyArray<string>, $ReadOnlyArray<AnimatedNode>, Object] {
function createAnimatedProps(inputProps: {
[string]: mixed,
}): [$ReadOnlyArray<string>, $ReadOnlyArray<AnimatedNode>, {[string]: mixed}] {
const nodeKeys: Array<string> = [];
const nodes: Array<AnimatedNode> = [];
const props: Object = {};
const props: {[string]: mixed} = {};
const keys = Object.keys(inputProps);
for (let ii = 0, length = keys.length; ii < length; ii++) {
@@ -62,29 +60,28 @@ function createAnimatedProps(
}
export default class AnimatedProps extends AnimatedNode {
#animatedView: any = null;
#callback: () => void;
#nodeKeys: $ReadOnlyArray<string>;
#nodes: $ReadOnlyArray<AnimatedNode>;
#props: {[string]: mixed};
_animatedView: any = null;
_props: Object;
_callback: () => void;
constructor(inputProps: Object, callback: () => void) {
constructor(inputProps: {[string]: mixed}, callback: () => void) {
super();
const [nodeKeys, nodes, props] = createAnimatedProps(inputProps);
this.#nodeKeys = nodeKeys;
this.#nodes = nodes;
this._props = props;
this._callback = callback;
this.#props = props;
this.#callback = callback;
}
__getValue(): Object {
const props: {[string]: any | ((...args: any) => void)} = {};
const props: {[string]: mixed} = {};
const keys = Object.keys(this._props);
const keys = Object.keys(this.#props);
for (let ii = 0, length = keys.length; ii < length; ii++) {
const key = keys[ii];
const value = this._props[key];
const value = this.#props[key];
if (value instanceof AnimatedNode) {
props[key] = value.__getValue();
@@ -99,7 +96,7 @@ export default class AnimatedProps extends AnimatedNode {
}
__getAnimatedValue(): Object {
const props: {[string]: any} = {};
const props: {[string]: mixed} = {};
const nodeKeys = this.#nodeKeys;
const nodes = this.#nodes;
@@ -121,10 +118,10 @@ export default class AnimatedProps extends AnimatedNode {
}
__detach(): void {
if (this.__isNative && this._animatedView) {
if (this.__isNative && this.#animatedView) {
this.__disconnectAnimatedView();
}
this._animatedView = null;
this.#animatedView = null;
const nodes = this.#nodes;
for (let ii = 0, length = nodes.length; ii < length; ii++) {
@@ -136,7 +133,7 @@ export default class AnimatedProps extends AnimatedNode {
}
update(): void {
this._callback();
this.#callback();
}
__makeNative(platformConfig: ?PlatformConfig): void {
@@ -154,17 +151,17 @@ export default class AnimatedProps extends AnimatedNode {
// where it will be needed to traverse the graph of attached values.
super.__setPlatformConfig(platformConfig);
if (this._animatedView) {
if (this.#animatedView) {
this.__connectAnimatedView();
}
}
}
setNativeView(animatedView: any): void {
if (this._animatedView === animatedView) {
if (this.#animatedView === animatedView) {
return;
}
this._animatedView = animatedView;
this.#animatedView = animatedView;
if (this.__isNative) {
this.__connectAnimatedView();
}
@@ -172,7 +169,7 @@ export default class AnimatedProps extends AnimatedNode {
__connectAnimatedView(): void {
invariant(this.__isNative, 'Expected node to be marked as "native"');
const nativeViewTag: ?number = findNodeHandle(this._animatedView);
const nativeViewTag: ?number = findNodeHandle(this.#animatedView);
invariant(
nativeViewTag != null,
'Unable to locate attached view in the native tree',
@@ -185,7 +182,7 @@ export default class AnimatedProps extends AnimatedNode {
__disconnectAnimatedView(): void {
invariant(this.__isNative, 'Expected node to be marked as "native"');
const nativeViewTag: ?number = findNodeHandle(this._animatedView);
const nativeViewTag: ?number = findNodeHandle(this.#animatedView);
invariant(
nativeViewTag != null,
'Unable to locate attached view in the native tree',
@@ -8,8 +8,6 @@
* @format
*/
'use strict';
import type {PlatformConfig} from '../AnimatedPlatformConfig';
import {validateStyles} from '../../../src/private/animated/NativeAnimatedValidation';
@@ -24,10 +22,10 @@ import AnimatedWithChildren from './AnimatedWithChildren';
function createAnimatedStyle(
inputStyle: {[string]: mixed},
keepUnanimatedValues: boolean,
): [$ReadOnlyArray<string>, $ReadOnlyArray<AnimatedNode>, Object] {
): [$ReadOnlyArray<string>, $ReadOnlyArray<AnimatedNode>, {[string]: mixed}] {
const nodeKeys: Array<string> = [];
const nodes: Array<AnimatedNode> = [];
const style: {[string]: any} = {};
const style: {[string]: mixed} = {};
const keys = Object.keys(inputStyle);
for (let ii = 0, length = keys.length; ii < length; ii++) {
@@ -71,11 +69,10 @@ function createAnimatedStyle(
}
export default class AnimatedStyle extends AnimatedWithChildren {
#inputStyle: any;
#nodeKeys: $ReadOnlyArray<string>;
#nodes: $ReadOnlyArray<AnimatedNode>;
_inputStyle: any;
_style: {[string]: any};
#style: {[string]: mixed};
/**
* Creates an `AnimatedStyle` if `value` contains `AnimatedNode` instances.
@@ -99,23 +96,23 @@ export default class AnimatedStyle extends AnimatedWithChildren {
constructor(
nodeKeys: $ReadOnlyArray<string>,
nodes: $ReadOnlyArray<AnimatedNode>,
style: {[string]: any},
style: {[string]: mixed},
inputStyle: any,
) {
super();
this.#nodeKeys = nodeKeys;
this.#nodes = nodes;
this._style = style;
this._inputStyle = inputStyle;
this.#style = style;
this.#inputStyle = inputStyle;
}
__getValue(): Object | Array<Object> {
const style: {[string]: any} = {};
const style: {[string]: mixed} = {};
const keys = Object.keys(this._style);
const keys = Object.keys(this.#style);
for (let ii = 0, length = keys.length; ii < length; ii++) {
const key = keys[ii];
const value = this._style[key];
const value = this.#style[key];
if (value instanceof AnimatedNode) {
style[key] = value.__getValue();
@@ -124,11 +121,11 @@ export default class AnimatedStyle extends AnimatedWithChildren {
}
}
return Platform.OS === 'web' ? [this._inputStyle, style] : style;
return Platform.OS === 'web' ? [this.#inputStyle, style] : style;
}
__getAnimatedValue(): Object {
const style: {[string]: any} = {};
const style: {[string]: mixed} = {};
const nodeKeys = this.#nodeKeys;
const nodes = this.#nodes;
@@ -946,10 +946,7 @@ exports[`public API should not change unintentionally Libraries/Animated/nodes/A
exports[`public API should not change unintentionally Libraries/Animated/nodes/AnimatedProps.js 1`] = `
"declare export default class AnimatedProps extends AnimatedNode {
_animatedView: any;
_props: Object;
_callback: () => void;
constructor(inputProps: Object, callback: () => void): void;
constructor(inputProps: { [string]: mixed }, callback: () => void): void;
__getValue(): Object;
__getAnimatedValue(): Object;
__attach(): void;
@@ -967,13 +964,11 @@ exports[`public API should not change unintentionally Libraries/Animated/nodes/A
exports[`public API should not change unintentionally Libraries/Animated/nodes/AnimatedStyle.js 1`] = `
"declare export default class AnimatedStyle extends AnimatedWithChildren {
_inputStyle: any;
_style: { [string]: any };
static from(inputStyle: any): ?AnimatedStyle;
constructor(
nodeKeys: $ReadOnlyArray<string>,
nodes: $ReadOnlyArray<AnimatedNode>,
style: { [string]: any },
style: { [string]: mixed },
inputStyle: any
): void;
__getValue(): Object | Array<Object>;