Skip to content

Commit b3fe06b

Browse files
yungstersfacebook-github-bot
authored andcommitted
Animated: Change onEnd to Private Property (#46721)
Summary: Pull Request resolved: #46721 Currently, the `Animation` class (from Animated) defines an unofficially protected `__onEnd` property that subclasses are expected to populate. However, subclasses are expected to *not* invoke `__onEnd`, but instead call `__debouncedOnEnd`. In order to make this code less fragile, this commit refactors the `Animation` class and its subclasses so that `onEnd` is stored in a private property and initialized by the `start` method in the `Animation` parent class. Now, the only way to invoke the `onEnd` callback is by calling `__debouncedOnEnd`. The subclasses have been adjusted to call the `start` superclass method to initialize this. Changelog: [General][Changed] - The `Animation` superclass no longer exposes `__onEnd` as a property. Subclasses must instead invoke `super.start(…)` in their `start()` implementation. Reviewed By: javache Differential Revision: D63572063 fbshipit-source-id: 221153ca890c67c59713d607032536f57d7a5b0a
1 parent fd35530 commit b3fe06b

5 files changed

Lines changed: 13 additions & 8 deletions

File tree

‎packages/react-native/Libraries/Animated/animations/Animation.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ let startNativeAnimationNextId = 1;
3636
// Once an animation has been stopped or finished its course, it will
3737
// not be reused.
3838
export default class Animation {
39+
#onEnd: ?EndCallback;
40+
3941
__active: boolean;
4042
__isInteraction: boolean;
41-
__onEnd: ?EndCallback;
4243
__iterations: number;
4344
__isLooping: ?boolean;
4445

@@ -50,7 +51,9 @@ export default class Animation {
5051
onEnd: ?EndCallback,
5152
previousAnimation: ?Animation,
5253
animatedValue: AnimatedValue,
53-
): void {}
54+
): void {
55+
this.#onEnd = onEnd;
56+
}
5457

5558
stop(): void {
5659
if (this._nativeId) {
@@ -66,8 +69,8 @@ export default class Animation {
6669

6770
// Helper function for subclasses to make sure onEnd is only called once.
6871
__debouncedOnEnd(result: EndResult): void {
69-
const onEnd = this.__onEnd;
70-
this.__onEnd = null;
72+
const onEnd = this.#onEnd;
73+
this.#onEnd = null;
7174
onEnd && onEnd(result);
7275
}
7376

‎packages/react-native/Libraries/Animated/animations/DecayAnimation.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ export default class DecayAnimation extends Animation {
7979
previousAnimation: ?Animation,
8080
animatedValue: AnimatedValue,
8181
): void {
82+
super.start(fromValue, onUpdate, onEnd, previousAnimation, animatedValue);
83+
8284
this.__active = true;
8385
this._lastValue = fromValue;
8486
this._fromValue = fromValue;
8587
this._onUpdate = onUpdate;
86-
this.__onEnd = onEnd;
8788
this._startTime = Date.now();
8889

8990
if (!this._useNativeDriver && animatedValue.__isNative === true) {

‎packages/react-native/Libraries/Animated/animations/SpringAnimation.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,13 @@ export default class SpringAnimation extends Animation {
202202
previousAnimation: ?Animation,
203203
animatedValue: AnimatedValue,
204204
): void {
205+
super.start(fromValue, onUpdate, onEnd, previousAnimation, animatedValue);
206+
205207
this.__active = true;
206208
this._startPosition = fromValue;
207209
this._lastPosition = this._startPosition;
208210

209211
this._onUpdate = onUpdate;
210-
this.__onEnd = onEnd;
211212
this._lastTime = Date.now();
212213
this._frameTime = 0.0;
213214

‎packages/react-native/Libraries/Animated/animations/TimingAnimation.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ export default class TimingAnimation extends Animation {
107107
previousAnimation: ?Animation,
108108
animatedValue: AnimatedValue,
109109
): void {
110+
super.start(fromValue, onUpdate, onEnd, previousAnimation, animatedValue);
111+
110112
this.__active = true;
111113
this._fromValue = fromValue;
112114
this._onUpdate = onUpdate;
113-
this.__onEnd = onEnd;
114115

115116
const start = () => {
116117
if (!this._useNativeDriver && animatedValue.__isNative === true) {

‎packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ export type AnimationConfig = {
427427
declare export default class Animation {
428428
__active: boolean;
429429
__isInteraction: boolean;
430-
__onEnd: ?EndCallback;
431430
__iterations: number;
432431
__isLooping: ?boolean;
433432
_nativeId: number;

0 commit comments

Comments
 (0)