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

Commit 9445e55

Browse files
committed
fix: add memoization of spring nodes
1 parent 95fe56e commit 9445e55

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
@@ -87,6 +87,8 @@ const {
8787
onChange,
8888
set,
8989
spring,
90+
// @ts-ignore
91+
proc,
9092
sub,
9193
timing,
9294
startClock,
@@ -96,6 +98,79 @@ const {
9698
Value,
9799
} = Animated;
98100

101+
const springHelper = proc(
102+
(
103+
finished: Animated.Value<number>,
104+
velocity: Animated.Value<number>,
105+
position: Animated.Value<number>,
106+
time: Animated.Value<number>,
107+
prevPosition: Animated.Value<number>,
108+
toValue: Animated.Adaptable<number>,
109+
damping: Animated.Adaptable<number>,
110+
mass: Animated.Adaptable<number>,
111+
stiffness: Animated.Adaptable<number>,
112+
overshootClamping: Animated.Adaptable<number>,
113+
restSpeedThreshold: Animated.Adaptable<number>,
114+
restDisplacementThreshold: Animated.Adaptable<number>,
115+
clock: Animated.Clock
116+
) =>
117+
spring(
118+
clock,
119+
{
120+
finished,
121+
velocity,
122+
position,
123+
time,
124+
// @ts-ignore
125+
prevPosition,
126+
},
127+
{
128+
toValue,
129+
damping,
130+
mass,
131+
stiffness,
132+
overshootClamping,
133+
restDisplacementThreshold,
134+
restSpeedThreshold,
135+
}
136+
)
137+
);
138+
139+
function memoizedSpring(
140+
clock: Animated.Clock,
141+
state: {
142+
finished: Animated.Value<number>;
143+
velocity: Animated.Value<number>;
144+
position: Animated.Value<number>;
145+
time: Animated.Value<number>;
146+
},
147+
config: {
148+
toValue: Animated.Adaptable<number>;
149+
damping: Animated.Adaptable<number>;
150+
mass: Animated.Adaptable<number>;
151+
stiffness: Animated.Adaptable<number>;
152+
overshootClamping: Animated.Adaptable<boolean>;
153+
restSpeedThreshold: Animated.Adaptable<number>;
154+
restDisplacementThreshold: Animated.Adaptable<number>;
155+
}
156+
) {
157+
return springHelper(
158+
state.finished,
159+
state.velocity,
160+
state.position,
161+
state.time,
162+
new Value(0),
163+
config.toValue,
164+
config.damping,
165+
config.mass,
166+
config.stiffness,
167+
config.overshootClamping,
168+
config.restSpeedThreshold,
169+
config.restDisplacementThreshold,
170+
clock
171+
);
172+
}
173+
99174
export default class Card extends React.Component<Props> {
100175
static defaultProps = {
101176
overlayEnabled: Platform.OS !== 'ios',
@@ -216,7 +291,7 @@ export default class Card extends React.Component<Props> {
216291
cond(
217292
eq(isVisible, 1),
218293
openingSpec.timing === 'spring'
219-
? spring(
294+
? memoizedSpring(
220295
this.clock,
221296
{ ...this.transitionState, velocity: this.transitionVelocity },
222297
{ ...openingSpec.config, toValue: this.toValue }
@@ -227,7 +302,7 @@ export default class Card extends React.Component<Props> {
227302
{ ...openingSpec.config, toValue: this.toValue }
228303
),
229304
closingSpec.timing === 'spring'
230-
? spring(
305+
? memoizedSpring(
231306
this.clock,
232307
{ ...this.transitionState, velocity: this.transitionVelocity },
233308
{ ...closingSpec.config, toValue: this.toValue }

0 commit comments

Comments
 (0)