Skip to content

Commit 8d32a45

Browse files
authored
fix: Fix Tried to synchronously call a non-worklet function (#507)
## Description It looks like reanimated's babel plugin in old reanimated versions didn't support file-level `'worklet'` directive. To fix the issue, I had to remove all file-level directives and move them to particular functions.
1 parent 2669fea commit 8d32a45

File tree

14 files changed

+88
-42
lines changed

14 files changed

+88
-42
lines changed

packages/react-native-sortables/src/constants/layoutAnimations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
'worklet';
21
import { type LayoutAnimation, withTiming } from 'react-native-reanimated';
32

43
const ITEM_LAYOUT_ANIMATION_DURATION = 300;
54

65
export const SortableItemExiting = (): LayoutAnimation => {
6+
'worklet';
77
const animations = {
88
opacity: withTiming(0, {
99
duration: ITEM_LAYOUT_ANIMATION_DURATION
@@ -27,6 +27,7 @@ export const SortableItemExiting = (): LayoutAnimation => {
2727
};
2828

2929
export const SortableItemEntering = (): LayoutAnimation => {
30+
'worklet';
3031
const animations = {
3132
opacity: withTiming(1, {
3233
duration: ITEM_LAYOUT_ANIMATION_DURATION

packages/react-native-sortables/src/integrations/reanimated/utils/animatedTimeout.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'worklet';
21
import { makeMutable } from 'react-native-reanimated';
32

43
import type { AnyFunction } from '../../../helperTypes';
@@ -9,6 +8,7 @@ const TIMEOUT_ID = makeMutable(0);
98
export type AnimatedTimeoutID = number;
109

1110
function removeFromPendingTimeouts(id: AnimatedTimeoutID): void {
11+
'worklet';
1212
PENDING_TIMEOUTS.modify(pendingTimeouts => {
1313
'worklet';
1414
delete pendingTimeouts[id];
@@ -20,6 +20,7 @@ export function setAnimatedTimeout<F extends AnyFunction>(
2020
callback: F,
2121
delay = 0
2222
): AnimatedTimeoutID {
23+
'worklet';
2324
let startTimestamp: number;
2425

2526
const currentId = TIMEOUT_ID.value;
@@ -45,5 +46,6 @@ export function setAnimatedTimeout<F extends AnyFunction>(
4546
}
4647

4748
export function clearAnimatedTimeout(handle: AnimatedTimeoutID): void {
49+
'worklet';
4850
removeFromPendingTimeouts(handle);
4951
}

packages/react-native-sortables/src/integrations/reanimated/utils/interpolation.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'worklet';
2-
31
import { Extrapolation, interpolate } from 'react-native-reanimated';
42

53
import type { Vector } from '../../../types/layout';
@@ -10,6 +8,7 @@ export const interpolateVector = (
108
to: Vector,
119
extrapolation?: Extrapolation
1210
) => {
11+
'worklet';
1312
const inputRange = [0, 1];
1413
const extrapolate = extrapolation ?? Extrapolation.CLAMP;
1514
return {

packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/updates/insert/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'worklet';
21
import type { ItemSizes } from '../../../../../types';
32
import { reorderInsert, resolveDimension } from '../../../../../utils';
43

@@ -28,6 +27,7 @@ const getGroupItemIndex = (
2827
group: Array<string>,
2928
keyToIndex: Record<string, number>
3029
) => {
30+
'worklet';
3131
const key = group[inGroupIndex];
3232
if (key === undefined) return null;
3333
return keyToIndex[key] ?? null;
@@ -38,6 +38,7 @@ export const getTotalGroupSize = (
3838
mainItemSizes: ItemSizes,
3939
gap: number
4040
) => {
41+
'worklet';
4142
const sizesSum = group.reduce(
4243
(total, key) => total + (resolveDimension(mainItemSizes, key) ?? 0),
4344
0
@@ -62,6 +63,7 @@ const getIndexesWhenSwappedToGroupBefore = ({
6263
mainGap,
6364
mainItemSizes
6465
}: ItemGroupSwapProps): SwappedGroupIndexesResult => {
66+
'worklet';
6567
if (groupSizeLimit === Infinity) {
6668
return null;
6769
}
@@ -146,6 +148,7 @@ const getIndexesWhenSwappedToGroupAfter = ({
146148
mainGap,
147149
mainItemSizes
148150
}: ItemGroupSwapProps): SwappedGroupIndexesResult => {
151+
'worklet';
149152
const activeGroup = itemGroups[currentGroupIndex];
150153
if (groupSizeLimit === Infinity || activeGroup === undefined) {
151154
return null;
@@ -227,6 +230,7 @@ const getIndexesWhenSwappedToGroupAfter = ({
227230
export const getSwappedToGroupBeforeIndices = (
228231
props: ItemGroupSwapProps
229232
): ItemGroupSwapResult | null => {
233+
'worklet';
230234
const indexes = getIndexesWhenSwappedToGroupBefore(props);
231235
if (indexes === null) return null;
232236

@@ -247,6 +251,7 @@ export const getSwappedToGroupBeforeIndices = (
247251
export const getSwappedToGroupAfterIndices = (
248252
props: ItemGroupSwapProps
249253
): ItemGroupSwapResult | null => {
254+
'worklet';
250255
const indexes = getIndexesWhenSwappedToGroupAfter(props);
251256
if (indexes === null) return null;
252257

packages/react-native-sortables/src/providers/flex/FlexLayoutProvider/utils/layout.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'worklet';
21
import { IS_WEB } from '../../../../constants';
32
import type {
43
AlignContent,
@@ -25,6 +24,7 @@ const createGroups = (
2524
groups: Array<Array<string>>;
2625
crossAxisGroupSizes: Array<number>;
2726
} => {
27+
'worklet';
2828
const groups: Array<Array<string>> = [];
2929
const crossAxisGroupSizes: Array<number> = [];
3030

@@ -77,6 +77,7 @@ const calculateAlignment = (
7777
totalSize: number;
7878
adjustedGap: number;
7979
} => {
80+
'worklet';
8081
let startOffset = 0;
8182
let adjustedGap = providedGap;
8283

@@ -150,6 +151,7 @@ const handleLayoutCalculation = (
150151
isReverse: boolean,
151152
shouldWrap: boolean
152153
) => {
154+
'worklet';
153155
const isRow = axisDirections.main === 'row';
154156
const expandMultiGroup = !IS_WEB && groups.length > 1; // expands to max height/width
155157
const paddingHorizontal = paddings.left + paddings.right;
@@ -332,6 +334,7 @@ export const calculateLayout = ({
332334
limits,
333335
paddings
334336
}: FlexLayoutProps): FlexLayout | null => {
337+
'worklet';
335338
if (!limits) {
336339
return null;
337340
}
Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
'worklet';
21
import { areValuesDifferent } from '../../../../utils';
32

4-
export const getMainIndex = (index: number, numGroups: number): number =>
5-
+index % numGroups;
3+
export const getMainIndex = (index: number, numGroups: number): number => {
4+
'worklet';
5+
return +index % numGroups;
6+
};
67

7-
export const getCrossIndex = (index: number, numGroups: number): number =>
8-
Math.floor(+index / numGroups);
8+
export const getCrossIndex = (index: number, numGroups: number): number => {
9+
'worklet';
10+
return Math.floor(+index / numGroups);
11+
};
912

1013
export const shouldUpdateContainerDimensions = (
1114
currentContainerCrossSize: null | number,
1215
calculatedContainerCrossSize: number,
1316
hasAutoOffsetAdjustment: boolean
14-
): boolean =>
15-
!currentContainerCrossSize ||
16-
(areValuesDifferent(
17-
currentContainerCrossSize,
18-
calculatedContainerCrossSize,
19-
1
20-
) &&
21-
(!hasAutoOffsetAdjustment ||
22-
calculatedContainerCrossSize > currentContainerCrossSize));
17+
): boolean => {
18+
'worklet';
19+
return (
20+
!currentContainerCrossSize ||
21+
!currentContainerCrossSize ||
22+
(areValuesDifferent(
23+
currentContainerCrossSize,
24+
calculatedContainerCrossSize,
25+
1
26+
) &&
27+
(!hasAutoOffsetAdjustment ||
28+
calculatedContainerCrossSize > currentContainerCrossSize))
29+
);
30+
};

packages/react-native-sortables/src/providers/grid/GridLayoutProvider/utils/layout.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'worklet';
21
import type {
32
AutoOffsetAdjustmentProps,
43
Coordinate,
@@ -18,6 +17,7 @@ export const calculateLayout = ({
1817
numGroups,
1918
startCrossOffset
2019
}: GridLayoutProps): GridLayout | null => {
20+
'worklet';
2121
const mainGroupSize = (isVertical ? itemWidths : itemHeights) as
2222
| null
2323
| number;
@@ -102,6 +102,7 @@ export const calculateItemCrossOffset = ({
102102
itemKey,
103103
numGroups
104104
}: AutoOffsetAdjustmentProps): number => {
105+
'worklet';
105106
let activeItemCrossOffset = 0;
106107
let currentGroupCrossSize = 0;
107108
let currentGroupCrossIndex = 0;

packages/react-native-sortables/src/providers/shared/AutoScrollProvider/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'worklet';
21
import type { ExtrapolationType } from 'react-native-reanimated';
32
import { Extrapolation, interpolate } from 'react-native-reanimated';
43

@@ -11,6 +10,7 @@ export const calculateRawProgress = (
1110
maxOverscroll: [number, number],
1211
extrapolation: ExtrapolationType
1312
) => {
13+
'worklet';
1414
const startDistance = containerPos + contentBounds[0];
1515
const startBoundProgress = interpolate(
1616
startDistance,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
'worklet';
21
import { EXTRA_SWAP_OFFSET } from '../../constants';
32
import type { Maybe } from '../../helperTypes';
43

54
export const getAdditionalSwapOffset = (size?: Maybe<number>) => {
5+
'worklet';
66
return size ? Math.min(EXTRA_SWAP_OFFSET, size / 2) : EXTRA_SWAP_OFFSET;
77
};

packages/react-native-sortables/src/utils/dimensions.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
'worklet';
21
import type { Dimensions, ItemSizes } from '../types';
32

4-
export const resolveDimension = (dimension: ItemSizes, key: string) =>
5-
dimension &&
6-
(typeof dimension === 'number' ? dimension : (dimension[key] ?? null));
3+
export const resolveDimension = (dimension: ItemSizes, key: string) => {
4+
'worklet';
5+
return (
6+
dimension &&
7+
(typeof dimension === 'number' ? dimension : (dimension[key] ?? null))
8+
);
9+
};
710

811
export const getItemDimensions = (
912
key: string,
1013
itemWidths: ItemSizes,
1114
itemHeights: ItemSizes
1215
): Dimensions | null => {
16+
'worklet';
1317
const itemWidth = resolveDimension(itemWidths, key);
1418
const itemHeight = resolveDimension(itemHeights, key);
1519

0 commit comments

Comments
 (0)