Skip to content

Commit cdf6962

Browse files
authored
chore: Clean up worklet directive usage (#447)
## Description This PR moves the `'worklet'` directive to the top of the file where possible (in files which contain only worklet functions).
1 parent 89eb24d commit cdf6962

File tree

14 files changed

+34
-78
lines changed

14 files changed

+34
-78
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
'worklet';
12
import { type LayoutAnimation, withTiming } from 'react-native-reanimated';
23

34
const ITEM_LAYOUT_ANIMATION_DURATION = 300;
45

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

2929
export const SortableItemEntering = (): LayoutAnimation => {
30-
'worklet';
3130
const animations = {
3231
opacity: withTiming(1, {
3332
duration: ITEM_LAYOUT_ANIMATION_DURATION

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'worklet';
12
import { makeMutable } from 'react-native-reanimated';
23

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

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

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

4847
export function clearAnimatedTimeout(handle: AnimatedTimeoutID): void {
49-
'worklet';
5048
removeFromPendingTimeouts(handle);
5149
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'worklet';
12
import type { ControlledSizes } from '../../../../../types';
23
import { reorderInsert, resolveDimension } from '../../../../../utils';
34

@@ -27,7 +28,6 @@ const getGroupItemIndex = (
2728
group: Array<string>,
2829
keyToIndex: Record<string, number>
2930
) => {
30-
'worklet';
3131
const key = group[inGroupIndex];
3232
if (key === undefined) return null;
3333
return keyToIndex[key] ?? null;
@@ -38,8 +38,6 @@ export const getTotalGroupSize = (
3838
mainItemSizes: ControlledSizes,
3939
gap: number
4040
) => {
41-
'worklet';
42-
4341
const sizesSum = group.reduce(
4442
(total, key) => total + (resolveDimension(mainItemSizes, key) ?? 0),
4543
0
@@ -64,7 +62,6 @@ const getIndexesWhenSwappedToGroupBefore = ({
6462
mainGap,
6563
mainItemSizes
6664
}: ItemGroupSwapProps): SwappedGroupIndexesResult => {
67-
'worklet';
6865
if (groupSizeLimit === Infinity) {
6966
return null;
7067
}
@@ -149,7 +146,6 @@ const getIndexesWhenSwappedToGroupAfter = ({
149146
mainGap,
150147
mainItemSizes
151148
}: ItemGroupSwapProps): SwappedGroupIndexesResult => {
152-
'worklet';
153149
const activeGroup = itemGroups[currentGroupIndex];
154150
if (groupSizeLimit === Infinity || activeGroup === undefined) {
155151
return null;
@@ -231,7 +227,6 @@ const getIndexesWhenSwappedToGroupAfter = ({
231227
export const getSwappedToGroupBeforeIndices = (
232228
props: ItemGroupSwapProps
233229
): ItemGroupSwapResult | null => {
234-
'worklet';
235230
const indexes = getIndexesWhenSwappedToGroupBefore(props);
236231
if (indexes === null) return null;
237232

@@ -252,7 +247,6 @@ export const getSwappedToGroupBeforeIndices = (
252247
export const getSwappedToGroupAfterIndices = (
253248
props: ItemGroupSwapProps
254249
): ItemGroupSwapResult | null => {
255-
'worklet';
256250
const indexes = getIndexesWhenSwappedToGroupAfter(props);
257251
if (indexes === null) return null;
258252

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'worklet';
12
import { IS_WEB } from '../../../../constants';
23
import type {
34
AlignContent,
@@ -24,7 +25,6 @@ const createGroups = (
2425
groups: Array<Array<string>>;
2526
crossAxisGroupSizes: Array<number>;
2627
} => {
27-
'worklet';
2828
const groups: Array<Array<string>> = [];
2929
const crossAxisGroupSizes: Array<number> = [];
3030

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

@@ -151,7 +150,6 @@ const handleLayoutCalculation = (
151150
isReverse: boolean,
152151
shouldWrap: boolean
153152
) => {
154-
'worklet';
155153
const isRow = axisDirections.main === 'row';
156154
const expandMultiGroup = !IS_WEB && groups.length > 1; // expands to max height/width
157155
const paddingHorizontal = paddings.left + paddings.right;
@@ -334,7 +332,6 @@ export const calculateLayout = ({
334332
limits,
335333
paddings
336334
}: FlexLayoutProps): FlexLayout | null => {
337-
'worklet';
338335
if (!limits) {
339336
return null;
340337
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
'worklet';
2+
13
export const getMainIndex = (index: number, numGroups: number): number => {
2-
'worklet';
34
return +index % numGroups;
45
};
56

67
export const getCrossIndex = (index: number, numGroups: number): number => {
7-
'worklet';
88
return Math.floor(+index / numGroups);
99
};

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'worklet';
12
import type { MeasuredDimensions } from 'react-native-reanimated';
23

34
import type { DebugLineUpdater, DebugRectUpdater } from '../../../types';
@@ -16,7 +17,6 @@ export const handleMeasurementsVertical = (
1617
debugRects: Record<'end' | 'start', DebugRectUpdater> | undefined,
1718
debugLine: DebugLineUpdater | undefined
1819
) => {
19-
'worklet';
2020
const { height: sH, pageY: sY } = scrollableMeasurements;
2121
const { height: cH, pageY: cY } = containerMeasurements;
2222
maxOverScrollOffset ??= threshold;
@@ -65,7 +65,6 @@ export const handleMeasurementsHorizontal = (
6565
debugRects: Record<'end' | 'start', DebugRectUpdater> | undefined,
6666
debugLine: DebugLineUpdater | undefined
6767
) => {
68-
'worklet';
6968
const { pageX: sX, width: sW } = scrollableMeasurements;
7069
const { pageX: cX, width: cW } = containerMeasurements;
7170
maxOverScrollOffset ??= threshold;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
'worklet';
12
import { EXTRA_SWAP_OFFSET } from '../../constants';
23
import type { Maybe } from '../../helperTypes';
34

45
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/arrays.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
export const zipArrays = <T, U>(a: Array<T>, b: Array<U>): Array<[T, U]> => {
2-
'worklet';
3-
return a.slice(0, b.length).map((_, i) => [a[i], b[i]]) as Array<[T, U]>;
4-
};
1+
'worklet';
2+
export const zipArrays = <T, U>(a: Array<T>, b: Array<U>) =>
3+
a.slice(0, b.length).map((_, i) => [a[i], b[i]]) as Array<[T, U]>;
54

65
export const reverseArray = <T>(array: Array<T>): void => {
7-
'worklet';
86
for (let i = 0; i < array.length / 2; i++) {
97
[array[i], array[array.length - i - 1]] = [
108
array[array.length - i - 1]!,

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

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

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

118
export const getItemDimensions = (
129
key: string,
1310
itemWidths: ControlledSizes,
1411
itemHeights: ControlledSizes
1512
): Dimensions | null => {
16-
'worklet';
1713
const itemWidth = resolveDimension(itemWidths, key);
1814
const itemHeight = resolveDimension(itemHeights, key);
1915

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

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
1+
'worklet';
12
import type { AnyRecord, Maybe } from '../helperTypes';
23
import type { Vector } from '../types';
34

4-
export function lt(a: number, b: number): boolean {
5-
'worklet';
6-
return a < b;
7-
}
8-
9-
export function gt(a: number, b: number): boolean {
10-
'worklet';
11-
return a > b;
12-
}
5+
export const lt = (a: number, b: number): boolean => a < b;
6+
export const gt = (a: number, b: number): boolean => a > b;
137

148
export const areArraysDifferent = <T>(
159
arr1: Array<T>,
1610
arr2: Array<T>,
1711
areEqual = (a: T, b: T): boolean => a === b
18-
): boolean => {
19-
'worklet';
20-
return (
21-
arr1.length !== arr2.length ||
22-
arr1.some((item, index) => !areEqual(item, arr2[index] as T))
23-
);
24-
};
12+
): boolean =>
13+
arr1.length !== arr2.length ||
14+
arr1.some((item, index) => !areEqual(item, arr2[index] as T));
2515

2616
export const areValuesDifferent = (
2717
dim1: number | undefined,
2818
dim2: number | undefined,
2919
eps?: number
3020
): boolean => {
31-
'worklet';
3221
if (dim1 === undefined) {
3322
return dim2 !== undefined;
3423
}
@@ -47,19 +36,14 @@ export const areVectorsDifferent = (
4736
vec1: Vector,
4837
vec2: Vector,
4938
eps?: number
50-
): boolean => {
51-
'worklet';
52-
return (
53-
areValuesDifferent(vec1.x, vec2.x, eps) ||
54-
areValuesDifferent(vec1.y, vec2.y, eps)
55-
);
56-
};
39+
): boolean =>
40+
areValuesDifferent(vec1.x, vec2.x, eps) ||
41+
areValuesDifferent(vec1.y, vec2.y, eps);
5742

5843
export const haveEqualPropValues = <T extends AnyRecord>(
5944
obj1: Maybe<T>,
6045
obj2: Maybe<T>
6146
): boolean => {
62-
'worklet';
6347
if (!obj1 || !obj2) {
6448
return false;
6549
}

0 commit comments

Comments
 (0)