Skip to content
This repository was archived by the owner on Feb 25, 2020. It is now read-only.

Commit 40a415b

Browse files
committed
fix: add memoization of spring nodes
1 parent dd4e0c3 commit 40a415b

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

jest-setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ jest.mock('react-native-reanimated', () => {
9797
timing: NOOP,
9898
spring: NOOP,
9999

100+
proc: a => a,
101+
100102
useCode: NOOP,
101103
},
102104

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"react": "*",
8282
"react-native": "*",
8383
"react-native-gesture-handler": "^1.0.0",
84-
"react-native-reanimated": "^1.0.0",
84+
"react-native-reanimated": "^1.3.0-alpha",
8585
"react-native-screens": "^1.0.0 || ^1.0.0-alpha"
8686
},
8787
"jest": {

src/views/Stack/Card.tsx

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ const {
8585
onChange,
8686
set,
8787
spring,
88+
// @ts-ignore
89+
proc,
8890
sub,
8991
timing,
9092
startClock,
@@ -94,6 +96,79 @@ const {
9496
Value,
9597
} = Animated;
9698

99+
const springHelper = proc(
100+
(
101+
finished: Animated.Value<number>,
102+
velocity: Animated.Value<number>,
103+
position: Animated.Value<number>,
104+
time: Animated.Value<number>,
105+
prevPosition: Animated.Value<number>,
106+
toValue: Animated.Adaptable<number>,
107+
damping: Animated.Adaptable<number>,
108+
mass: Animated.Adaptable<number>,
109+
stiffness: Animated.Adaptable<number>,
110+
overshootClamping: Animated.Adaptable<number>,
111+
restSpeedThreshold: Animated.Adaptable<number>,
112+
restDisplacementThreshold: Animated.Adaptable<number>,
113+
clock: Animated.Clock
114+
) =>
115+
spring(
116+
clock,
117+
{
118+
finished,
119+
velocity,
120+
position,
121+
time,
122+
// @ts-ignore
123+
prevPosition,
124+
},
125+
{
126+
toValue,
127+
damping,
128+
mass,
129+
stiffness,
130+
overshootClamping,
131+
restDisplacementThreshold,
132+
restSpeedThreshold,
133+
}
134+
)
135+
);
136+
137+
function memoizedSpring(
138+
clock: Animated.Clock,
139+
state: {
140+
finished: Animated.Value<number>;
141+
velocity: Animated.Value<number>;
142+
position: Animated.Value<number>;
143+
time: Animated.Value<number>;
144+
},
145+
config: {
146+
toValue: Animated.Adaptable<number>;
147+
damping: Animated.Adaptable<number>;
148+
mass: Animated.Adaptable<number>;
149+
stiffness: Animated.Adaptable<number>;
150+
overshootClamping: Animated.Adaptable<boolean>;
151+
restSpeedThreshold: Animated.Adaptable<number>;
152+
restDisplacementThreshold: Animated.Adaptable<number>;
153+
}
154+
) {
155+
return springHelper(
156+
state.finished,
157+
state.velocity,
158+
state.position,
159+
state.time,
160+
new Value(0),
161+
config.toValue,
162+
config.damping,
163+
config.mass,
164+
config.stiffness,
165+
config.overshootClamping,
166+
config.restSpeedThreshold,
167+
config.restDisplacementThreshold,
168+
clock
169+
);
170+
}
171+
97172
export default class Card extends React.Component<Props> {
98173
static defaultProps = {
99174
overlayEnabled: Platform.OS !== 'ios',
@@ -203,7 +278,7 @@ export default class Card extends React.Component<Props> {
203278
cond(
204279
eq(isVisible, 1),
205280
openingSpec.timing === 'spring'
206-
? spring(
281+
? memoizedSpring(
207282
this.clock,
208283
{ ...this.transitionState, velocity: this.transitionVelocity },
209284
{ ...openingSpec.config, toValue: this.toValue }
@@ -214,7 +289,7 @@ export default class Card extends React.Component<Props> {
214289
{ ...openingSpec.config, toValue: this.toValue }
215290
),
216291
closingSpec.timing === 'spring'
217-
? spring(
292+
? memoizedSpring(
218293
this.clock,
219294
{ ...this.transitionState, velocity: this.transitionVelocity },
220295
{ ...closingSpec.config, toValue: this.toValue }

0 commit comments

Comments
 (0)