Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error when passing easing from 'react-native' instead of 'reanimated' #5639

Merged
merged 36 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9c4e836
Add a good error log
Feb 5, 2024
6b52e25
Refactor
Feb 5, 2024
bf2343c
Fix
Feb 5, 2024
6d544f4
Merge branch 'main' into acynk/no-rn-easing
Feb 5, 2024
949854d
Fix
Feb 5, 2024
700a96d
Update src/reanimated2/animation/timing.ts
Latropos Feb 5, 2024
7ffbf34
Improve message
Feb 5, 2024
cc9cb92
Add dev
Feb 7, 2024
db8da1f
Add comment
Feb 8, 2024
bf35466
Self review
Feb 8, 2024
db734de
Better error format
Feb 8, 2024
21ec0d6
Update src/reanimated2/animation/timing.ts
Latropos Feb 20, 2024
97951df
Merge branch 'main' into acynk/no-rn-easing
Feb 20, 2024
5ab4919
Code review
Feb 20, 2024
e978800
Merge branch 'main' into acynk/no-rn-easing
Feb 21, 2024
b552873
Skip checks on web
Feb 22, 2024
5d952e9
Merge branch 'main' into acynk/no-rn-easing
Feb 22, 2024
1b2ed19
Reneme 'isWorklet' into 'isWorkletFunction'
Feb 22, 2024
9664833
Apply suggestions from code review
Latropos Feb 23, 2024
cbe0a2c
fix
Feb 23, 2024
8ca3364
Remove 'bind' check
Mar 4, 2024
8124a3a
Improve formatting
Mar 4, 2024
762e012
Merge branch 'main' into acynk/no-rn-easing
Mar 5, 2024
b2fa1eb
Code review
Mar 5, 2024
0518206
Don't verify easing in keyframe
Mar 5, 2024
e4756c9
Get rid of function name in error \n and improve
Mar 6, 2024
433c93a
revert changes to EmptyExample.tsx
Mar 6, 2024
70c1fbc
Move checks on JS
Mar 6, 2024
e521d4e
Merge branch 'main' into acynk/no-rn-easing
Mar 6, 2024
83a083d
Update src/reanimated2/commonTypes.ts
Latropos Mar 6, 2024
52a3538
code review
Mar 6, 2024
00ac6fb
refactor
Mar 8, 2024
1597003
Merge branch 'main' into acynk/no-rn-easing
Mar 11, 2024
4c298a5
Add worklet check
Mar 11, 2024
959ef09
Update src/reanimated2/animation/util.ts
Latropos Mar 20, 2024
a2c60a0
Update src/reanimated2/animation/timing.ts
Latropos Mar 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/reanimated2/animation/timing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use strict';
import type { EasingFunction, EasingFunctionFactory } from '../Easing';
import { Easing } from '../Easing';
import { defineAnimation, getReduceMotionForAnimation } from './util';
import {
assertEasingIsWorklet,
defineAnimation,
getReduceMotionForAnimation,
} from './util';
import type {
Animation,
AnimationCallback,
Expand Down Expand Up @@ -65,6 +69,10 @@ export const withTiming = function (
): Animation<TimingAnimation> {
'worklet';

if (__DEV__ && userConfig?.easing) {
assertEasingIsWorklet(userConfig?.easing);
Latropos marked this conversation as resolved.
Show resolved Hide resolved
}
Latropos marked this conversation as resolved.
Show resolved Hide resolved

return defineAnimation<TimingAnimation>(toValue, () => {
'worklet';
const config: Required<Omit<TimingConfig, 'reduceMotion'>> = {
Expand Down
26 changes: 23 additions & 3 deletions src/reanimated2/animation/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
toGammaSpace,
toLinearSpace,
} from '../Colors';
import { ReduceMotion } from '../commonTypes';
import { ReduceMotion, isWorkletFunction } from '../commonTypes';
import type {
SharedValue,
AnimatableValue,
Expand All @@ -33,6 +33,7 @@ import {
getRotationMatrix,
} from './transformationMatrix/matrixUtils';
import { isReducedMotion, shouldBeUseWeb } from '../PlatformChecker';
import type { EasingFunction, EasingFunctionFactory } from '../Easing';

let IN_STYLE_UPDATER = false;
const IS_REDUCED_MOTION = isReducedMotion();
Expand All @@ -43,6 +44,27 @@ if (__DEV__ && IS_REDUCED_MOTION) {
);
}

const SHOULD_BE_USE_WEB = shouldBeUseWeb();

export function assertEasingIsWorklet(
easing: EasingFunction | EasingFunctionFactory
) {
Latropos marked this conversation as resolved.
Show resolved Hide resolved
'worklet';
if (_WORKLET) {
// If 'assertEasingIsWorklet' is called on UI runtime then easing is a copy and it is not a worklet
return true;
}
Latropos marked this conversation as resolved.
Show resolved Hide resolved
if (SHOULD_BE_USE_WEB) {
// It is possible to run reanimated on web without plugin, so let's skip this check on web
return true;
Latropos marked this conversation as resolved.
Show resolved Hide resolved
}
if (!isWorkletFunction(easing)) {
throw new Error(
`[Reanimated] The easing function is not a worklet. Are you sure you didn't import it from react-native? `
Latropos marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

export function initialUpdaterRun<T>(updater: () => T) {
IN_STYLE_UPDATER = true;
const result = updater();
Expand Down Expand Up @@ -478,8 +500,6 @@ type AnimationToDecoration<
? Record<string, unknown>
: U | (() => U) | AnimatableValue;

const SHOULD_BE_USE_WEB = shouldBeUseWeb();

export function defineAnimation<
T extends AnimationObject | StyleLayoutAnimation, // type that's supposed to be returned
U extends AnimationObject | StyleLayoutAnimation = T // type that's received
Expand Down
6 changes: 5 additions & 1 deletion src/reanimated2/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ export function isWorkletFunction<
'worklet';
// Since host objects always return true for `in` operator, we have to use dot notation to check if the property exists.
// See https://github.com/facebook/hermes/blob/340726ef8cf666a7cce75bc60b02fa56b3e54560/lib/VM/JSObject.cpp#L1276.
return !!(value as Record<string, unknown>).__workletHash;
return (
typeof value === 'function' &&
'__workletHash' in value &&
!!value.__workletHash
);
Latropos marked this conversation as resolved.
Show resolved Hide resolved
}

export type AnimatedPropsAdapterFunction = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
import type { EasingFunction } from '../../Easing';
import { BaseAnimationBuilder } from './BaseAnimationBuilder';
import type { StyleProps } from '../../commonTypes';
import { assertEasingIsWorklet } from '../../animation/util';

export class ComplexAnimationBuilder extends BaseAnimationBuilder {
easingV?: EasingFunction;
Expand Down Expand Up @@ -40,6 +41,9 @@ export class ComplexAnimationBuilder extends BaseAnimationBuilder {
}

easing(easingFunction: EasingFunction): this {
if (__DEV__) {
assertEasingIsWorklet(easingFunction);
}
this.easingV = easingFunction;
return this;
}
Expand Down
10 changes: 9 additions & 1 deletion src/reanimated2/layoutReanimation/animationBuilder/Keyframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import type {
import type { StyleProps } from '../../commonTypes';
import type { TransformArrayItem } from '../../helperTypes';
import { ReduceMotion } from '../../commonTypes';
import { getReduceMotionFromConfig } from '../../animation/util';
import {
assertEasingIsWorklet,
getReduceMotionFromConfig,
} from '../../animation/util';

interface KeyframePoint {
duration: number;
Expand Down Expand Up @@ -129,6 +132,11 @@ class InnerKeyframe implements IEntryExitAnimationBuilder {
"[Reanimated] Keyframe can contain only that set of properties that were provide with initial values (keyframe 0 or 'from')"
);
}

if (__DEV__ && easing) {
assertEasingIsWorklet(easing);
}

parsedKeyframes[key].push({
duration: getAnimationDuration(key, currentKeyPoint),
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BaseAnimationBuilder } from '../animationBuilder';
import type { EasingFunction } from '../../Easing';
import { Easing } from '../../Easing';
import { withTiming } from '../../animation';
import { assertEasingIsWorklet } from '../../animation/util';

/**
* Layout transitions with a curved animation. You can modify the behavior by chaining methods like `.duration(500)` or `.delay(500)`.
Expand Down Expand Up @@ -36,6 +37,9 @@ export class CurvedTransition
}

easingX(easing: EasingFunction): CurvedTransition {
if (__DEV__) {
assertEasingIsWorklet(easing);
}
this.easingXV = easing;
return this;
}
Expand All @@ -46,6 +50,9 @@ export class CurvedTransition
}

easingY(easing: EasingFunction): CurvedTransition {
if (__DEV__) {
assertEasingIsWorklet(easing);
}
this.easingYV = easing;
return this;
}
Expand All @@ -56,6 +63,9 @@ export class CurvedTransition
}

easingWidth(easing: EasingFunction): CurvedTransition {
if (__DEV__) {
assertEasingIsWorklet(easing);
}
this.easingWidthV = easing;
return this;
}
Expand All @@ -66,6 +76,9 @@ export class CurvedTransition
}

easingHeight(easing: EasingFunction): CurvedTransition {
if (__DEV__) {
assertEasingIsWorklet(easing);
}
this.easingHeightV = easing;
return this;
}
Expand Down