Skip to content

Commit

Permalink
fix: refactoring some code on data prop
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Nov 4, 2022
1 parent e4a29a7 commit 5ff96ab
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 46 deletions.
25 changes: 15 additions & 10 deletions src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(

const {
testID,
data,
rawData,
loop,
autoFillData,
// Fill data with autoFillData
data,
// Length of fill data
dataLength,
// Raw data that has not been processed
rawData,
// Length of raw data
rawDataLength,
mode,
style,
width,
Expand All @@ -49,7 +55,6 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(

const commonVariables = useCommonVariables(props);
const { size, handlerOffset } = commonVariables;
const dataLength = data.length;

const offsetX = useDerivedValue(() => {
const totalSize = size * dataLength;
Expand All @@ -61,20 +66,20 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
return isNaN(x) ? 0 : x;
}, [loop, size, dataLength]);

usePropsErrorBoundary(props);
usePropsErrorBoundary({ ...props, dataLength });
useOnProgressChange({
autoFillData,
loop,
size,
offsetX,
rawData,
rawDataLength,
onProgressChange,
});

const carouselController = useCarouselController({
loop,
size,
data,
dataLength,
autoFillData,
handlerOffset,
withAnimation,
Expand All @@ -99,7 +104,7 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(

const realIndex = computedRealIndexWithAutoFillData({
index: _sharedIndex,
dataLength: rawData.length,
dataLength: rawDataLength,
loop,
autoFillData,
});
Expand All @@ -112,7 +117,7 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
}, [
loop,
autoFillData,
rawData.length,
rawDataLength,
getSharedIndex,
onSnapToItem,
onScrollEnd,
Expand Down Expand Up @@ -148,7 +153,7 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
);

const visibleRanges = useVisibleRanges({
total: data.length,
total: dataLength,
viewSize: size,
translation: handlerOffset,
windowSize,
Expand All @@ -160,7 +165,7 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
(item: any, i: number) => {
const realIndex = computedRealIndexWithAutoFillData({
index: i,
dataLength: rawData.length,
dataLength: rawDataLength,
loop,
autoFillData,
});
Expand Down
4 changes: 2 additions & 2 deletions src/ScrollViewGesture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
const {
props: {
vertical,
data,
pagingEnabled,
snapEnabled,
panGestureHandlerProps,
loop: infinite,
scrollAnimationDuration,
withAnimation,
enabled,
dataLength,
},
} = React.useContext(CTX);

Expand All @@ -63,7 +63,7 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
onTouchEnd,
} = props;

const maxPage = data.length;
const maxPage = dataLength;
const isHorizontal = useDerivedValue(() => !vertical, [vertical]);
const touching = useSharedValue(false);
const scrollEndTranslation = useSharedValue(0);
Expand Down
12 changes: 6 additions & 6 deletions src/hooks/useCarouselController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { round } from "../utils/log";
interface IOpts {
loop: boolean
size: number
data: TCarouselProps["data"]
dataLength: number
autoFillData: TCarouselProps["autoFillData"]
handlerOffset: Animated.SharedValue<number>
withAnimation?: TCarouselProps["withAnimation"]
Expand All @@ -41,8 +41,8 @@ export interface ICarouselController {
export function useCarouselController(options: IOpts): ICarouselController {
const {
size,
data,
loop,
dataLength,
handlerOffset,
withAnimation,
defaultIndex = 0,
Expand All @@ -52,11 +52,11 @@ export function useCarouselController(options: IOpts): ICarouselController {

const dataInfo = React.useMemo(
() => ({
length: data.length,
disable: !data.length,
originalLength: data.length,
length: dataLength,
disable: !dataLength,
originalLength: dataLength,
}),
[data],
[dataLength],
);

const index = useSharedValue<number>(defaultIndex);
Expand Down
12 changes: 6 additions & 6 deletions src/hooks/useCommonVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ export function useCommonVariables(
vertical,
height,
width,
data,
dataLength,
defaultIndex,
defaultScrollOffsetValue,
loop,
} = props;
const size = vertical ? height : width;
const validLength = data.length - 1;
const validLength = dataLength - 1;
const defaultHandlerOffsetValue = -Math.abs(defaultIndex * size);
const _handlerOffset = useSharedValue<number>(defaultHandlerOffsetValue);
const handlerOffset = defaultScrollOffsetValue ?? _handlerOffset;
const prevDataLength = useSharedValue(data.length);
const prevDataLength = useSharedValue(dataLength);

React.useEffect(() => {
handlerOffset.value = defaultHandlerOffsetValue;
}, [vertical, handlerOffset, defaultHandlerOffsetValue]);

useAnimatedReaction(() => {
const previousLength = prevDataLength.value;
const currentLength = data.length;
const currentLength = dataLength;
const isLengthChanged = previousLength !== currentLength;
const shouldComputed = isLengthChanged && loop;

if (shouldComputed)
prevDataLength.value = data.length;
prevDataLength.value = dataLength;

return {
shouldComputed,
Expand All @@ -63,7 +63,7 @@ export function useCommonVariables(
handlerOffset: handlerOffset.value,
});
}
}, [data, loop]);
}, [dataLength, loop]);

return {
size,
Expand Down
17 changes: 13 additions & 4 deletions src/hooks/useInitProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ TGetRequiredProps<
> & {
// Raw data that has not been processed
rawData: T[]
dataLength: number
rawDataLength: number
};

export function useInitProps<T>(
Expand Down Expand Up @@ -47,31 +49,38 @@ export function useInitProps<T>(

const data = React.useMemo<T[]>(
() => {
const computedData = computedFillDataWithAutoFillData<T>({
return computedFillDataWithAutoFillData<T>({
loop,
autoFillData,
data: rawData,
dataLength: rawData.length,
});

return Array.from({ length: computedData.length });
},
[rawData, loop, autoFillData],
);

const dataLength = data.length;
const rawDataLength = rawData.length;

if (props.mode === "vertical-stack" || props.mode === "horizontal-stack") {
if (!props.modeConfig)
props.modeConfig = {};

props.modeConfig.showLength = props.modeConfig?.showLength ?? data.length - 1;
props.modeConfig.showLength = props.modeConfig?.showLength ?? dataLength - 1;
}

return {
...props,
defaultIndex,
autoFillData,
// Fill data with autoFillData
data,
// Length of fill data
dataLength,
// Raw data that has not been processed
rawData,
// Length of raw data
rawDataLength,
loop,
enabled,
autoPlayInterval,
Expand Down
11 changes: 6 additions & 5 deletions src/hooks/useOffsetX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface IOpts {
index: number
size: number
handlerOffset: Animated.SharedValue<number>
data: unknown[]
dataLength: number
type?: "positive" | "negative"
viewCount?: number
loop?: boolean
Expand All @@ -23,16 +23,17 @@ export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {
index,
size,
loop,
data,
dataLength,
type = "positive",
viewCount = Math.round((data.length - 1) / 2),
viewCount: _viewCount,
} = opts;

const ITEM_LENGTH = data.length;
const ITEM_LENGTH = dataLength;
const VALID_LENGTH = ITEM_LENGTH - 1;
const TOTAL_WIDTH = size * ITEM_LENGTH;
const HALF_WIDTH = 0.5 * size;

const viewCount = _viewCount ?? Math.round((ITEM_LENGTH - 1) / 2);
const positiveCount
= type === "positive" ? viewCount : VALID_LENGTH - viewCount;

Expand Down Expand Up @@ -81,7 +82,7 @@ export const useOffsetX = (opts: IOpts, visibleRanges: IVisibleRanges) => {
}

return handlerOffset.value + size * index;
}, [loop, data, viewCount, type, size, visibleRanges]);
}, [loop, dataLength, viewCount, type, size, visibleRanges]);

return x;
};
6 changes: 2 additions & 4 deletions src/hooks/useOnProgressChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ export function useOnProgressChange(
autoFillData: boolean
loop: boolean
offsetX: Animated.SharedValue<number>
rawData: TCarouselProps["data"]
rawDataLength: number
} & Pick<TCarouselProps, "onProgressChange">,
) {
const { autoFillData, loop, offsetX, rawData, size, onProgressChange }
const { autoFillData, loop, offsetX, size, rawDataLength, onProgressChange }
= opts;

const rawDataLength = rawData.length;

useAnimatedReaction(
() => offsetX.value,
(_value) => {
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/usePropsErrorBoundary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import React from "react";

import type { TCarouselProps } from "../types";

export function usePropsErrorBoundary(props: TCarouselProps) {
export function usePropsErrorBoundary(props: TCarouselProps & { dataLength: number }) {
React.useEffect(() => {
const { defaultIndex, data } = props;
const { defaultIndex, dataLength: viewCount } = props;

const viewCount = data.length;
if (typeof defaultIndex === "number" && viewCount > 0) {
if (defaultIndex < 0 || defaultIndex >= viewCount) {
throw new Error(
Expand Down
6 changes: 3 additions & 3 deletions src/layouts/BaseLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const BaseLayout: React.FC<{
const {
props: {
loop,
data,
dataLength,
width,
height,
vertical,
Expand All @@ -51,7 +51,7 @@ export const BaseLayout: React.FC<{
handlerOffset,
index,
size,
data,
dataLength,
loop,
...(typeof customConfig === "function" ? customConfig() : {}),
};
Expand All @@ -63,7 +63,7 @@ export const BaseLayout: React.FC<{
handlerOffset,
index,
size,
data,
dataLength,
loop,
type: snapDirection === "right" ? "negative" : "positive",
viewCount: showLength,
Expand Down
6 changes: 3 additions & 3 deletions src/layouts/ParallaxLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ IComputedDirectionTypes<
loop?: boolean
handlerOffset: Animated.SharedValue<number>
index: number
data: unknown[]
dataLength: number
visibleRanges: IVisibleRanges
} & ILayoutConfig
>
Expand All @@ -34,7 +34,7 @@ IComputedDirectionTypes<
width,
height,
loop,
data,
dataLength,
children,
visibleRanges,
vertical,
Expand All @@ -49,7 +49,7 @@ IComputedDirectionTypes<
handlerOffset,
index,
size,
data,
dataLength,
loop,
},
visibleRanges,
Expand Down

0 comments on commit 5ff96ab

Please sign in to comment.