Skip to content

Commit 0a68763

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Add explicit useNativeDriver: false to callsites
Summary: In order to cleanup the callsites that are not using Animated's native driver, we are going to make useNativeDriver a required option so people have to think about whether they want the native driver or not. I made this change by changing [Animated.js](https://fburl.com/ritcebri) to have this animation config type: ``` export type AnimationConfig = { isInteraction?: boolean, useNativeDriver: true, onComplete?: ?EndCallback, iterations?: number, }; ``` This causes Flow to error anywhere where useNativeDriver isn't set or where it is set to false. I then used these Flow errors to codemod the callsites. I got the location of the Flow errors by running: ``` flow status --strip-root --json --message-width=0 | jq '.errors | [.[].extra | .[].message | .[].loc | objects | {source: .source, start: .start, end: .end}]' ``` And then ran this codemod: ``` const json = JSON.parse('JSON RESULT FROM FLOW'); const fileLookup = new Map(); json.forEach(item => { if (!fileLookup.has(item.source)) { fileLookup.set(item.source, []); } fileLookup.get(item.source).push(item); }); export default function transformer(file, api) { const j = api.jscodeshift; const filePath = file.path; if (!fileLookup.has(filePath)) { return; } const locationInfo = fileLookup.get(filePath); return j(file.source) .find(j.ObjectExpression) .forEach(path => { if ( path.node.properties.some( property => property != null && property.key != null && property.key.name === 'useNativeDriver', ) ) { return; } const hasErrorOnLine = locationInfo.some( singleLocationInfo => singleLocationInfo.start.line === path.node.loc.start.line && Math.abs( singleLocationInfo.start.column - path.node.loc.start.column, ) <= 2, ); if (!hasErrorOnLine) { return; } path.node.properties.push( j.property( 'init', j.identifier('useNativeDriver'), j.booleanLiteral(false), ), ); }) .toSource(); } export const parser = 'flow'; ``` ``` yarn jscodeshift --parser=flow --transform addUseNativeDriver.js RKJSModules react-native-github ``` Followed up with ``` hg status -n --change . | xargs js1 prettier ``` Reviewed By: mdvacca Differential Revision: D16611291 fbshipit-source-id: 1157587416ec7603d1a59e1fad6a821f1f57b952
1 parent 2a8c188 commit 0a68763

File tree

9 files changed

+151
-35
lines changed

9 files changed

+151
-35
lines changed

