@react-native-ohos/flash-list 1.6.4-rc.1
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 +7 -0
- package/README.OpenSource +11 -0
- package/README.md +9 -0
- package/dist/AnimatedFlashList.d.ts +6 -0
- package/dist/AnimatedFlashList.d.ts.map +1 -0
- package/dist/AnimatedFlashList.js +8 -0
- package/dist/AnimatedFlashList.js.map +1 -0
- package/dist/FlashList.d.ts +126 -0
- package/dist/FlashList.d.ts.map +1 -0
- package/dist/FlashList.js +524 -0
- package/dist/FlashList.js.map +1 -0
- package/dist/FlashListProps.d.ts +256 -0
- package/dist/FlashListProps.d.ts.map +1 -0
- package/dist/FlashListProps.js +9 -0
- package/dist/FlashListProps.js.map +1 -0
- package/dist/GridLayoutProviderWithProps.d.ts +42 -0
- package/dist/GridLayoutProviderWithProps.d.ts.map +1 -0
- package/dist/GridLayoutProviderWithProps.js +115 -0
- package/dist/GridLayoutProviderWithProps.js.map +1 -0
- package/dist/MasonryFlashList.d.ts +51 -0
- package/dist/MasonryFlashList.d.ts.map +1 -0
- package/dist/MasonryFlashList.js +252 -0
- package/dist/MasonryFlashList.js.map +1 -0
- package/dist/PureComponentWrapper.d.ts +22 -0
- package/dist/PureComponentWrapper.d.ts.map +1 -0
- package/dist/PureComponentWrapper.js +37 -0
- package/dist/PureComponentWrapper.js.map +1 -0
- package/dist/__tests__/AverageWindow.test.d.ts +2 -0
- package/dist/__tests__/AverageWindow.test.d.ts.map +1 -0
- package/dist/__tests__/AverageWindow.test.js +69 -0
- package/dist/__tests__/AverageWindow.test.js.map +1 -0
- package/dist/__tests__/ContentContainerUtils.test.d.ts +2 -0
- package/dist/__tests__/ContentContainerUtils.test.d.ts.map +1 -0
- package/dist/__tests__/ContentContainerUtils.test.js +85 -0
- package/dist/__tests__/ContentContainerUtils.test.js.map +1 -0
- package/dist/__tests__/FlashList.test.d.ts +2 -0
- package/dist/__tests__/FlashList.test.d.ts.map +1 -0
- package/dist/__tests__/FlashList.test.js +792 -0
- package/dist/__tests__/FlashList.test.js.map +1 -0
- package/dist/__tests__/GridLayoutProviderWithProps.test.d.ts +2 -0
- package/dist/__tests__/GridLayoutProviderWithProps.test.d.ts.map +1 -0
- package/dist/__tests__/GridLayoutProviderWithProps.test.js +143 -0
- package/dist/__tests__/GridLayoutProviderWithProps.test.js.map +1 -0
- package/dist/__tests__/MasonryFlashList.test.d.ts +2 -0
- package/dist/__tests__/MasonryFlashList.test.d.ts.map +1 -0
- package/dist/__tests__/MasonryFlashList.test.js +254 -0
- package/dist/__tests__/MasonryFlashList.test.js.map +1 -0
- package/dist/__tests__/PlatformHelper.web.test.d.ts +2 -0
- package/dist/__tests__/PlatformHelper.web.test.d.ts.map +1 -0
- package/dist/__tests__/PlatformHelper.web.test.js +33 -0
- package/dist/__tests__/PlatformHelper.web.test.js.map +1 -0
- package/dist/__tests__/ViewabilityHelper.test.d.ts +2 -0
- package/dist/__tests__/ViewabilityHelper.test.d.ts.map +1 -0
- package/dist/__tests__/ViewabilityHelper.test.js +187 -0
- package/dist/__tests__/ViewabilityHelper.test.js.map +1 -0
- package/dist/__tests__/helpers/mountFlashList.d.ts +19 -0
- package/dist/__tests__/helpers/mountFlashList.d.ts.map +1 -0
- package/dist/__tests__/helpers/mountFlashList.js +44 -0
- package/dist/__tests__/helpers/mountFlashList.js.map +1 -0
- package/dist/__tests__/helpers/mountMasonryFlashList.d.ts +18 -0
- package/dist/__tests__/helpers/mountMasonryFlashList.d.ts.map +1 -0
- package/dist/__tests__/helpers/mountMasonryFlashList.js +49 -0
- package/dist/__tests__/helpers/mountMasonryFlashList.js.map +1 -0
- package/dist/__tests__/useBlankAreaTracker.test.d.ts +2 -0
- package/dist/__tests__/useBlankAreaTracker.test.d.ts.map +1 -0
- package/dist/__tests__/useBlankAreaTracker.test.js +177 -0
- package/dist/__tests__/useBlankAreaTracker.test.js.map +1 -0
- package/dist/benchmark/AutoScrollHelper.d.ts +18 -0
- package/dist/benchmark/AutoScrollHelper.d.ts.map +1 -0
- package/dist/benchmark/AutoScrollHelper.js +68 -0
- package/dist/benchmark/AutoScrollHelper.js.map +1 -0
- package/dist/benchmark/JSFPSMonitor.d.ts +23 -0
- package/dist/benchmark/JSFPSMonitor.d.ts.map +1 -0
- package/dist/benchmark/JSFPSMonitor.js +65 -0
- package/dist/benchmark/JSFPSMonitor.js.map +1 -0
- package/dist/benchmark/roundToDecimalPlaces.d.ts +2 -0
- package/dist/benchmark/roundToDecimalPlaces.d.ts.map +1 -0
- package/dist/benchmark/roundToDecimalPlaces.js +9 -0
- package/dist/benchmark/roundToDecimalPlaces.js.map +1 -0
- package/dist/benchmark/useBenchmark.d.ts +35 -0
- package/dist/benchmark/useBenchmark.d.ts.map +1 -0
- package/dist/benchmark/useBenchmark.js +167 -0
- package/dist/benchmark/useBenchmark.js.map +1 -0
- package/dist/benchmark/useBlankAreaTracker.d.ts +34 -0
- package/dist/benchmark/useBlankAreaTracker.d.ts.map +1 -0
- package/dist/benchmark/useBlankAreaTracker.js +67 -0
- package/dist/benchmark/useBlankAreaTracker.js.map +1 -0
- package/dist/benchmark/useDataMultiplier.d.ts +9 -0
- package/dist/benchmark/useDataMultiplier.d.ts.map +1 -0
- package/dist/benchmark/useDataMultiplier.js +25 -0
- package/dist/benchmark/useDataMultiplier.js.map +1 -0
- package/dist/benchmark/useFlatListBenchmark.d.ts +13 -0
- package/dist/benchmark/useFlatListBenchmark.d.ts.map +1 -0
- package/dist/benchmark/useFlatListBenchmark.js +100 -0
- package/dist/benchmark/useFlatListBenchmark.js.map +1 -0
- package/dist/errors/CustomError.d.ts +8 -0
- package/dist/errors/CustomError.d.ts.map +1 -0
- package/dist/errors/CustomError.js +14 -0
- package/dist/errors/CustomError.js.map +1 -0
- package/dist/errors/ExceptionList.d.ts +24 -0
- package/dist/errors/ExceptionList.d.ts.map +1 -0
- package/dist/errors/ExceptionList.js +26 -0
- package/dist/errors/ExceptionList.js.map +1 -0
- package/dist/errors/Warnings.d.ts +9 -0
- package/dist/errors/Warnings.d.ts.map +1 -0
- package/dist/errors/Warnings.js +13 -0
- package/dist/errors/Warnings.js.map +1 -0
- package/dist/fabric/AutoLayoutNativeComponent.d.ts +19 -0
- package/dist/fabric/AutoLayoutNativeComponent.d.ts.map +1 -0
- package/dist/fabric/AutoLayoutNativeComponent.js +29 -0
- package/dist/fabric/AutoLayoutNativeComponent.js.map +1 -0
- package/dist/fabric/CellContainerNativeComponent.d.ts +8 -0
- package/dist/fabric/CellContainerNativeComponent.d.ts.map +1 -0
- package/dist/fabric/CellContainerNativeComponent.js +29 -0
- package/dist/fabric/CellContainerNativeComponent.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/native/auto-layout/AutoLayoutView.d.ts +22 -0
- package/dist/native/auto-layout/AutoLayoutView.d.ts.map +1 -0
- package/dist/native/auto-layout/AutoLayoutView.js +48 -0
- package/dist/native/auto-layout/AutoLayoutView.js.map +1 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.android.d.ts +4 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.android.d.ts.map +1 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.android.js +6 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.android.js.map +1 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.d.ts +5 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.d.ts.map +1 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.harmony.d.ts +4 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.harmony.d.ts.map +1 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.harmony.js +29 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.harmony.js.map +1 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.ios.d.ts +4 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.ios.d.ts.map +1 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.ios.js +6 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.ios.js.map +1 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.js +6 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponent.js.map +1 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponentProps.d.ts +16 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponentProps.d.ts.map +1 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponentProps.js +3 -0
- package/dist/native/auto-layout/AutoLayoutViewNativeComponentProps.js.map +1 -0
- package/dist/native/cell-container/CellContainer.android.d.ts +6 -0
- package/dist/native/cell-container/CellContainer.android.d.ts.map +1 -0
- package/dist/native/cell-container/CellContainer.android.js +9 -0
- package/dist/native/cell-container/CellContainer.android.js.map +1 -0
- package/dist/native/cell-container/CellContainer.d.ts +8 -0
- package/dist/native/cell-container/CellContainer.d.ts.map +1 -0
- package/dist/native/cell-container/CellContainer.harmony.d.ts +6 -0
- package/dist/native/cell-container/CellContainer.harmony.d.ts.map +1 -0
- package/dist/native/cell-container/CellContainer.harmony.js +32 -0
- package/dist/native/cell-container/CellContainer.harmony.js.map +1 -0
- package/dist/native/cell-container/CellContainer.ios.d.ts +6 -0
- package/dist/native/cell-container/CellContainer.ios.d.ts.map +1 -0
- package/dist/native/cell-container/CellContainer.ios.js +9 -0
- package/dist/native/cell-container/CellContainer.ios.js.map +1 -0
- package/dist/native/cell-container/CellContainer.js +11 -0
- package/dist/native/cell-container/CellContainer.js.map +1 -0
- package/dist/native/cell-container/CellContainer.web.d.ts +7 -0
- package/dist/native/cell-container/CellContainer.web.d.ts.map +1 -0
- package/dist/native/cell-container/CellContainer.web.js +13 -0
- package/dist/native/cell-container/CellContainer.web.js.map +1 -0
- package/dist/native/config/PlatformHelper.android.d.ts +26 -0
- package/dist/native/config/PlatformHelper.android.d.ts.map +1 -0
- package/dist/native/config/PlatformHelper.android.js +23 -0
- package/dist/native/config/PlatformHelper.android.js.map +1 -0
- package/dist/native/config/PlatformHelper.d.ts +26 -0
- package/dist/native/config/PlatformHelper.d.ts.map +1 -0
- package/dist/native/config/PlatformHelper.harmony.d.ts +26 -0
- package/dist/native/config/PlatformHelper.harmony.d.ts.map +1 -0
- package/dist/native/config/PlatformHelper.harmony.js +22 -0
- package/dist/native/config/PlatformHelper.harmony.js.map +1 -0
- package/dist/native/config/PlatformHelper.ios.d.ts +26 -0
- package/dist/native/config/PlatformHelper.ios.d.ts.map +1 -0
- package/dist/native/config/PlatformHelper.ios.js +22 -0
- package/dist/native/config/PlatformHelper.ios.js.map +1 -0
- package/dist/native/config/PlatformHelper.js +23 -0
- package/dist/native/config/PlatformHelper.js.map +1 -0
- package/dist/native/config/PlatformHelper.web.d.ts +27 -0
- package/dist/native/config/PlatformHelper.web.d.ts.map +1 -0
- package/dist/native/config/PlatformHelper.web.js +25 -0
- package/dist/native/config/PlatformHelper.web.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/utils/AverageWindow.d.ts +21 -0
- package/dist/utils/AverageWindow.d.ts.map +1 -0
- package/dist/utils/AverageWindow.js +49 -0
- package/dist/utils/AverageWindow.js.map +1 -0
- package/dist/utils/ContentContainerUtils.d.ts +27 -0
- package/dist/utils/ContentContainerUtils.d.ts.map +1 -0
- package/dist/utils/ContentContainerUtils.js +48 -0
- package/dist/utils/ContentContainerUtils.js.map +1 -0
- package/dist/viewability/ViewToken.d.ts +8 -0
- package/dist/viewability/ViewToken.d.ts.map +1 -0
- package/dist/viewability/ViewToken.js +3 -0
- package/dist/viewability/ViewToken.js.map +1 -0
- package/dist/viewability/ViewabilityHelper.d.ts +25 -0
- package/dist/viewability/ViewabilityHelper.d.ts.map +1 -0
- package/dist/viewability/ViewabilityHelper.js +104 -0
- package/dist/viewability/ViewabilityHelper.js.map +1 -0
- package/dist/viewability/ViewabilityManager.d.ts +24 -0
- package/dist/viewability/ViewabilityManager.d.ts.map +1 -0
- package/dist/viewability/ViewabilityManager.js +94 -0
- package/dist/viewability/ViewabilityManager.js.map +1 -0
- package/harmony/flash_list/BuildProfile.ets +5 -0
- package/harmony/flash_list/LICENSE +7 -0
- package/harmony/flash_list/build-profile.json5 +8 -0
- package/harmony/flash_list/hvigorfile.ts +1 -0
- package/harmony/flash_list/index.ets +29 -0
- package/harmony/flash_list/obfuscation-rules.txt +18 -0
- package/harmony/flash_list/oh-package-lock.json5 +17 -0
- package/harmony/flash_list/oh-package.json5 +13 -0
- package/harmony/flash_list/src/main/cpp/AutoLayoutNode.cpp +63 -0
- package/harmony/flash_list/src/main/cpp/AutoLayoutNode.h +57 -0
- package/harmony/flash_list/src/main/cpp/AutoLayoutShadow.cpp +140 -0
- package/harmony/flash_list/src/main/cpp/AutoLayoutShadow.h +66 -0
- package/harmony/flash_list/src/main/cpp/AutoLayoutViewComponentInstance.cpp +208 -0
- package/harmony/flash_list/src/main/cpp/AutoLayoutViewComponentInstance.h +78 -0
- package/harmony/flash_list/src/main/cpp/AutoLayoutViewEventEmitRequestHandler.h +53 -0
- package/harmony/flash_list/src/main/cpp/AutoLayoutViewJSIBinder.h +50 -0
- package/harmony/flash_list/src/main/cpp/CMakeLists.txt +7 -0
- package/harmony/flash_list/src/main/cpp/CellContainerComponentInstance.cpp +80 -0
- package/harmony/flash_list/src/main/cpp/CellContainerComponentInstance.h +67 -0
- package/harmony/flash_list/src/main/cpp/CellContainerJSIBinder.h +39 -0
- package/harmony/flash_list/src/main/cpp/ComponentDescriptors.h +40 -0
- package/harmony/flash_list/src/main/cpp/EventEmitters.cpp +40 -0
- package/harmony/flash_list/src/main/cpp/EventEmitters.h +48 -0
- package/harmony/flash_list/src/main/cpp/FlashListPackage.h +78 -0
- package/harmony/flash_list/src/main/cpp/FlashListStackNode.cpp +89 -0
- package/harmony/flash_list/src/main/cpp/FlashListStackNode.h +59 -0
- package/harmony/flash_list/src/main/cpp/Props.cpp +52 -0
- package/harmony/flash_list/src/main/cpp/Props.h +64 -0
- package/harmony/flash_list/src/main/cpp/ShadowNodes.cpp +34 -0
- package/harmony/flash_list/src/main/cpp/ShadowNodes.h +48 -0
- package/harmony/flash_list/src/main/ets/Logger.ets +64 -0
- package/harmony/flash_list/src/main/ets/RNAutoLayoutShadow.ets +154 -0
- package/harmony/flash_list/src/main/ets/RNAutoLayoutView.ets +252 -0
- package/harmony/flash_list/src/main/ets/RNCellContainer.ets +113 -0
- package/harmony/flash_list/src/main/module.json5 +7 -0
- package/harmony/flash_list/src/main/resources/base/element/color.json +8 -0
- package/harmony/flash_list/src/main/resources/base/element/string.json +16 -0
- package/harmony/flash_list/src/main/resources/base/media/icon.png +0 -0
- package/harmony/flash_list/src/main/resources/base/profile/main_pages.json +5 -0
- package/harmony/flash_list/src/main/resources/en_US/element/string.json +16 -0
- package/harmony/flash_list/src/main/resources/zh_CN/element/string.json +16 -0
- package/harmony/flash_list.har +0 -0
- package/jestSetup.js +16 -0
- package/package.json +88 -0
- package/src/AnimatedFlashList.ts +11 -0
- package/src/FlashList.tsx +870 -0
- package/src/FlashListProps.ts +335 -0
- package/src/GridLayoutProviderWithProps.ts +180 -0
- package/src/MasonryFlashList.tsx +476 -0
- package/src/PureComponentWrapper.tsx +42 -0
- package/src/__tests__/AverageWindow.test.ts +80 -0
- package/src/__tests__/ContentContainerUtils.test.ts +130 -0
- package/src/__tests__/FlashList.test.tsx +886 -0
- package/src/__tests__/GridLayoutProviderWithProps.test.ts +179 -0
- package/src/__tests__/MasonryFlashList.test.ts +292 -0
- package/src/__tests__/PlatformHelper.web.test.ts +45 -0
- package/src/__tests__/ViewabilityHelper.test.ts +283 -0
- package/src/__tests__/helpers/mountFlashList.tsx +62 -0
- package/src/__tests__/helpers/mountMasonryFlashList.tsx +70 -0
- package/src/__tests__/useBlankAreaTracker.test.tsx +204 -0
- package/src/benchmark/AutoScrollHelper.ts +70 -0
- package/src/benchmark/JSFPSMonitor.ts +74 -0
- package/src/benchmark/roundToDecimalPlaces.ts +4 -0
- package/src/benchmark/useBenchmark.ts +240 -0
- package/src/benchmark/useBlankAreaTracker.ts +117 -0
- package/src/benchmark/useDataMultiplier.ts +19 -0
- package/src/benchmark/useFlatListBenchmark.ts +107 -0
- package/src/errors/CustomError.ts +10 -0
- package/src/errors/ExceptionList.ts +28 -0
- package/src/errors/Warnings.ts +15 -0
- package/src/fabric/AutoLayoutNativeComponent.ts +51 -0
- package/src/fabric/CellContainerNativeComponent.ts +34 -0
- package/src/index.ts +43 -0
- package/src/native/auto-layout/AutoLayoutView.tsx +73 -0
- package/src/native/auto-layout/AutoLayoutViewNativeComponent.android.ts +7 -0
- package/src/native/auto-layout/AutoLayoutViewNativeComponent.harmony.ts +30 -0
- package/src/native/auto-layout/AutoLayoutViewNativeComponent.ios.ts +7 -0
- package/src/native/auto-layout/AutoLayoutViewNativeComponent.ts +7 -0
- package/src/native/auto-layout/AutoLayoutViewNativeComponentProps.ts +17 -0
- package/src/native/cell-container/CellContainer.android.ts +7 -0
- package/src/native/cell-container/CellContainer.harmony.ts +30 -0
- package/src/native/cell-container/CellContainer.ios.ts +6 -0
- package/src/native/cell-container/CellContainer.tsx +14 -0
- package/src/native/cell-container/CellContainer.web.tsx +9 -0
- package/src/native/config/PlatformHelper.android.ts +29 -0
- package/src/native/config/PlatformHelper.harmony.ts +51 -0
- package/src/native/config/PlatformHelper.ios.ts +28 -0
- package/src/native/config/PlatformHelper.ts +29 -0
- package/src/native/config/PlatformHelper.web.ts +34 -0
- package/src/utils/AverageWindow.ts +49 -0
- package/src/utils/ContentContainerUtils.ts +92 -0
- package/src/viewability/ViewToken.ts +7 -0
- package/src/viewability/ViewabilityHelper.ts +162 -0
- package/src/viewability/ViewabilityManager.ts +134 -0
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
import React, { useCallback, useRef, useEffect, useMemo } from "react";
|
|
2
|
+
import {
|
|
3
|
+
View,
|
|
4
|
+
Dimensions,
|
|
5
|
+
ScrollViewProps,
|
|
6
|
+
LayoutChangeEvent,
|
|
7
|
+
NativeScrollEvent,
|
|
8
|
+
NativeSyntheticEvent,
|
|
9
|
+
} from "react-native";
|
|
10
|
+
|
|
11
|
+
import CustomError from "./errors/CustomError";
|
|
12
|
+
import ExceptionList from "./errors/ExceptionList";
|
|
13
|
+
import FlashList from "./FlashList";
|
|
14
|
+
import { FlashListProps, ListRenderItemInfo } from "./FlashListProps";
|
|
15
|
+
import { applyContentContainerInsetForLayoutManager } from "./utils/ContentContainerUtils";
|
|
16
|
+
import ViewToken from "./viewability/ViewToken";
|
|
17
|
+
|
|
18
|
+
export interface MasonryListRenderItemInfo<TItem>
|
|
19
|
+
extends ListRenderItemInfo<TItem> {
|
|
20
|
+
columnSpan: number;
|
|
21
|
+
columnIndex: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type MasonryListRenderItem<TItem> = (
|
|
25
|
+
info: MasonryListRenderItemInfo<TItem>
|
|
26
|
+
) => React.ReactElement | null;
|
|
27
|
+
|
|
28
|
+
export interface MasonryFlashListProps<T>
|
|
29
|
+
extends Omit<
|
|
30
|
+
FlashListProps<T>,
|
|
31
|
+
| "horizontal"
|
|
32
|
+
| "initialScrollIndex"
|
|
33
|
+
| "inverted"
|
|
34
|
+
| "onBlankArea"
|
|
35
|
+
| "renderItem"
|
|
36
|
+
| "viewabilityConfigCallbackPairs"
|
|
37
|
+
> {
|
|
38
|
+
/**
|
|
39
|
+
* Allows you to change the column widths of the list. This is helpful if you want some columns to be wider than the others.
|
|
40
|
+
* e.g, if `numColumns` is `3`, you can return `2` for `index 1` and `1` for the rest to achieve a `1:2:1` split by width.
|
|
41
|
+
*/
|
|
42
|
+
getColumnFlex?: (
|
|
43
|
+
items: MasonryListItem<T>[],
|
|
44
|
+
columnIndex: number,
|
|
45
|
+
maxColumns: number,
|
|
46
|
+
extraData?: any
|
|
47
|
+
) => number;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* If enabled, MasonryFlashList will try to reduce difference in column height by modifying item order.
|
|
51
|
+
* `overrideItemLayout` is required to make this work.
|
|
52
|
+
*/
|
|
53
|
+
optimizeItemArrangement?: boolean;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Extends typical `renderItem` to include `columnIndex` and `columnSpan` (number of columns the item spans).
|
|
57
|
+
* `columnIndex` gives the consumer column information in case they might need to treat items differently based on column.
|
|
58
|
+
* This information may not otherwise be derived if using the `optimizeItemArrangement` feature, as the items will no
|
|
59
|
+
* longer be linearly distributed across the columns; instead they are allocated to the column with the least estimated height.
|
|
60
|
+
*/
|
|
61
|
+
renderItem: MasonryListRenderItem<T> | null | undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type OnScrollCallback = ScrollViewProps["onScroll"];
|
|
65
|
+
const defaultEstimatedItemSize = 100;
|
|
66
|
+
|
|
67
|
+
export interface MasonryFlashListScrollEvent extends NativeScrollEvent {
|
|
68
|
+
doNotPropagate?: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface MasonryListItem<T> {
|
|
72
|
+
originalIndex: number;
|
|
73
|
+
originalItem: T;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* MasonryFlashListRef with support for scroll related methods
|
|
78
|
+
*/
|
|
79
|
+
export interface MasonryFlashListRef<T> {
|
|
80
|
+
scrollToOffset: FlashList<T>["scrollToOffset"];
|
|
81
|
+
scrollToEnd: FlashList<T>["scrollToEnd"];
|
|
82
|
+
getScrollableNode: FlashList<T>["getScrollableNode"];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* FlashList variant that enables rendering of masonry layouts.
|
|
87
|
+
* If you want `MasonryFlashList` to optimize item arrangement, enable `optimizeItemArrangement` and pass a valid `overrideItemLayout` function.
|
|
88
|
+
*/
|
|
89
|
+
const MasonryFlashListComponent = React.forwardRef(
|
|
90
|
+
<T,>(
|
|
91
|
+
/**
|
|
92
|
+
* Forward Ref will force cast generic parament T to unknown. Export has a explicit cast to solve this.
|
|
93
|
+
*/
|
|
94
|
+
props: MasonryFlashListProps<T>,
|
|
95
|
+
forwardRef: React.ForwardedRef<MasonryFlashListRef<T>>
|
|
96
|
+
) => {
|
|
97
|
+
const columnCount = props.numColumns || 1;
|
|
98
|
+
const drawDistance = props.drawDistance;
|
|
99
|
+
const estimatedListSize = props.estimatedListSize ??
|
|
100
|
+
Dimensions.get("window") ?? { height: 500, width: 500 };
|
|
101
|
+
|
|
102
|
+
if (props.optimizeItemArrangement && !props.overrideItemLayout) {
|
|
103
|
+
throw new CustomError(
|
|
104
|
+
ExceptionList.overrideItemLayoutRequiredForMasonryOptimization
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
const dataSet = useDataSet(
|
|
108
|
+
columnCount,
|
|
109
|
+
Boolean(props.optimizeItemArrangement),
|
|
110
|
+
props.data,
|
|
111
|
+
props.overrideItemLayout,
|
|
112
|
+
props.extraData
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const totalColumnFlex = useTotalColumnFlex(dataSet, props);
|
|
116
|
+
|
|
117
|
+
const propsRef = useRef(props);
|
|
118
|
+
propsRef.current = props;
|
|
119
|
+
|
|
120
|
+
const onScrollRef = useRef<OnScrollCallback[]>([]);
|
|
121
|
+
const emptyScrollEvent = useRef(getEmptyScrollEvent())
|
|
122
|
+
.current as NativeSyntheticEvent<MasonryFlashListScrollEvent>;
|
|
123
|
+
const ScrollComponent = useRef(
|
|
124
|
+
getFlashListScrollView(onScrollRef, () => {
|
|
125
|
+
return (
|
|
126
|
+
getListRenderedSize(parentFlashList)?.height ||
|
|
127
|
+
estimatedListSize.height
|
|
128
|
+
);
|
|
129
|
+
})
|
|
130
|
+
).current;
|
|
131
|
+
|
|
132
|
+
const onScrollProxy = useRef<OnScrollCallback>(
|
|
133
|
+
(scrollEvent: NativeSyntheticEvent<MasonryFlashListScrollEvent>) => {
|
|
134
|
+
emptyScrollEvent.nativeEvent.contentOffset.y =
|
|
135
|
+
scrollEvent.nativeEvent.contentOffset.y -
|
|
136
|
+
(parentFlashList.current?.firstItemOffset ?? 0);
|
|
137
|
+
onScrollRef.current?.forEach((onScrollCallback) => {
|
|
138
|
+
onScrollCallback?.(emptyScrollEvent);
|
|
139
|
+
});
|
|
140
|
+
if (!scrollEvent.nativeEvent.doNotPropagate) {
|
|
141
|
+
propsRef.current.onScroll?.(scrollEvent);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
).current;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* We're triggering an onScroll on internal lists so that they register the correct offset which is offset - header size.
|
|
148
|
+
* This will make sure viewability callbacks are triggered correctly.
|
|
149
|
+
* 32 ms is equal to two frames at 60 fps. Faster framerates will not cause any problems.
|
|
150
|
+
*/
|
|
151
|
+
const onLoadForNestedLists = useRef((args: { elapsedTimeInMs: number }) => {
|
|
152
|
+
setTimeout(() => {
|
|
153
|
+
emptyScrollEvent.nativeEvent.doNotPropagate = true;
|
|
154
|
+
onScrollProxy?.(emptyScrollEvent);
|
|
155
|
+
emptyScrollEvent.nativeEvent.doNotPropagate = false;
|
|
156
|
+
}, 32);
|
|
157
|
+
propsRef.current.onLoad?.(args);
|
|
158
|
+
}).current;
|
|
159
|
+
|
|
160
|
+
const [parentFlashList, getFlashList] =
|
|
161
|
+
useRefWithForwardRef<FlashList<MasonryListItem<T>[]>>(forwardRef);
|
|
162
|
+
|
|
163
|
+
const {
|
|
164
|
+
renderItem,
|
|
165
|
+
getItemType,
|
|
166
|
+
getColumnFlex,
|
|
167
|
+
overrideItemLayout,
|
|
168
|
+
viewabilityConfig,
|
|
169
|
+
keyExtractor,
|
|
170
|
+
onLoad,
|
|
171
|
+
onViewableItemsChanged,
|
|
172
|
+
data,
|
|
173
|
+
stickyHeaderIndices,
|
|
174
|
+
CellRendererComponent,
|
|
175
|
+
ItemSeparatorComponent,
|
|
176
|
+
...remainingProps
|
|
177
|
+
} = props;
|
|
178
|
+
|
|
179
|
+
const firstColumnHeight =
|
|
180
|
+
(dataSet[0]?.length ?? 0) *
|
|
181
|
+
(props.estimatedItemSize ?? defaultEstimatedItemSize);
|
|
182
|
+
|
|
183
|
+
const insetForLayoutManager = applyContentContainerInsetForLayoutManager(
|
|
184
|
+
{ height: 0, width: 0 },
|
|
185
|
+
props.contentContainerStyle,
|
|
186
|
+
false
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<FlashList
|
|
191
|
+
ref={getFlashList}
|
|
192
|
+
{...remainingProps}
|
|
193
|
+
horizontal={false}
|
|
194
|
+
numColumns={columnCount}
|
|
195
|
+
data={dataSet}
|
|
196
|
+
onScroll={onScrollProxy}
|
|
197
|
+
estimatedItemSize={firstColumnHeight || estimatedListSize.height}
|
|
198
|
+
renderItem={(args) => {
|
|
199
|
+
return (
|
|
200
|
+
<FlashList
|
|
201
|
+
renderScrollComponent={ScrollComponent}
|
|
202
|
+
estimatedItemSize={props.estimatedItemSize}
|
|
203
|
+
data={args.item}
|
|
204
|
+
onLoad={args.index === 0 ? onLoadForNestedLists : undefined}
|
|
205
|
+
renderItem={(innerArgs) => {
|
|
206
|
+
return (
|
|
207
|
+
renderItem?.({
|
|
208
|
+
...innerArgs,
|
|
209
|
+
item: innerArgs.item.originalItem,
|
|
210
|
+
index: innerArgs.item.originalIndex,
|
|
211
|
+
columnSpan: 1,
|
|
212
|
+
columnIndex: args.index,
|
|
213
|
+
}) ?? null
|
|
214
|
+
);
|
|
215
|
+
}}
|
|
216
|
+
keyExtractor={
|
|
217
|
+
keyExtractor
|
|
218
|
+
? (item, _) => {
|
|
219
|
+
return keyExtractor?.(
|
|
220
|
+
item.originalItem,
|
|
221
|
+
item.originalIndex
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
: undefined
|
|
225
|
+
}
|
|
226
|
+
getItemType={
|
|
227
|
+
getItemType
|
|
228
|
+
? (item, _, extraData) => {
|
|
229
|
+
return getItemType?.(
|
|
230
|
+
item.originalItem,
|
|
231
|
+
item.originalIndex,
|
|
232
|
+
extraData
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
: undefined
|
|
236
|
+
}
|
|
237
|
+
drawDistance={drawDistance}
|
|
238
|
+
estimatedListSize={{
|
|
239
|
+
height: estimatedListSize.height,
|
|
240
|
+
width:
|
|
241
|
+
(((getListRenderedSize(parentFlashList)?.width ||
|
|
242
|
+
estimatedListSize.width) +
|
|
243
|
+
insetForLayoutManager.width) /
|
|
244
|
+
totalColumnFlex) *
|
|
245
|
+
(getColumnFlex?.(
|
|
246
|
+
args.item,
|
|
247
|
+
args.index,
|
|
248
|
+
columnCount,
|
|
249
|
+
props.extraData
|
|
250
|
+
) ?? 1),
|
|
251
|
+
}}
|
|
252
|
+
extraData={props.extraData}
|
|
253
|
+
CellRendererComponent={CellRendererComponent}
|
|
254
|
+
ItemSeparatorComponent={ItemSeparatorComponent}
|
|
255
|
+
viewabilityConfig={viewabilityConfig}
|
|
256
|
+
onViewableItemsChanged={
|
|
257
|
+
onViewableItemsChanged
|
|
258
|
+
? (info) => {
|
|
259
|
+
updateViewTokens(info.viewableItems);
|
|
260
|
+
updateViewTokens(info.changed);
|
|
261
|
+
onViewableItemsChanged?.(info);
|
|
262
|
+
}
|
|
263
|
+
: undefined
|
|
264
|
+
}
|
|
265
|
+
overrideItemLayout={
|
|
266
|
+
overrideItemLayout
|
|
267
|
+
? (layout, item, _, __, extraData) => {
|
|
268
|
+
overrideItemLayout?.(
|
|
269
|
+
layout,
|
|
270
|
+
item.originalItem,
|
|
271
|
+
item.originalIndex,
|
|
272
|
+
columnCount,
|
|
273
|
+
extraData
|
|
274
|
+
);
|
|
275
|
+
layout.span = undefined;
|
|
276
|
+
}
|
|
277
|
+
: undefined
|
|
278
|
+
}
|
|
279
|
+
/>
|
|
280
|
+
);
|
|
281
|
+
}}
|
|
282
|
+
overrideItemLayout={
|
|
283
|
+
getColumnFlex
|
|
284
|
+
? (layout, item, index, maxColumns, extraData) => {
|
|
285
|
+
layout.span =
|
|
286
|
+
(columnCount *
|
|
287
|
+
getColumnFlex(item, index, maxColumns, extraData)) /
|
|
288
|
+
totalColumnFlex;
|
|
289
|
+
}
|
|
290
|
+
: undefined
|
|
291
|
+
}
|
|
292
|
+
/>
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Splits data for each column's FlashList
|
|
299
|
+
*/
|
|
300
|
+
const useDataSet = <T,>(
|
|
301
|
+
columnCount: number,
|
|
302
|
+
optimizeItemArrangement: boolean,
|
|
303
|
+
sourceData?: FlashListProps<T>["data"],
|
|
304
|
+
overrideItemLayout?: MasonryFlashListProps<T>["overrideItemLayout"],
|
|
305
|
+
extraData?: MasonryFlashListProps<T>["extraData"]
|
|
306
|
+
): MasonryListItem<T>[][] => {
|
|
307
|
+
return useMemo(() => {
|
|
308
|
+
if (!sourceData || sourceData.length === 0) {
|
|
309
|
+
return [];
|
|
310
|
+
}
|
|
311
|
+
const columnHeightTracker = new Array<number>(columnCount).fill(0);
|
|
312
|
+
const layoutObject: { size: number | undefined } = { size: undefined };
|
|
313
|
+
const dataSet = new Array<MasonryListItem<T>[]>(columnCount);
|
|
314
|
+
const dataSize = sourceData.length;
|
|
315
|
+
|
|
316
|
+
for (let i = 0; i < columnCount; i++) {
|
|
317
|
+
dataSet[i] = [];
|
|
318
|
+
}
|
|
319
|
+
for (let i = 0; i < dataSize; i++) {
|
|
320
|
+
let nextColumnIndex = i % columnCount;
|
|
321
|
+
if (optimizeItemArrangement) {
|
|
322
|
+
for (let j = 0; j < columnCount; j++) {
|
|
323
|
+
if (columnHeightTracker[j] < columnHeightTracker[nextColumnIndex]) {
|
|
324
|
+
nextColumnIndex = j;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
// update height of column
|
|
328
|
+
layoutObject.size = undefined;
|
|
329
|
+
overrideItemLayout!(
|
|
330
|
+
layoutObject,
|
|
331
|
+
sourceData[i],
|
|
332
|
+
i,
|
|
333
|
+
columnCount,
|
|
334
|
+
extraData
|
|
335
|
+
);
|
|
336
|
+
columnHeightTracker[nextColumnIndex] +=
|
|
337
|
+
layoutObject.size ?? defaultEstimatedItemSize;
|
|
338
|
+
}
|
|
339
|
+
dataSet[nextColumnIndex].push({
|
|
340
|
+
originalItem: sourceData[i],
|
|
341
|
+
originalIndex: i,
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
return dataSet;
|
|
345
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
346
|
+
}, [sourceData, columnCount, optimizeItemArrangement, extraData]);
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const useTotalColumnFlex = <T,>(
|
|
350
|
+
dataSet: MasonryListItem<T>[][],
|
|
351
|
+
props: MasonryFlashListProps<T>
|
|
352
|
+
): number => {
|
|
353
|
+
return useMemo(() => {
|
|
354
|
+
const columnCount = props.numColumns || 1;
|
|
355
|
+
if (!props.getColumnFlex) {
|
|
356
|
+
return columnCount;
|
|
357
|
+
}
|
|
358
|
+
let totalFlexSum = 0;
|
|
359
|
+
const dataSize = dataSet.length;
|
|
360
|
+
for (let i = 0; i < dataSize; i++) {
|
|
361
|
+
totalFlexSum += props.getColumnFlex(
|
|
362
|
+
dataSet[i],
|
|
363
|
+
i,
|
|
364
|
+
columnCount,
|
|
365
|
+
props.extraData
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
return totalFlexSum;
|
|
369
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
370
|
+
}, [dataSet, props.getColumnFlex, props.extraData]);
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Handle both function refs and refs with current property
|
|
375
|
+
*/
|
|
376
|
+
const useRefWithForwardRef = <T,>(
|
|
377
|
+
forwardRef: any
|
|
378
|
+
): [React.MutableRefObject<T | null>, (instance: T | null) => void] => {
|
|
379
|
+
const ref: React.MutableRefObject<T | null> = useRef(null);
|
|
380
|
+
return [
|
|
381
|
+
ref,
|
|
382
|
+
useCallback(
|
|
383
|
+
(instance: T | null) => {
|
|
384
|
+
ref.current = instance;
|
|
385
|
+
if (typeof forwardRef === "function") {
|
|
386
|
+
forwardRef(instance);
|
|
387
|
+
} else if (forwardRef) {
|
|
388
|
+
forwardRef.current = instance;
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
[forwardRef]
|
|
392
|
+
),
|
|
393
|
+
];
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* This ScrollView is actually just a view mimicking a scrollview. We block the onScroll event from being passed to the parent list directly.
|
|
398
|
+
* We manually drive onScroll from the parent and thus, achieve recycling.
|
|
399
|
+
*/
|
|
400
|
+
const getFlashListScrollView = (
|
|
401
|
+
onScrollRef: React.RefObject<OnScrollCallback[]>,
|
|
402
|
+
getParentHeight: () => number
|
|
403
|
+
) => {
|
|
404
|
+
const FlashListScrollView = React.forwardRef(
|
|
405
|
+
(props: ScrollViewProps, ref: React.ForwardedRef<View>) => {
|
|
406
|
+
const { onLayout, onScroll, ...rest } = props;
|
|
407
|
+
const onLayoutProxy = useCallback(
|
|
408
|
+
(layoutEvent: LayoutChangeEvent) => {
|
|
409
|
+
onLayout?.({
|
|
410
|
+
nativeEvent: {
|
|
411
|
+
layout: {
|
|
412
|
+
height: getParentHeight(),
|
|
413
|
+
width: layoutEvent.nativeEvent.layout.width,
|
|
414
|
+
},
|
|
415
|
+
},
|
|
416
|
+
} as LayoutChangeEvent);
|
|
417
|
+
},
|
|
418
|
+
[onLayout]
|
|
419
|
+
);
|
|
420
|
+
useEffect(() => {
|
|
421
|
+
if (onScroll) {
|
|
422
|
+
onScrollRef.current?.push(onScroll);
|
|
423
|
+
}
|
|
424
|
+
return () => {
|
|
425
|
+
if (!onScrollRef.current || !onScroll) {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
const indexToDelete = onScrollRef.current.indexOf(onScroll);
|
|
429
|
+
if (indexToDelete > -1) {
|
|
430
|
+
onScrollRef.current.splice(indexToDelete, 1);
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
}, [onScroll]);
|
|
434
|
+
return <View ref={ref} {...rest} onLayout={onLayoutProxy} />;
|
|
435
|
+
}
|
|
436
|
+
);
|
|
437
|
+
FlashListScrollView.displayName = "FlashListScrollView";
|
|
438
|
+
return FlashListScrollView;
|
|
439
|
+
};
|
|
440
|
+
const updateViewTokens = (tokens: ViewToken[]) => {
|
|
441
|
+
const length = tokens.length;
|
|
442
|
+
for (let i = 0; i < length; i++) {
|
|
443
|
+
const token = tokens[i];
|
|
444
|
+
if (token.index !== null && token.index !== undefined) {
|
|
445
|
+
if (token.item) {
|
|
446
|
+
token.index = token.item.originalIndex;
|
|
447
|
+
token.item = token.item.originalItem;
|
|
448
|
+
} else {
|
|
449
|
+
token.index = null;
|
|
450
|
+
token.item = undefined;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const getEmptyScrollEvent = () => {
|
|
457
|
+
return {
|
|
458
|
+
nativeEvent: { contentOffset: { y: 0, x: 0 } },
|
|
459
|
+
};
|
|
460
|
+
};
|
|
461
|
+
const getListRenderedSize = <T,>(
|
|
462
|
+
parentFlashList: React.MutableRefObject<FlashList<T[]> | null>
|
|
463
|
+
) => {
|
|
464
|
+
return parentFlashList?.current?.recyclerlistview_unsafe?.getRenderedSize();
|
|
465
|
+
};
|
|
466
|
+
MasonryFlashListComponent.displayName = "MasonryFlashList";
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* FlashList variant that enables rendering of masonry layouts.
|
|
470
|
+
* If you want `MasonryFlashList` to optimize item arrangement, enable `optimizeItemArrangement` and pass a valid `overrideItemLayout` function.
|
|
471
|
+
*/
|
|
472
|
+
export const MasonryFlashList = MasonryFlashListComponent as <T>(
|
|
473
|
+
props: MasonryFlashListProps<T> & {
|
|
474
|
+
ref?: React.RefObject<MasonryFlashListRef<T> | null>;
|
|
475
|
+
}
|
|
476
|
+
) => React.ReactElement;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
export interface PureComponentWrapperProps {
|
|
4
|
+
renderer: (arg: unknown) => React.ReactElement | null;
|
|
5
|
+
|
|
6
|
+
/** Renderer is called with this argument.
|
|
7
|
+
* Don't change this value everytime or else component will always rerender. Prefer primitives. */
|
|
8
|
+
arg?: unknown;
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
[other: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Pure component wrapper that can be used to prevent renders of the `renderer` method passed to the component. Any change in props will lead to `renderer` getting called.
|
|
15
|
+
*/
|
|
16
|
+
export class PureComponentWrapper extends React.PureComponent<PureComponentWrapperProps> {
|
|
17
|
+
static defaultProps = {
|
|
18
|
+
enabled: true,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
private overrideEnabled: boolean | undefined = undefined;
|
|
22
|
+
|
|
23
|
+
/** Once set explicitly, prop will be ignored. Not using state because of performance reasons. */
|
|
24
|
+
public setEnabled(enabled: boolean) {
|
|
25
|
+
if (enabled !== this.overrideEnabled) {
|
|
26
|
+
this.overrideEnabled = enabled;
|
|
27
|
+
this.forceUpdate();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
render() {
|
|
32
|
+
if (this.overrideEnabled === undefined) {
|
|
33
|
+
return (
|
|
34
|
+
(this.props.enabled && this.props.renderer(this.props.arg)) || null
|
|
35
|
+
);
|
|
36
|
+
} else {
|
|
37
|
+
return (
|
|
38
|
+
(this.overrideEnabled && this.props.renderer(this.props.arg)) || null
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { AverageWindow } from "../utils/AverageWindow";
|
|
2
|
+
|
|
3
|
+
describe("AverageWindow", () => {
|
|
4
|
+
const fillAverageWindow = (
|
|
5
|
+
averageWindow: AverageWindow,
|
|
6
|
+
from: number,
|
|
7
|
+
to: number
|
|
8
|
+
) => {
|
|
9
|
+
for (let i = from; i < to; i++) {
|
|
10
|
+
averageWindow.addValue(i);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
it("all input window values should be filled with correct values", () => {
|
|
14
|
+
const commonValue = 50;
|
|
15
|
+
const size = 100;
|
|
16
|
+
const averageWindow = new AverageWindow(size);
|
|
17
|
+
const inputValues = averageWindow["inputValues"];
|
|
18
|
+
|
|
19
|
+
for (let i = 0; i < size; i++) {
|
|
20
|
+
averageWindow.addValue(commonValue);
|
|
21
|
+
}
|
|
22
|
+
expect(Math.round(averageWindow.currentValue)).toBe(commonValue);
|
|
23
|
+
expect(inputValues.every((val) => val === commonValue)).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
it("computes correct average", () => {
|
|
26
|
+
// We're gonna check if average from internal array matches the current average value
|
|
27
|
+
const size = 100;
|
|
28
|
+
const averageWindow = new AverageWindow(size);
|
|
29
|
+
const inputValues = averageWindow["inputValues"];
|
|
30
|
+
fillAverageWindow(averageWindow, 0, 200);
|
|
31
|
+
|
|
32
|
+
// calculate average directly
|
|
33
|
+
const reduceObj = inputValues.reduce(
|
|
34
|
+
(obj, val) => {
|
|
35
|
+
if (val !== undefined) {
|
|
36
|
+
obj.sum += val;
|
|
37
|
+
obj.count++;
|
|
38
|
+
}
|
|
39
|
+
return obj;
|
|
40
|
+
},
|
|
41
|
+
{ sum: 0, count: 0 }
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
// running average could be different by few decimal points so floor is required
|
|
45
|
+
expect(Math.floor(averageWindow.currentValue)).toBe(
|
|
46
|
+
Math.floor(reduceObj.sum / reduceObj.count)
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
it("should prioritize new values quickly", () => {
|
|
50
|
+
const size = 30;
|
|
51
|
+
const averageWindow = new AverageWindow(size);
|
|
52
|
+
fillAverageWindow(averageWindow, 0, 100);
|
|
53
|
+
expect(averageWindow.currentValue).toBeGreaterThan(70);
|
|
54
|
+
|
|
55
|
+
fillAverageWindow(averageWindow, 0, 30);
|
|
56
|
+
expect(averageWindow.currentValue).toBeLessThan(30);
|
|
57
|
+
});
|
|
58
|
+
it("reports initial average correctly", () => {
|
|
59
|
+
const size = 30;
|
|
60
|
+
const averageWindow = new AverageWindow(size, 25);
|
|
61
|
+
expect(averageWindow.currentValue).toBe(25);
|
|
62
|
+
});
|
|
63
|
+
it("updates initial average correctly", () => {
|
|
64
|
+
const size = 30;
|
|
65
|
+
const averageWindow = new AverageWindow(size, 10);
|
|
66
|
+
averageWindow.addValue(20);
|
|
67
|
+
expect(averageWindow.currentValue).toBe(15);
|
|
68
|
+
});
|
|
69
|
+
it("increments next index correctly", () => {
|
|
70
|
+
const size = 30;
|
|
71
|
+
const averageWindow = new AverageWindow(size);
|
|
72
|
+
for (let i = 0; i < 300; i++) {
|
|
73
|
+
expect(averageWindow["getNextIndex"]()).toBe(i % size);
|
|
74
|
+
}
|
|
75
|
+
const averageWindow2 = new AverageWindow(size, 1);
|
|
76
|
+
for (let i = 1; i < 300; i++) {
|
|
77
|
+
expect(averageWindow2["getNextIndex"]()).toBe(i % size);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
});
|