Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animated: Cleanup Types in AnimatedTransform #46285

Closed
wants to merge 1 commit into from
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
86 changes: 51 additions & 35 deletions packages/react-native/Libraries/Animated/nodes/AnimatedTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ import NativeAnimatedHelper from '../NativeAnimatedHelper';
import AnimatedNode from './AnimatedNode';
import AnimatedWithChildren from './AnimatedWithChildren';

type Transform<T = AnimatedNode> = {
[string]:
| number
| string
| T
| $ReadOnlyArray<number | string | T>
| {[string]: number | string | T},
};

export default class AnimatedTransform extends AnimatedWithChildren {
_transforms: $ReadOnlyArray<Object>;
_transforms: $ReadOnlyArray<Transform<>>;

constructor(transforms: $ReadOnlyArray<Object>) {
constructor(transforms: $ReadOnlyArray<Transform<>>) {
super();
this._transforms = transforms;
}
Expand All @@ -36,12 +45,16 @@ export default class AnimatedTransform extends AnimatedWithChildren {
super.__makeNative(platformConfig);
}

__getValue(): $ReadOnlyArray<Object> {
return this._get(animatedNode => animatedNode.__getValue());
__getValue(): $ReadOnlyArray<Transform<any>> {
return mapTransforms(this._transforms, animatedNode =>
animatedNode.__getValue(),
);
}

__getAnimatedValue(): $ReadOnlyArray<Object> {
return this._get(animatedNode => animatedNode.__getAnimatedValue());
__getAnimatedValue(): $ReadOnlyArray<Transform<any>> {
return mapTransforms(this._transforms, animatedNode =>
animatedNode.__getAnimatedValue(),
);
}

__attach(): void {
Expand Down Expand Up @@ -83,6 +96,9 @@ export default class AnimatedTransform extends AnimatedWithChildren {
transConfigs.push({
type: 'static',
property: key,
/* $FlowFixMe[incompatible-call] - `value` can be an array or an
object. This is not currently handled by `transformDataType`.
Migrating to `TransformObject` might solve this. */
value: NativeAnimatedHelper.transformDataType(value),
});
}
Expand All @@ -95,36 +111,36 @@ export default class AnimatedTransform extends AnimatedWithChildren {
transforms: transConfigs,
};
}
}

_get(getter: AnimatedNode => any): $ReadOnlyArray<Object> {
return this._transforms.map(transform => {
const result: {[string]: any} = {};
for (const key in transform) {
const value = transform[key];
if (value instanceof AnimatedNode) {
result[key] = getter(value);
} else if (Array.isArray(value)) {
result[key] = value.map(element => {
if (element instanceof AnimatedNode) {
return getter(element);
} else {
return element;
}
});
} else if (typeof value === 'object') {
result[key] = {};
for (const [nestedKey, nestedValue] of Object.entries(value)) {
if (nestedValue instanceof AnimatedNode) {
result[key][nestedKey] = getter(nestedValue);
} else {
result[key][nestedKey] = nestedValue;
}
}
} else {
result[key] = value;
function mapTransforms<T>(
transforms: $ReadOnlyArray<Transform<>>,
mapFunction: AnimatedNode => T,
): $ReadOnlyArray<Transform<T>> {
return transforms.map(transform => {
const result: Transform<T> = {};
for (const key in transform) {
const value = transform[key];
if (value instanceof AnimatedNode) {
result[key] = mapFunction(value);
} else if (Array.isArray(value)) {
result[key] = value.map(element =>
element instanceof AnimatedNode ? mapFunction(element) : element,
);
} else if (typeof value === 'object') {
const object: {[string]: number | string | T} = {};
for (const propertyName in value) {
const propertyValue = value[propertyName];
object[propertyName] =
propertyValue instanceof AnimatedNode
? mapFunction(propertyValue)
: propertyValue;
}
result[key] = object;
} else {
result[key] = value;
}
return result;
});
}
}
return result;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -1118,16 +1118,23 @@ exports[`public API should not change unintentionally Libraries/Animated/nodes/A
`;

exports[`public API should not change unintentionally Libraries/Animated/nodes/AnimatedTransform.js 1`] = `
"declare export default class AnimatedTransform extends AnimatedWithChildren {
_transforms: $ReadOnlyArray<Object>;
constructor(transforms: $ReadOnlyArray<Object>): void;
"type Transform<T = AnimatedNode> = {
[string]:
| number
| string
| T
| $ReadOnlyArray<number | string | T>
| { [string]: number | string | T },
};
declare export default class AnimatedTransform extends AnimatedWithChildren {
_transforms: $ReadOnlyArray<Transform<>>;
constructor(transforms: $ReadOnlyArray<Transform<>>): void;
__makeNative(platformConfig: ?PlatformConfig): void;
__getValue(): $ReadOnlyArray<Object>;
__getAnimatedValue(): $ReadOnlyArray<Object>;
__getValue(): $ReadOnlyArray<Transform<any>>;
__getAnimatedValue(): $ReadOnlyArray<Transform<any>>;
__attach(): void;
__detach(): void;
__getNativeConfig(): any;
_get(getter: (AnimatedNode) => any): $ReadOnlyArray<Object>;
}
"
`;
Expand Down
Loading