Libraries/Animated/src/AnimatedImplementation.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,12 @@ const parallel = function(
403403

404404
const delay = function(time: number): CompositeAnimation {
405405
// Would be nice to make a specialized implementation
406-
return timing(new AnimatedValue(0), {toValue: 0, delay: time, duration: 0});
406+
return timing(new AnimatedValue(0), {
407+
toValue: 0,
408+
delay: time,
409+
duration: 0,
410+
useNativeDriver: false,
411+
});
407412
};
408413

409414
const stagger = function(

RNTester/js/examples/Animated/AnimatedExample.js

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ exports.examples = [
5757
// Uses easing functions
5858
this.state.fadeAnim, // The value to drive
5959
{
60-
toValue: 1, // Target
61-
duration: 2000, // Configuration
60+
// Target
61+
toValue: 1,
62+
63+
// Configuration
64+
duration: 2000,
65+
66+
useNativeDriver: false,
6267
},
6368
).start(); // Don't forget start!
6469
}
@@ -121,10 +126,19 @@ exports.examples = [
121126
<RNTesterButton
122127
onPress={() => {
123128
Animated.spring(this.anim, {
124-
toValue: 0, // Returns to the start
125-
velocity: 3, // Velocity makes it move
126-
tension: -10, // Slow
127-
friction: 1, // Oscillate a lot
129+
// Returns to the start
130+
toValue: 0,
131+
132+
// Velocity makes it move
133+
velocity: 3,
134+
135+
// Slow
136+
tension: -10,
137+
138+
// Oscillate a lot
139+
friction: 1,
140+
141+
useNativeDriver: false,
128142
}).start();
129143
}}>
130144
Press to Fling it!
@@ -182,18 +196,35 @@ exports.examples = [
182196
timing(this.anims[0], {
183197
toValue: 200,
184198
easing: Easing.linear,
199+
useNativeDriver: false,
185200
}),
186201
Animated.delay(400), // Use with sequence
187202
timing(this.anims[0], {
188203
toValue: 0,
189-
easing: Easing.elastic(2), // Springy
204+
205+
// Springy
206+
easing: Easing.elastic(2),
207+
208+
useNativeDriver: false,
190209
}),
191210
Animated.delay(400),
192211
Animated.stagger(
193212
200,
194213
this.anims
195-
.map(anim => timing(anim, {toValue: 200}))
196-
.concat(this.anims.map(anim => timing(anim, {toValue: 0}))),
214+
.map(anim =>
215+
timing(anim, {
216+
toValue: 200,
217+
useNativeDriver: false,
218+
}),
219+
)
220+
.concat(
221+
this.anims.map(anim =>
222+
timing(anim, {
223+
toValue: 0,
224+
useNativeDriver: false,
225+
}),
226+
),
227+
),
197228
),
198229
Animated.delay(400),
199230
Animated.parallel(
@@ -206,6 +237,7 @@ exports.examples = [
206237
toValue: 320,
207238
easing,
208239
duration: 3000,
240+
useNativeDriver: false,
209241
}),
210242
),
211243
),
@@ -215,8 +247,12 @@ exports.examples = [
215247
this.anims.map(anim =>
216248
timing(anim, {
217249
toValue: 0,
218-
easing: Easing.bounce, // Like a ball
250+
251+
// Like a ball
252+
easing: Easing.bounce,
253+
219254
duration: 2000,
255+
useNativeDriver: false,
220256
}),
221257
),
222258
),
@@ -250,10 +286,19 @@ exports.examples = [
250286
<RNTesterButton
251287
onPress={() => {
252288
Animated.spring(this.anim, {
253-
toValue: 0, // Returns to the start
254-
velocity: 3, // Velocity makes it move
255-
tension: -10, // Slow
256-
friction: 1, // Oscillate a lot
289+
// Returns to the start
290+
toValue: 0,
291+
292+
// Velocity makes it move
293+
velocity: 3,
294+
295+
// Slow
296+
tension: -10,
297+
298+
// Oscillate a lot
299+
friction: 1,
300+
301+
useNativeDriver: false,
257302
}).start();
258303
}}>
259304
Press to Spin it!

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExApp.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ class Circle extends React.Component<any, any> {
4848
this.props.onMove && this.props.onMove(value);
4949
});
5050
Animated.spring(this.state.pop, {
51-
toValue: 1, // Pop to larger size. (step2b: uncomment)
52-
...config, // Reuse config for convenient consistency (step2b: uncomment)
51+
// Pop to larger size. (step2b: uncomment)
52+
toValue: 1,
53+
54+
// Reuse config for convenient consistency (step2b: uncomment)
55+
...config,
56+
57+
useNativeDriver: false,
5358
}).start();
5459
this.setState(
5560
{
@@ -61,8 +66,11 @@ class Circle extends React.Component<any, any> {
6166
onPanResponderRelease: (e, gestureState) => {
6267
LayoutAnimation.easeInEaseOut(); // @flowfixme animates layout update as one batch (step3: uncomment)
6368
Animated.spring(this.state.pop, {
64-
toValue: 0, // Pop back to 0 (step2c: uncomment)
69+
// Pop back to 0 (step2c: uncomment)
70+
toValue: 0,
71+
6572
...config,
73+
useNativeDriver: false,
6674
}).start();
6775
this.setState({panResponder: undefined});
6876
this.props.onMove &&
@@ -185,15 +193,23 @@ class Circle extends React.Component<any, any> {
185193
_toggleIsActive(velocity) {
186194
const config = {tension: 30, friction: 7};
187195
if (this.state.isActive) {
188-
Animated.spring(this.props.openVal, {toValue: 0, ...config}).start(() => {
196+
Animated.spring(this.props.openVal, {
197+
toValue: 0,
198+
...config,
199+
useNativeDriver: false,
200+
}).start(() => {
189201
// (step4: uncomment)
190202
this.setState({isActive: false}, this.props.onDeactivate);
191203
}); // (step4: uncomment)
192204
} else {
193205
this.props.onActivate();
194206
this.setState({isActive: true, panResponder: undefined}, () => {
195207
// this.props.openVal.setValue(1); // (step4: comment)
196-
Animated.spring(this.props.openVal, {toValue: 1, ...config}).start(); // (step4: uncomment)
208+
Animated.spring(this.props.openVal, {
209+
toValue: 1,
210+
...config,
211+
useNativeDriver: false,
212+
}).start(); // (step4: uncomment)
197213
});
198214
}
199215
}

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExBobble.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ class AnExBobble extends React.Component<Object, any> {
4242
if (this.state.selectedBobble !== null) {
4343
const restSpot = BOBBLE_SPOTS[this.state.selectedBobble];
4444
Animated.spring(this.state.bobbles[this.state.selectedBobble], {
45-
toValue: restSpot, // return previously selected bobble to rest position
45+
// return previously selected bobble to rest position
46+
toValue: restSpot,
47+
48+
useNativeDriver: false,
4649
}).start();
4750
}
4851
if (newSelected !== null && newSelected !== 0) {
4952
Animated.spring(this.state.bobbles[newSelected], {
50-
toValue: this.state.bobbles[0], // newly selected should track the selector
53+
// newly selected should track the selector
54+
toValue: this.state.bobbles[0],
55+
56+
useNativeDriver: false,
5157
}).start();
5258
}
5359
this.state.selectedBobble = newSelected;
@@ -56,7 +62,10 @@ class AnExBobble extends React.Component<Object, any> {
5662
const releaseBobble = () => {
5763
this.state.bobbles.forEach((bobble, i) => {
5864
Animated.spring(bobble, {
59-
toValue: {x: 0, y: 0}, // all bobbles return to zero
65+
// all bobbles return to zero
66+
toValue: {x: 0, y: 0},
67+
68+
useNativeDriver: false,
6069
}).start();
6170
});
6271
};
@@ -65,8 +74,13 @@ class AnExBobble extends React.Component<Object, any> {
6574
onPanResponderGrant: () => {
6675
BOBBLE_SPOTS.forEach((spot, idx) => {
6776
Animated.spring(this.state.bobbles[idx], {
68-
toValue: spot, // spring each bobble to its spot
69-
friction: 3, // less friction => bouncier
77+
// spring each bobble to its spot
78+
toValue: spot,
79+
80+
// less friction => bouncier
81+
friction: 3,
82+
83+
useNativeDriver: false,
7084
}).start();
7185
});
7286
},

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExChained.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ class AnExChained extends React.Component<Object, any> {
2525
const sticker = new Animated.ValueXY();
2626
Animated.spring(sticker, {
2727
...stickerConfig,
28-
toValue: this.state.stickers[i], // Animated toValue's are tracked
28+
29+
// Animated toValue's are tracked
30+
toValue: this.state.stickers[i],
31+
32+
useNativeDriver: false,
2933
}).start();
3034
this.state.stickers.push(sticker); // push on the followers
3135
}
@@ -36,10 +40,15 @@ class AnExChained extends React.Component<Object, any> {
3640
Animated.decay(this.state.stickers[0], {
3741
// coast to a stop
3842
velocity: {x: gestureState.vx, y: gestureState.vy},
43+
3944
deceleration: 0.997,
45+
useNativeDriver: false,
4046
}),
4147
Animated.spring(this.state.stickers[0], {
42-
toValue: {x: 0, y: 0}, // return to start
48+
// return to start
49+
toValue: {x: 0, y: 0},
50+
51+
useNativeDriver: false,
4352
}),
4453
]).start();
4554
};

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExSet.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class AnExSet extends React.Component<Object, any> {
8282
inputRange: [0, 300], // and interpolate pixel distance
8383
outputRange: [1, 0], // to a fraction.
8484
}),
85+
86+
useNativeDriver: false,
8587
}).start();
8688
},
8789
onPanResponderMove: Animated.event(
@@ -92,7 +94,10 @@ class AnExSet extends React.Component<Object, any> {
9294
this.props.onDismiss(gestureState.vy); // delegates dismiss action to parent
9395
} else {
9496
Animated.spring(this.props.openVal, {
95-
toValue: 1, // animate back open if released early
97+
// animate back open if released early
98+
toValue: 1,
99+
100+
useNativeDriver: false,
96101
}).start();
97102
}
98103
},

RNTester/js/examples/Animated/AnimatedGratuitousApp/AnExTilt.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ class AnExTilt extends React.Component<Object, any> {
2929
inputRange: [-300, 0, 300], // pan is in pixels
3030
outputRange: [0, 1, 0], // goes to zero at both edges
3131
}),
32-
duration: 0, // direct tracking
32+
33+
// direct tracking
34+
duration: 0,
35+
36+
useNativeDriver: false,
3337
}).start();
3438
},
3539
onPanResponderMove: Animated.event(
@@ -43,18 +47,26 @@ class AnExTilt extends React.Component<Object, any> {
4347
toValue = -500;
4448
}
4549
Animated.spring(this.state.panX, {
46-
toValue, // animate back to center or off screen
47-
velocity: gestureState.vx, // maintain gesture velocity
50+
// animate back to center or off screen
51+
toValue,
52+
53+
// maintain gesture velocity
54+
velocity: gestureState.vx,
55+
4856
tension: 10,
4957
friction: 3,
58+
useNativeDriver: false,
5059
}).start();
5160
this.state.panX.removeAllListeners();
5261
const id = this.state.panX.addListener(({value}) => {
5362
// listen until offscreen
5463
if (Math.abs(value) > 400) {
5564
this.state.panX.removeListener(id); // offscreen, so stop listening
5665
Animated.timing(this.state.opacity, {
57-
toValue: 1, // Fade back in. This unlinks it from tracking this.state.panX
66+
// Fade back in. This unlinks it from tracking this.state.panX
67+
toValue: 1,
68+
69+
useNativeDriver: false,
5870
}).start();
5971
this.state.panX.setValue(0); // Note: stops the spring animation
6072
toValue !== 0 && this._startBurnsZoom();
@@ -67,8 +79,13 @@ class AnExTilt extends React.Component<Object, any> {
6779
_startBurnsZoom() {
6880
this.state.burns.setValue(1); // reset to beginning
6981
Animated.decay(this.state.burns, {
70-
velocity: 1, // subtle zoom
71-
deceleration: 0.9999, // slow decay
82+
// subtle zoom
83+
velocity: 1,
84+
85+
// slow decay
86+
deceleration: 0.9999,
87+
88+
useNativeDriver: false,
7289
}).start();
7390
}
7491

RNTester/js/examples/Touchable/TouchableExample.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,11 @@ exports.examples = [
494494
platform: 'android',
495495
render: function() {
496496
const mScale = new Animated.Value(1);
497-
Animated.timing(mScale, {toValue: 0.3, duration: 1000}).start();
497+
Animated.timing(mScale, {
498+
toValue: 0.3,
499+
duration: 1000,
500+
useNativeDriver: false,
501+
}).start();
498502
const style = {
499503
backgroundColor: 'rgb(180, 64, 119)',
500504
width: 200,

RNTester/js/examples/Transform/TransformExample.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Flip extends React.Component<{}, $FlowFixMeState> {
2727
Animated.timing(this.state.theta, {
2828
toValue: 360,
2929
duration: 5000,
30+
useNativeDriver: false,
3031
}).start(this._animate);
3132
};
3233

0 commit comments

Comments
 (0)