Skip to content

Commit

Permalink
fix: fixed a computed error
Browse files Browse the repository at this point in the history
fix #291, fix #294
  • Loading branch information
dohooo committed Nov 1, 2022
1 parent 1f38cd2 commit b49715c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
15 changes: 12 additions & 3 deletions src/hooks/computeNewIndexWhenDataChanges.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export function omitZero(a: number, b: number) {
"worklet";
if (a === 0)
return 0;

return b;
}

export function computeNewIndexWhenDataChanges(params: {
direction: number
handlerOffset: number
Expand All @@ -16,14 +24,15 @@ export function computeNewIndexWhenDataChanges(params: {

if (isPositive) {
positionIndex = (Math.abs(handlerOffset)) / size;
round = parseInt(String(positionIndex / previousLength));
round = parseInt(String(omitZero(previousLength, positionIndex / previousLength)));
}
else {
positionIndex = (Math.abs(handlerOffset) - size) / size;
round = parseInt(String(positionIndex / previousLength)) + 1;
round = parseInt(String(omitZero(previousLength, positionIndex / previousLength))) + 1;
}

const prevIndex = isPositive ? (positionIndex) % previousLength : previousLength - (positionIndex) % previousLength - 1;
const prevOffset = omitZero(previousLength, positionIndex % previousLength);
const prevIndex = isPositive ? prevOffset : previousLength - prevOffset - 1;
const changedLength = round * (currentLength - previousLength);
const changedOffset = changedLength * size;
if (prevIndex > currentLength - 1 && currentLength < previousLength) {
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,15 @@ describe("should work as expected", () => {

expect(handlerOffset / size).toBe(4 * negative);
});

it("Changing length of data set from 0 to 3, the index remains original.", async () => {
const handlerOffset = computeNewIndexWhenDataChanges(params({
currentIndex: 0,
direction: "positive",
previousLength: 0,
currentLength: 3,
}));

expect(handlerOffset / size).toBe(0 * positive);
});
});
17 changes: 9 additions & 8 deletions src/hooks/useCommonVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ export function useCommonVariables(
}, [vertical, handlerOffset, defaultHandlerOffsetValue]);

useAnimatedReaction(() => {
const _data = data.slice();
const previousLength = prevData.value.length;
const currentLength = data.length;
const currentLength = _data.length;
const isLengthChanged = previousLength !== currentLength;
const shouldComputed = isLengthChanged && loop;

if (shouldComputed)
prevData.value = _data;

return {
shouldComputed: isLengthChanged && loop,
shouldComputed,
previousLength,
currentLength,
data,
};
}, ({ shouldComputed, previousLength, currentLength }) => {
if (shouldComputed) {
Expand All @@ -58,12 +63,8 @@ export function useCommonVariables(
size,
handlerOffset: handlerOffset.value,
});

prevData.value = data;
}
}, [
data, loop,
]);
}, [data, loop]);

return {
size,
Expand Down

0 comments on commit b49715c

Please sign in to comment.