@leonsilicon/react-native-reanimated-carousel 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +652 -0
- package/lib/commonjs/components/Carousel.js +2 -0
- package/lib/commonjs/components/Carousel.js.map +1 -0
- package/lib/commonjs/components/CarouselLayout.js +2 -0
- package/lib/commonjs/components/CarouselLayout.js.map +1 -0
- package/lib/commonjs/components/ItemLayout.js +2 -0
- package/lib/commonjs/components/ItemLayout.js.map +1 -0
- package/lib/commonjs/components/ItemRenderer.js +2 -0
- package/lib/commonjs/components/ItemRenderer.js.map +1 -0
- package/lib/commonjs/components/LazyView.js +2 -0
- package/lib/commonjs/components/LazyView.js.map +1 -0
- package/lib/commonjs/components/Pagination/Basic/PaginationItem.js +2 -0
- package/lib/commonjs/components/Pagination/Basic/PaginationItem.js.map +1 -0
- package/lib/commonjs/components/Pagination/Basic/index.js +2 -0
- package/lib/commonjs/components/Pagination/Basic/index.js.map +1 -0
- package/lib/commonjs/components/Pagination/Custom/PaginationItem.js +2 -0
- package/lib/commonjs/components/Pagination/Custom/PaginationItem.js.map +1 -0
- package/lib/commonjs/components/Pagination/Custom/index.js +2 -0
- package/lib/commonjs/components/Pagination/Custom/index.js.map +1 -0
- package/lib/commonjs/components/Pagination/index.js +2 -0
- package/lib/commonjs/components/Pagination/index.js.map +1 -0
- package/lib/commonjs/components/ScrollViewGesture.js +2 -0
- package/lib/commonjs/components/ScrollViewGesture.js.map +1 -0
- package/lib/commonjs/constants/index.js +2 -0
- package/lib/commonjs/constants/index.js.map +1 -0
- package/lib/commonjs/hooks/useAutoPlay.js +2 -0
- package/lib/commonjs/hooks/useAutoPlay.js.map +1 -0
- package/lib/commonjs/hooks/useCarouselController.js +2 -0
- package/lib/commonjs/hooks/useCarouselController.js.map +1 -0
- package/lib/commonjs/hooks/useCheckMounted.js +2 -0
- package/lib/commonjs/hooks/useCheckMounted.js.map +1 -0
- package/lib/commonjs/hooks/useCommonVariables.js +2 -0
- package/lib/commonjs/hooks/useCommonVariables.js.map +1 -0
- package/lib/commonjs/hooks/useInitProps.js +2 -0
- package/lib/commonjs/hooks/useInitProps.js.map +1 -0
- package/lib/commonjs/hooks/useLayoutConfig.js +2 -0
- package/lib/commonjs/hooks/useLayoutConfig.js.map +1 -0
- package/lib/commonjs/hooks/useOffsetX.js +2 -0
- package/lib/commonjs/hooks/useOffsetX.js.map +1 -0
- package/lib/commonjs/hooks/useOnProgressChange.js +2 -0
- package/lib/commonjs/hooks/useOnProgressChange.js.map +1 -0
- package/lib/commonjs/hooks/usePanGestureProxy.js +2 -0
- package/lib/commonjs/hooks/usePanGestureProxy.js.map +1 -0
- package/lib/commonjs/hooks/usePropsErrorBoundary.js +2 -0
- package/lib/commonjs/hooks/usePropsErrorBoundary.js.map +1 -0
- package/lib/commonjs/hooks/useSizeResolver.js +2 -0
- package/lib/commonjs/hooks/useSizeResolver.js.map +1 -0
- package/lib/commonjs/hooks/useUpdateGestureConfig.js +2 -0
- package/lib/commonjs/hooks/useUpdateGestureConfig.js.map +1 -0
- package/lib/commonjs/hooks/useVisibleRanges.js +2 -0
- package/lib/commonjs/hooks/useVisibleRanges.js.map +1 -0
- package/lib/commonjs/index.js +2 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/layouts/index.js +2 -0
- package/lib/commonjs/layouts/index.js.map +1 -0
- package/lib/commonjs/layouts/normal.js +2 -0
- package/lib/commonjs/layouts/normal.js.map +1 -0
- package/lib/commonjs/layouts/parallax.js +2 -0
- package/lib/commonjs/layouts/parallax.js.map +1 -0
- package/lib/commonjs/layouts/stack.js +2 -0
- package/lib/commonjs/layouts/stack.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/store/index.js +2 -0
- package/lib/commonjs/store/index.js.map +1 -0
- package/lib/commonjs/types.js +2 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/utils/compute-gesture-translation.js +2 -0
- package/lib/commonjs/utils/compute-gesture-translation.js.map +1 -0
- package/lib/commonjs/utils/compute-offset-if-data-changed.js +2 -0
- package/lib/commonjs/utils/compute-offset-if-data-changed.js.map +1 -0
- package/lib/commonjs/utils/compute-offset-if-size-changed.js +2 -0
- package/lib/commonjs/utils/compute-offset-if-size-changed.js.map +1 -0
- package/lib/commonjs/utils/compute-offset-if-sizes-changed.js +2 -0
- package/lib/commonjs/utils/compute-offset-if-sizes-changed.js.map +1 -0
- package/lib/commonjs/utils/computed-with-auto-fill-data.js +2 -0
- package/lib/commonjs/utils/computed-with-auto-fill-data.js.map +1 -0
- package/lib/commonjs/utils/deal-with-animation.js +2 -0
- package/lib/commonjs/utils/deal-with-animation.js.map +1 -0
- package/lib/commonjs/utils/handleroffset-direction.js +2 -0
- package/lib/commonjs/utils/handleroffset-direction.js.map +1 -0
- package/lib/commonjs/utils/log.js +2 -0
- package/lib/commonjs/utils/log.js.map +1 -0
- package/lib/commonjs/utils/sanitize-animation-style.js +2 -0
- package/lib/commonjs/utils/sanitize-animation-style.js.map +1 -0
- package/lib/commonjs/utils/size-resolver.js +2 -0
- package/lib/commonjs/utils/size-resolver.js.map +1 -0
- package/lib/module/components/Carousel.js +2 -0
- package/lib/module/components/Carousel.js.map +1 -0
- package/lib/module/components/CarouselLayout.js +2 -0
- package/lib/module/components/CarouselLayout.js.map +1 -0
- package/lib/module/components/ItemLayout.js +2 -0
- package/lib/module/components/ItemLayout.js.map +1 -0
- package/lib/module/components/ItemRenderer.js +2 -0
- package/lib/module/components/ItemRenderer.js.map +1 -0
- package/lib/module/components/LazyView.js +2 -0
- package/lib/module/components/LazyView.js.map +1 -0
- package/lib/module/components/Pagination/Basic/PaginationItem.js +2 -0
- package/lib/module/components/Pagination/Basic/PaginationItem.js.map +1 -0
- package/lib/module/components/Pagination/Basic/index.js +2 -0
- package/lib/module/components/Pagination/Basic/index.js.map +1 -0
- package/lib/module/components/Pagination/Custom/PaginationItem.js +2 -0
- package/lib/module/components/Pagination/Custom/PaginationItem.js.map +1 -0
- package/lib/module/components/Pagination/Custom/index.js +2 -0
- package/lib/module/components/Pagination/Custom/index.js.map +1 -0
- package/lib/module/components/Pagination/index.js +2 -0
- package/lib/module/components/Pagination/index.js.map +1 -0
- package/lib/module/components/ScrollViewGesture.js +2 -0
- package/lib/module/components/ScrollViewGesture.js.map +1 -0
- package/lib/module/constants/index.js +2 -0
- package/lib/module/constants/index.js.map +1 -0
- package/lib/module/hooks/useAutoPlay.js +2 -0
- package/lib/module/hooks/useAutoPlay.js.map +1 -0
- package/lib/module/hooks/useCarouselController.js +2 -0
- package/lib/module/hooks/useCarouselController.js.map +1 -0
- package/lib/module/hooks/useCheckMounted.js +2 -0
- package/lib/module/hooks/useCheckMounted.js.map +1 -0
- package/lib/module/hooks/useCommonVariables.js +2 -0
- package/lib/module/hooks/useCommonVariables.js.map +1 -0
- package/lib/module/hooks/useInitProps.js +2 -0
- package/lib/module/hooks/useInitProps.js.map +1 -0
- package/lib/module/hooks/useLayoutConfig.js +2 -0
- package/lib/module/hooks/useLayoutConfig.js.map +1 -0
- package/lib/module/hooks/useOffsetX.js +2 -0
- package/lib/module/hooks/useOffsetX.js.map +1 -0
- package/lib/module/hooks/useOnProgressChange.js +2 -0
- package/lib/module/hooks/useOnProgressChange.js.map +1 -0
- package/lib/module/hooks/usePanGestureProxy.js +2 -0
- package/lib/module/hooks/usePanGestureProxy.js.map +1 -0
- package/lib/module/hooks/usePropsErrorBoundary.js +2 -0
- package/lib/module/hooks/usePropsErrorBoundary.js.map +1 -0
- package/lib/module/hooks/useSizeResolver.js +2 -0
- package/lib/module/hooks/useSizeResolver.js.map +1 -0
- package/lib/module/hooks/useUpdateGestureConfig.js +2 -0
- package/lib/module/hooks/useUpdateGestureConfig.js.map +1 -0
- package/lib/module/hooks/useVisibleRanges.js +2 -0
- package/lib/module/hooks/useVisibleRanges.js.map +1 -0
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/layouts/index.js +2 -0
- package/lib/module/layouts/index.js.map +1 -0
- package/lib/module/layouts/normal.js +2 -0
- package/lib/module/layouts/normal.js.map +1 -0
- package/lib/module/layouts/parallax.js +2 -0
- package/lib/module/layouts/parallax.js.map +1 -0
- package/lib/module/layouts/stack.js +2 -0
- package/lib/module/layouts/stack.js.map +1 -0
- package/lib/module/store/index.js +2 -0
- package/lib/module/store/index.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/utils/compute-gesture-translation.js +2 -0
- package/lib/module/utils/compute-gesture-translation.js.map +1 -0
- package/lib/module/utils/compute-offset-if-data-changed.js +2 -0
- package/lib/module/utils/compute-offset-if-data-changed.js.map +1 -0
- package/lib/module/utils/compute-offset-if-size-changed.js +2 -0
- package/lib/module/utils/compute-offset-if-size-changed.js.map +1 -0
- package/lib/module/utils/compute-offset-if-sizes-changed.js +2 -0
- package/lib/module/utils/compute-offset-if-sizes-changed.js.map +1 -0
- package/lib/module/utils/computed-with-auto-fill-data.js +2 -0
- package/lib/module/utils/computed-with-auto-fill-data.js.map +1 -0
- package/lib/module/utils/deal-with-animation.js +2 -0
- package/lib/module/utils/deal-with-animation.js.map +1 -0
- package/lib/module/utils/handleroffset-direction.js +2 -0
- package/lib/module/utils/handleroffset-direction.js.map +1 -0
- package/lib/module/utils/log.js +2 -0
- package/lib/module/utils/log.js.map +1 -0
- package/lib/module/utils/sanitize-animation-style.js +2 -0
- package/lib/module/utils/sanitize-animation-style.js.map +1 -0
- package/lib/module/utils/size-resolver.js +2 -0
- package/lib/module/utils/size-resolver.js.map +1 -0
- package/lib/typescript/components/Carousel.d.ts +8 -0
- package/lib/typescript/components/Carousel.d.ts.map +1 -0
- package/lib/typescript/components/CarouselLayout.d.ts +6 -0
- package/lib/typescript/components/CarouselLayout.d.ts.map +1 -0
- package/lib/typescript/components/ItemLayout.d.ts +15 -0
- package/lib/typescript/components/ItemLayout.d.ts.map +1 -0
- package/lib/typescript/components/ItemRenderer.d.ts +24 -0
- package/lib/typescript/components/ItemRenderer.d.ts.map +1 -0
- package/lib/typescript/components/LazyView.d.ts +8 -0
- package/lib/typescript/components/LazyView.d.ts.map +1 -0
- package/lib/typescript/components/Pagination/Basic/PaginationItem.d.ts +29 -0
- package/lib/typescript/components/Pagination/Basic/PaginationItem.d.ts.map +1 -0
- package/lib/typescript/components/Pagination/Basic/index.d.ts +23 -0
- package/lib/typescript/components/Pagination/Basic/index.d.ts.map +1 -0
- package/lib/typescript/components/Pagination/Custom/PaginationItem.d.ts +35 -0
- package/lib/typescript/components/Pagination/Custom/PaginationItem.d.ts.map +1 -0
- package/lib/typescript/components/Pagination/Custom/index.d.ts +26 -0
- package/lib/typescript/components/Pagination/Custom/index.d.ts.map +1 -0
- package/lib/typescript/components/Pagination/index.d.ts +5 -0
- package/lib/typescript/components/Pagination/index.d.ts.map +1 -0
- package/lib/typescript/components/ScrollViewGesture.d.ts +19 -0
- package/lib/typescript/components/ScrollViewGesture.d.ts.map +1 -0
- package/lib/typescript/constants/index.d.ts +9 -0
- package/lib/typescript/constants/index.d.ts.map +1 -0
- package/lib/typescript/hooks/useAutoPlay.d.ts +12 -0
- package/lib/typescript/hooks/useAutoPlay.d.ts.map +1 -0
- package/lib/typescript/hooks/useCarouselController.d.ts +28 -0
- package/lib/typescript/hooks/useCarouselController.d.ts.map +1 -0
- package/lib/typescript/hooks/useCheckMounted.d.ts +3 -0
- package/lib/typescript/hooks/useCheckMounted.d.ts.map +1 -0
- package/lib/typescript/hooks/useCommonVariables.d.ts +13 -0
- package/lib/typescript/hooks/useCommonVariables.d.ts.map +1 -0
- package/lib/typescript/hooks/useInitProps.d.ts +17 -0
- package/lib/typescript/hooks/useInitProps.d.ts.map +1 -0
- package/lib/typescript/hooks/useLayoutConfig.d.ts +10 -0
- package/lib/typescript/hooks/useLayoutConfig.d.ts.map +1 -0
- package/lib/typescript/hooks/useOffsetX.d.ts +24 -0
- package/lib/typescript/hooks/useOffsetX.d.ts.map +1 -0
- package/lib/typescript/hooks/useOnProgressChange.d.ts +14 -0
- package/lib/typescript/hooks/useOnProgressChange.d.ts.map +1 -0
- package/lib/typescript/hooks/usePanGestureProxy.d.ts +10 -0
- package/lib/typescript/hooks/usePanGestureProxy.d.ts.map +1 -0
- package/lib/typescript/hooks/usePropsErrorBoundary.d.ts +5 -0
- package/lib/typescript/hooks/usePropsErrorBoundary.d.ts.map +1 -0
- package/lib/typescript/hooks/useSizeResolver.d.ts +27 -0
- package/lib/typescript/hooks/useSizeResolver.d.ts.map +1 -0
- package/lib/typescript/hooks/useUpdateGestureConfig.d.ts +6 -0
- package/lib/typescript/hooks/useUpdateGestureConfig.d.ts.map +1 -0
- package/lib/typescript/hooks/useVisibleRanges.d.ts +23 -0
- package/lib/typescript/hooks/useVisibleRanges.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +7 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/layouts/index.d.ts +11 -0
- package/lib/typescript/layouts/index.d.ts.map +1 -0
- package/lib/typescript/layouts/normal.d.ts +16 -0
- package/lib/typescript/layouts/normal.d.ts.map +1 -0
- package/lib/typescript/layouts/parallax.d.ts +50 -0
- package/lib/typescript/layouts/parallax.d.ts.map +1 -0
- package/lib/typescript/layouts/stack.d.ts +38 -0
- package/lib/typescript/layouts/stack.d.ts.map +1 -0
- package/lib/typescript/store/index.d.ts +38 -0
- package/lib/typescript/store/index.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +326 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/lib/typescript/utils/compute-gesture-translation.d.ts +9 -0
- package/lib/typescript/utils/compute-gesture-translation.d.ts.map +1 -0
- package/lib/typescript/utils/compute-offset-if-data-changed.d.ts +9 -0
- package/lib/typescript/utils/compute-offset-if-data-changed.d.ts.map +1 -0
- package/lib/typescript/utils/compute-offset-if-size-changed.d.ts +6 -0
- package/lib/typescript/utils/compute-offset-if-size-changed.d.ts.map +1 -0
- package/lib/typescript/utils/compute-offset-if-sizes-changed.d.ts +14 -0
- package/lib/typescript/utils/compute-offset-if-sizes-changed.d.ts.map +1 -0
- package/lib/typescript/utils/computed-with-auto-fill-data.d.ts +23 -0
- package/lib/typescript/utils/computed-with-auto-fill-data.d.ts.map +1 -0
- package/lib/typescript/utils/deal-with-animation.d.ts +3 -0
- package/lib/typescript/utils/deal-with-animation.d.ts.map +1 -0
- package/lib/typescript/utils/handleroffset-direction.d.ts +4 -0
- package/lib/typescript/utils/handleroffset-direction.d.ts.map +1 -0
- package/lib/typescript/utils/log.d.ts +7 -0
- package/lib/typescript/utils/log.d.ts.map +1 -0
- package/lib/typescript/utils/sanitize-animation-style.d.ts +3 -0
- package/lib/typescript/utils/sanitize-animation-style.d.ts.map +1 -0
- package/lib/typescript/utils/size-resolver.d.ts +87 -0
- package/lib/typescript/utils/size-resolver.d.ts.map +1 -0
- package/package.json +151 -0
- package/src/components/Carousel.test.tsx +1153 -0
- package/src/components/Carousel.tsx +35 -0
- package/src/components/CarouselLayout.tsx +231 -0
- package/src/components/ItemLayout.tsx +217 -0
- package/src/components/ItemRenderer.tsx +114 -0
- package/src/components/LazyView.test.tsx +61 -0
- package/src/components/LazyView.tsx +14 -0
- package/src/components/Pagination/Basic/PaginationItem.tsx +149 -0
- package/src/components/Pagination/Basic/index.tsx +98 -0
- package/src/components/Pagination/Custom/PaginationItem.tsx +166 -0
- package/src/components/Pagination/Custom/index.tsx +111 -0
- package/src/components/Pagination/Pagination.test.tsx +178 -0
- package/src/components/Pagination/index.tsx +7 -0
- package/src/components/ScrollViewGesture.tsx +577 -0
- package/src/components/rnr-demo.test.tsx +53 -0
- package/src/constants/index.ts +11 -0
- package/src/hooks/useAutoPlay.test.ts +194 -0
- package/src/hooks/useAutoPlay.ts +58 -0
- package/src/hooks/useCarouselController.test.tsx +1158 -0
- package/src/hooks/useCarouselController.tsx +525 -0
- package/src/hooks/useCheckMounted.test.ts +47 -0
- package/src/hooks/useCheckMounted.ts +14 -0
- package/src/hooks/useCommonVariables.test.tsx +384 -0
- package/src/hooks/useCommonVariables.ts +202 -0
- package/src/hooks/useInitProps.test.tsx +134 -0
- package/src/hooks/useInitProps.ts +111 -0
- package/src/hooks/useLayoutConfig.test.tsx +247 -0
- package/src/hooks/useLayoutConfig.ts +30 -0
- package/src/hooks/useOffsetX.test.ts +110 -0
- package/src/hooks/useOffsetX.ts +109 -0
- package/src/hooks/useOnProgressChange.test.tsx +207 -0
- package/src/hooks/useOnProgressChange.ts +105 -0
- package/src/hooks/usePanGestureProxy.test.tsx +368 -0
- package/src/hooks/usePanGestureProxy.ts +144 -0
- package/src/hooks/usePropsErrorBoundary.ts +138 -0
- package/src/hooks/useSizeResolver.test.tsx +112 -0
- package/src/hooks/useSizeResolver.ts +106 -0
- package/src/hooks/useUpdateGestureConfig.test.ts +89 -0
- package/src/hooks/useUpdateGestureConfig.ts +14 -0
- package/src/hooks/useVisibleRanges.test.tsx +366 -0
- package/src/hooks/useVisibleRanges.tsx +123 -0
- package/src/index.tsx +13 -0
- package/src/layouts/index.tsx +12 -0
- package/src/layouts/normal.ts +32 -0
- package/src/layouts/parallax.test.ts +239 -0
- package/src/layouts/parallax.ts +83 -0
- package/src/layouts/stack.test.ts +252 -0
- package/src/layouts/stack.ts +306 -0
- package/src/store/index.test.tsx +314 -0
- package/src/store/index.tsx +66 -0
- package/src/types.ts +348 -0
- package/src/utils/compute-gesture-translation.test.ts +70 -0
- package/src/utils/compute-gesture-translation.ts +29 -0
- package/src/utils/compute-offset-if-data-changed.test.ts +133 -0
- package/src/utils/compute-offset-if-data-changed.ts +44 -0
- package/src/utils/compute-offset-if-size-changed.test.ts +78 -0
- package/src/utils/compute-offset-if-size-changed.ts +14 -0
- package/src/utils/compute-offset-if-sizes-changed.test.ts +74 -0
- package/src/utils/compute-offset-if-sizes-changed.ts +44 -0
- package/src/utils/computed-with-auto-fill-data.test.ts +298 -0
- package/src/utils/computed-with-auto-fill-data.ts +92 -0
- package/src/utils/deal-with-animation.test.ts +181 -0
- package/src/utils/deal-with-animation.ts +17 -0
- package/src/utils/handleroffset-direction.test.ts +124 -0
- package/src/utils/handleroffset-direction.ts +18 -0
- package/src/utils/index.test.ts +90 -0
- package/src/utils/log.test.ts +134 -0
- package/src/utils/log.ts +12 -0
- package/src/utils/sanitize-animation-style.test.ts +40 -0
- package/src/utils/sanitize-animation-style.ts +20 -0
- package/src/utils/size-resolver.test.ts +193 -0
- package/src/utils/size-resolver.ts +216 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import type { SharedValue } from "react-native-reanimated";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Worklet-safe abstraction over per-item sizes. Lets every position-math site
|
|
5
|
+
* read `offsetAt(i)` / `indexAt(offset)` instead of multiplying by a scalar
|
|
6
|
+
* `size`. Two implementations:
|
|
7
|
+
*
|
|
8
|
+
* - Uniform: backed by a single scalar; O(1) for every operation.
|
|
9
|
+
* - Variable: backed by a prefix-sum SharedValue<number[]>; O(log n) for
|
|
10
|
+
* `indexAt`, with a cached-hint fast path for sequential scrolls.
|
|
11
|
+
*/
|
|
12
|
+
export interface SizeResolver {
|
|
13
|
+
/**
|
|
14
|
+
* `true` when sizes vary per item (variable mode). UI-thread reads should be
|
|
15
|
+
* cheap — this flag lives outside SharedValues so worklets can branch on it.
|
|
16
|
+
*/
|
|
17
|
+
readonly variable: boolean;
|
|
18
|
+
/** Total span across all items: sum of every item's size. */
|
|
19
|
+
readonly total: SharedValue<number>;
|
|
20
|
+
/** Number of items (must equal `prefix.length - 1` in variable mode). */
|
|
21
|
+
readonly count: SharedValue<number>;
|
|
22
|
+
/** Size of item at `index`. Worklet-safe. */
|
|
23
|
+
itemSize: (index: number) => number;
|
|
24
|
+
/** Start offset of item at `index` (sum of sizes of items [0, index)). Worklet-safe. */
|
|
25
|
+
offsetAt: (index: number) => number;
|
|
26
|
+
/** Index of the item that contains pixel `offset` from the start. Worklet-safe. */
|
|
27
|
+
indexAt: (offset: number) => number;
|
|
28
|
+
/** Center offset of item at `index`: `offsetAt(index) + itemSize(index) / 2`. Worklet-safe. */
|
|
29
|
+
midpointOf: (index: number) => number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Binary search the prefix-sum array for the index `i` such that
|
|
34
|
+
* `prefix[i] <= target < prefix[i+1]`. Clamps to `[0, length-1]`.
|
|
35
|
+
*
|
|
36
|
+
* Worklet-callable. `prefix` should have `length === itemCount + 1` so that
|
|
37
|
+
* `prefix[itemCount]` equals the total span.
|
|
38
|
+
*/
|
|
39
|
+
export function binarySearchPrefix(prefix: number[], target: number): number {
|
|
40
|
+
"worklet";
|
|
41
|
+
const n = prefix.length - 1;
|
|
42
|
+
if (n <= 0) return 0;
|
|
43
|
+
if (target <= prefix[0]) return 0;
|
|
44
|
+
if (target >= prefix[n]) return n - 1;
|
|
45
|
+
|
|
46
|
+
let lo = 0;
|
|
47
|
+
let hi = n - 1;
|
|
48
|
+
while (lo <= hi) {
|
|
49
|
+
const mid = (lo + hi) >> 1;
|
|
50
|
+
const start = prefix[mid];
|
|
51
|
+
const end = prefix[mid + 1];
|
|
52
|
+
if (target < start) hi = mid - 1;
|
|
53
|
+
else if (target >= end) lo = mid + 1;
|
|
54
|
+
else return mid;
|
|
55
|
+
}
|
|
56
|
+
return Math.max(0, Math.min(n - 1, lo));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Cached-hint variant: checks `hint`, `hint+1`, `hint-1` before falling back to
|
|
61
|
+
* full binary search. The common case during a single gesture is that the
|
|
62
|
+
* answer either stays the same or moves by ±1, so this keeps amortized cost at
|
|
63
|
+
* O(1) for typical scroll patterns.
|
|
64
|
+
*/
|
|
65
|
+
export function binarySearchPrefixCached(prefix: number[], target: number, hint: number): number {
|
|
66
|
+
"worklet";
|
|
67
|
+
const n = prefix.length - 1;
|
|
68
|
+
if (n <= 0) return 0;
|
|
69
|
+
|
|
70
|
+
const clampedHint = Math.max(0, Math.min(n - 1, hint));
|
|
71
|
+
// Check hint, hint+1, hint-1 first.
|
|
72
|
+
for (let delta = 0; delta <= 1; delta++) {
|
|
73
|
+
const i = clampedHint + delta;
|
|
74
|
+
if (i >= 0 && i < n && target >= prefix[i] && target < prefix[i + 1]) return i;
|
|
75
|
+
if (delta === 0) continue;
|
|
76
|
+
const j = clampedHint - delta;
|
|
77
|
+
if (j >= 0 && j < n && target >= prefix[j] && target < prefix[j + 1]) return j;
|
|
78
|
+
}
|
|
79
|
+
return binarySearchPrefix(prefix, target);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Creates a resolver for the uniform-size case. The scalar `size` is read on
|
|
84
|
+
* every call so callers always get the latest value. The owner is responsible
|
|
85
|
+
* for keeping `total` in sync with `size * count` (typically via
|
|
86
|
+
* `useAnimatedReaction`).
|
|
87
|
+
*
|
|
88
|
+
* `size` may be `SharedValue<number | null>` — `null` is treated as 0, which
|
|
89
|
+
* lets callers pass `common.resolvedSize` directly without a cast.
|
|
90
|
+
*/
|
|
91
|
+
export function createUniformSizeResolver(opts: {
|
|
92
|
+
size: SharedValue<number | null> | SharedValue<number>;
|
|
93
|
+
count: SharedValue<number>;
|
|
94
|
+
total: SharedValue<number>;
|
|
95
|
+
}): SizeResolver {
|
|
96
|
+
const { size, count, total } = opts;
|
|
97
|
+
|
|
98
|
+
const readSize = () => {
|
|
99
|
+
"worklet";
|
|
100
|
+
const v = (size as SharedValue<number | null>).value;
|
|
101
|
+
return typeof v === "number" ? v : 0;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
variable: false,
|
|
106
|
+
total,
|
|
107
|
+
count,
|
|
108
|
+
itemSize: (_index: number) => {
|
|
109
|
+
"worklet";
|
|
110
|
+
return readSize();
|
|
111
|
+
},
|
|
112
|
+
offsetAt: (index: number) => {
|
|
113
|
+
"worklet";
|
|
114
|
+
return readSize() * index;
|
|
115
|
+
},
|
|
116
|
+
indexAt: (offset: number) => {
|
|
117
|
+
"worklet";
|
|
118
|
+
const s = readSize();
|
|
119
|
+
if (s <= 0) return 0;
|
|
120
|
+
return Math.round(offset / s);
|
|
121
|
+
},
|
|
122
|
+
midpointOf: (index: number) => {
|
|
123
|
+
"worklet";
|
|
124
|
+
const s = readSize();
|
|
125
|
+
return s * index + s / 2;
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Creates a resolver backed by a prefix-sum array. `prefix[i]` is the sum of
|
|
132
|
+
* sizes of items `[0, i)`, so `prefix[0] === 0` and `prefix[count] === total`.
|
|
133
|
+
* `sizes` is `[size(0), size(1), ..., size(count-1)]`.
|
|
134
|
+
*
|
|
135
|
+
* `hint` is an optional SharedValue used as the seed for `indexAt`'s cached
|
|
136
|
+
* binary search; passing the previous result keeps lookups O(1) during
|
|
137
|
+
* continuous scrolls.
|
|
138
|
+
*/
|
|
139
|
+
export function createVariableSizeResolver(opts: {
|
|
140
|
+
sizes: SharedValue<number[]>;
|
|
141
|
+
prefix: SharedValue<number[]>;
|
|
142
|
+
total: SharedValue<number>;
|
|
143
|
+
count: SharedValue<number>;
|
|
144
|
+
hint?: SharedValue<number>;
|
|
145
|
+
}): SizeResolver {
|
|
146
|
+
const { sizes, prefix, total, count, hint } = opts;
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
variable: true,
|
|
150
|
+
total,
|
|
151
|
+
count,
|
|
152
|
+
itemSize: (index: number) => {
|
|
153
|
+
"worklet";
|
|
154
|
+
const arr = sizes.value;
|
|
155
|
+
const n = arr.length;
|
|
156
|
+
if (n === 0) return 0;
|
|
157
|
+
if (index < 0) return arr[0];
|
|
158
|
+
if (index >= n) return arr[n - 1];
|
|
159
|
+
return arr[index];
|
|
160
|
+
},
|
|
161
|
+
offsetAt: (index: number) => {
|
|
162
|
+
"worklet";
|
|
163
|
+
const arr = prefix.value;
|
|
164
|
+
const n = arr.length;
|
|
165
|
+
if (n === 0) return 0;
|
|
166
|
+
if (index <= 0) return 0;
|
|
167
|
+
if (index >= n) return arr[n - 1];
|
|
168
|
+
return arr[index];
|
|
169
|
+
},
|
|
170
|
+
indexAt: (offset: number) => {
|
|
171
|
+
"worklet";
|
|
172
|
+
const arr = prefix.value;
|
|
173
|
+
if (arr.length <= 1) return 0;
|
|
174
|
+
if (hint) {
|
|
175
|
+
const found = binarySearchPrefixCached(arr, offset, hint.value);
|
|
176
|
+
hint.value = found;
|
|
177
|
+
return found;
|
|
178
|
+
}
|
|
179
|
+
return binarySearchPrefix(arr, offset);
|
|
180
|
+
},
|
|
181
|
+
midpointOf: (index: number) => {
|
|
182
|
+
"worklet";
|
|
183
|
+
const prefixArr = prefix.value;
|
|
184
|
+
const sizesArr = sizes.value;
|
|
185
|
+
const n = sizesArr.length;
|
|
186
|
+
if (n === 0) return 0;
|
|
187
|
+
const clamped = Math.max(0, Math.min(n - 1, index));
|
|
188
|
+
return prefixArr[clamped] + sizesArr[clamped] / 2;
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Builds a `(sizes, prefix, total)` tuple from a `getItemSize(index)` getter.
|
|
195
|
+
* JS-thread, O(n). Returns plain arrays — callers ferry them into SharedValues.
|
|
196
|
+
*
|
|
197
|
+
* Guards against non-finite or non-positive sizes by treating them as 0 so the
|
|
198
|
+
* carousel doesn't NaN-out. `usePropsErrorBoundary` warns the user separately.
|
|
199
|
+
*/
|
|
200
|
+
export function buildPrefixSum(
|
|
201
|
+
count: number,
|
|
202
|
+
getItemSize: (index: number) => number
|
|
203
|
+
): { sizes: number[]; prefix: number[]; total: number } {
|
|
204
|
+
const sizes = new Array<number>(count);
|
|
205
|
+
const prefix = new Array<number>(count + 1);
|
|
206
|
+
prefix[0] = 0;
|
|
207
|
+
let running = 0;
|
|
208
|
+
for (let i = 0; i < count; i++) {
|
|
209
|
+
const raw = getItemSize(i);
|
|
210
|
+
const safe = Number.isFinite(raw) && raw > 0 ? raw : 0;
|
|
211
|
+
sizes[i] = safe;
|
|
212
|
+
running += safe;
|
|
213
|
+
prefix[i + 1] = running;
|
|
214
|
+
}
|
|
215
|
+
return { sizes, prefix, total: running };
|
|
216
|
+
}
|