Skip to content

Commit b49715c

Browse files
committed
fix: fixed a computed error
fix #291, fix #294
1 parent 1f38cd2 commit b49715c

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/hooks/computeNewIndexWhenDataChanges.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
export function omitZero(a: number, b: number) {
2+
"worklet";
3+
if (a === 0)
4+
return 0;
5+
6+
return b;
7+
}
8+
19
export function computeNewIndexWhenDataChanges(params: {
210
direction: number
311
handlerOffset: number
@@ -16,14 +24,15 @@ export function computeNewIndexWhenDataChanges(params: {
1624

1725
if (isPositive) {
1826
positionIndex = (Math.abs(handlerOffset)) / size;
19-
round = parseInt(String(positionIndex / previousLength));
27+
round = parseInt(String(omitZero(previousLength, positionIndex / previousLength)));
2028
}
2129
else {
2230
positionIndex = (Math.abs(handlerOffset) - size) / size;
23-
round = parseInt(String(positionIndex / previousLength)) + 1;
31+
round = parseInt(String(omitZero(previousLength, positionIndex / previousLength))) + 1;
2432
}
2533

26-
const prevIndex = isPositive ? (positionIndex) % previousLength : previousLength - (positionIndex) % previousLength - 1;
34+
const prevOffset = omitZero(previousLength, positionIndex % previousLength);
35+
const prevIndex = isPositive ? prevOffset : previousLength - prevOffset - 1;
2736
const changedLength = round * (currentLength - previousLength);
2837
const changedOffset = changedLength * size;
2938
if (prevIndex > currentLength - 1 && currentLength < previousLength) {

src/hooks/index.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,15 @@ describe("should work as expected", () => {
6868

6969
expect(handlerOffset / size).toBe(4 * negative);
7070
});
71+
72+
it("Changing length of data set from 0 to 3, the index remains original.", async () => {
73+
const handlerOffset = computeNewIndexWhenDataChanges(params({
74+
currentIndex: 0,
75+
direction: "positive",
76+
previousLength: 0,
77+
currentLength: 3,
78+
}));
79+
80+
expect(handlerOffset / size).toBe(0 * positive);
81+
});
7182
});

src/hooks/useCommonVariables.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ export function useCommonVariables(
3737
}, [vertical, handlerOffset, defaultHandlerOffsetValue]);
3838

3939
useAnimatedReaction(() => {
40+
const _data = data.slice();
4041
const previousLength = prevData.value.length;
41-
const currentLength = data.length;
42+
const currentLength = _data.length;
4243
const isLengthChanged = previousLength !== currentLength;
44+
const shouldComputed = isLengthChanged && loop;
45+
46+
if (shouldComputed)
47+
prevData.value = _data;
48+
4349
return {
44-
shouldComputed: isLengthChanged && loop,
50+
shouldComputed,
4551
previousLength,
4652
currentLength,
47-
data,
4853
};
4954
}, ({ shouldComputed, previousLength, currentLength }) => {
5055
if (shouldComputed) {
@@ -58,12 +63,8 @@ export function useCommonVariables(
5863
size,
5964
handlerOffset: handlerOffset.value,
6065
});
61-
62-
prevData.value = data;
6366
}
64-
}, [
65-
data, loop,
66-
]);
67+
}, [data, loop]);
6768

6869
return {
6970
size,

0 commit comments

Comments
 (0)