Skip to content

Commit

Permalink
Mark functions as a cacheable
Browse files Browse the repository at this point in the history
  • Loading branch information
piaskowyk committed Nov 25, 2024
1 parent 0efd0c7 commit ef9393e
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 14 deletions.
7 changes: 4 additions & 3 deletions packages/react-native-reanimated/src/animation/clamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
AnimationObject,
ReduceMotion,
} from '../commonTypes';
import type { ClampAnimation } from './commonTypes';
import type { CacheableWorklet, ClampAnimation } from './commonTypes';
import { logger } from '../logger';

type withClampType = <T extends number | string>(
Expand All @@ -22,7 +22,7 @@ type withClampType = <T extends number | string>(
clampedAnimation: T
) => T;

export const withClamp = function <T extends number | string>(
export const withClamp = <withClampType & CacheableWorklet>function <T extends number | string>(
config: { min?: T; max?: T; reduceMotion?: ReduceMotion },
_animationToClamp: AnimationObject<T> | (() => AnimationObject<T>)
): Animation<ClampAnimation> {
Expand Down Expand Up @@ -131,4 +131,5 @@ export const withClamp = function <T extends number | string>(
};
}
);
} as withClampType;
};
withClamp.cacheable = true;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
} from './utils';
import { rigidDecay } from './rigidDecay';
import { ReanimatedError } from '../../errors';
import type { CacheableWorklet } from '../commonTypes';

export type WithDecayConfig = DecayConfig;

Expand Down Expand Up @@ -121,4 +122,5 @@ export const withDecay = function (
reduceMotion: getReduceMotionForAnimation(config.reduceMotion),
} as DecayAnimation;
});
} as unknown as withDecayType;
} as unknown as withDecayType & CacheableWorklet;
withDecay.cacheable = true;
7 changes: 4 additions & 3 deletions packages/react-native-reanimated/src/animation/delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
AnimationObject,
ReduceMotion,
} from '../commonTypes';
import type { DelayAnimation } from './commonTypes';
import type { CacheableWorklet, DelayAnimation } from './commonTypes';

// TODO TYPESCRIPT This is a temporary type to get rid of .d.ts file.
type withDelayType = <T extends AnimatableValue>(
Expand All @@ -29,7 +29,7 @@ type withDelayType = <T extends AnimatableValue>(
* which holds the current state of the animation.
* @see https://docs.swmansion.com/react-native-reanimated/docs/animations/withDelay
*/
export const withDelay = function <T extends AnimationObject>(
export const withDelay = <withDelayType & CacheableWorklet>function <T extends AnimationObject>(
delayMs: number,
_nextAnimation: T | (() => T),
reduceMotion?: ReduceMotion
Expand Down Expand Up @@ -114,4 +114,5 @@ export const withDelay = function <T extends AnimationObject>(
};
}
);
} as withDelayType;
};
withDelay.cacheable = true;
7 changes: 4 additions & 3 deletions packages/react-native-reanimated/src/animation/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
AnimationObject,
ReduceMotion,
} from '../commonTypes';
import type { RepeatAnimation } from './commonTypes';
import type { CacheableWorklet, RepeatAnimation } from './commonTypes';

// TODO TYPESCRIPT This is a temporary type to get rid of .d.ts file.
type withRepeatType = <T extends AnimatableValue>(
Expand Down Expand Up @@ -36,7 +36,7 @@ type withRepeatType = <T extends AnimatableValue>(
* which holds the current state of the animation.
* @see https://docs.swmansion.com/react-native-reanimated/docs/animations/withRepeat
*/
export const withRepeat = function <T extends AnimationObject>(
export const withRepeat = <withRepeatType & CacheableWorklet>function <T extends AnimationObject>(
_nextAnimation: T | (() => T),
numberOfReps = 2,
reverse = false,
Expand Down Expand Up @@ -141,4 +141,5 @@ export const withRepeat = function <T extends AnimationObject>(
};
}
);
} as withRepeatType;
};
withRepeat.cacheable = true;
1 change: 1 addition & 0 deletions packages/react-native-reanimated/src/animation/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,4 @@ export function withSequence(
}
);
}
withSequence.cacheable = true;
8 changes: 5 additions & 3 deletions packages/react-native-reanimated/src/animation/spring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
scaleZetaToMatchClamps,
checkIfConfigIsValid,
} from './springUtils';
import type { CacheableWorklet } from './commonTypes';

// TODO TYPESCRIPT This is a temporary type to get rid of .d.ts file.
type withSpringType = <T extends AnimatableValue>(
Expand All @@ -43,11 +44,11 @@ type withSpringType = <T extends AnimatableValue>(
* which holds the current state of the animation
* @see https://docs.swmansion.com/react-native-reanimated/docs/animations/withSpring
*/
export const withSpring = ((
export const withSpring = <withSpringType & CacheableWorklet>function (
toValue: AnimatableValue,
userConfig?: SpringConfig,
callback?: AnimationCallback
): Animation<SpringAnimation> => {
): Animation<SpringAnimation> {
'worklet';

return defineAnimation<SpringAnimation>(toValue, () => {
Expand Down Expand Up @@ -245,4 +246,5 @@ export const withSpring = ((
reduceMotion: getReduceMotionForAnimation(config.reduceMotion),
} as SpringAnimation;
});
}) as withSpringType;
};
withSpring.cacheable = true;
2 changes: 1 addition & 1 deletion packages/react-native-reanimated/src/animation/timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type withTimingType = <T extends AnimatableValue>(
* which holds the current state of the animation.
* @see https://docs.swmansion.com/react-native-reanimated/docs/animations/withTiming
*/
export const withTiming = <CacheableWorklet & withTimingType>function (
export const withTiming = <withTimingType & CacheableWorklet>function (
toValue: AnimatableValue,
userConfig?: TimingConfig,
callback?: AnimationCallback
Expand Down

0 comments on commit ef9393e

Please sign in to comment.