@infinite-table/infinite-react 0.4.12 → 0.5.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.
Files changed (282) hide show
  1. package/.vscode/settings.json +3 -0
  2. package/API.md +0 -0
  3. package/build/buildPackageJson.js +43 -0
  4. package/build/scripts/test.js +37 -0
  5. package/bundler.config.js +25 -0
  6. package/globals.d.ts +7 -0
  7. package/package.json +95 -3
  8. package/rollup.config.js +15 -0
  9. package/run-build-deepmap.js +47 -0
  10. package/run-build.js +45 -0
  11. package/run-dts-concat.js +21 -0
  12. package/src/components/CSSVariableWatch.tsx +82 -0
  13. package/src/components/DataSource/BooleanCollectionState.ts +214 -0
  14. package/src/components/DataSource/BooleanDeepCollectionState.ts +208 -0
  15. package/src/components/DataSource/DataSourceContext.ts +20 -0
  16. package/src/components/DataSource/GroupRowsState.ts +74 -0
  17. package/src/components/DataSource/Indexer.ts +27 -0
  18. package/src/components/DataSource/RowSelectionState.ts +763 -0
  19. package/src/components/DataSource/defaultFilterTypes.ts +64 -0
  20. package/src/components/DataSource/index.tsx +108 -0
  21. package/src/components/DataSource/privateHooks/getChangeDetect.ts +7 -0
  22. package/src/components/DataSource/privateHooks/useLoadData.ts +783 -0
  23. package/src/components/DataSource/publicHooks/useDataSource.ts +19 -0
  24. package/src/components/DataSource/publicHooks/useDataSourceActions.ts +14 -0
  25. package/src/components/DataSource/state/getInitialState.ts +494 -0
  26. package/src/components/DataSource/state/normalizeSortInfo.ts +35 -0
  27. package/src/components/DataSource/state/reducer.ts +466 -0
  28. package/src/components/DataSource/state/rowInfoStatus.ts +27 -0
  29. package/src/components/DataSource/types.ts +548 -0
  30. package/src/components/HeadlessTable/HeadlessTableWithPinnedContainers.tsx +142 -0
  31. package/src/components/HeadlessTable/MappedCells.ts +251 -0
  32. package/src/components/HeadlessTable/RawTable.tsx +98 -0
  33. package/src/components/HeadlessTable/ReactHeadlessTableRenderer.tsx +1516 -0
  34. package/src/components/HeadlessTable/index.tsx +234 -0
  35. package/src/components/InfiniteTable/InfiniteCls.css.ts +164 -0
  36. package/src/components/InfiniteTable/InfiniteTableContext.ts +16 -0
  37. package/src/components/InfiniteTable/api/getImperativeApi.ts +391 -0
  38. package/src/components/InfiniteTable/api/getSelectionApi.ts +364 -0
  39. package/src/components/InfiniteTable/api/type.ts +11 -0
  40. package/src/components/InfiniteTable/components/ActiveCellIndicator.css.ts +51 -0
  41. package/src/components/InfiniteTable/components/ActiveCellIndicator.tsx +116 -0
  42. package/src/components/InfiniteTable/components/ActiveRowIndicator.css.ts +56 -0
  43. package/src/components/InfiniteTable/components/ActiveRowIndicator.tsx +94 -0
  44. package/src/components/InfiniteTable/components/CheckBox.css.ts +13 -0
  45. package/src/components/InfiniteTable/components/CheckBox.tsx +96 -0
  46. package/src/components/InfiniteTable/components/FilterEditors.tsx +52 -0
  47. package/src/components/InfiniteTable/components/InfiniteTableBody/InfiniteTableBody.tsx +27 -0
  48. package/src/components/InfiniteTable/components/InfiniteTableBody/index.tsx +1 -0
  49. package/src/components/InfiniteTable/components/InfiniteTableFooter/InfiniteTableFooter.tsx +31 -0
  50. package/src/components/InfiniteTable/components/InfiniteTableFooter/index.tsx +1 -0
  51. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableColumnHeaderFilter.tsx +102 -0
  52. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeader.tsx +163 -0
  53. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderCell.tsx +574 -0
  54. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderGroup.tsx +79 -0
  55. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderTypes.tsx +25 -0
  56. package/src/components/InfiniteTable/components/InfiniteTableHeader/InfiniteTableHeaderWrapper.tsx +185 -0
  57. package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/GroupResizeHandle.tsx +159 -0
  58. package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/ResizeHandle.css.ts +124 -0
  59. package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/columnResizer.ts +166 -0
  60. package/src/components/InfiniteTable/components/InfiniteTableHeader/ResizeHandle/index.tsx +146 -0
  61. package/src/components/InfiniteTable/components/InfiniteTableHeader/buildColumnAndGroupTree.ts +240 -0
  62. package/src/components/InfiniteTable/components/InfiniteTableHeader/header.css.ts +358 -0
  63. package/src/components/InfiniteTable/components/InfiniteTableHeader/index.tsx +1 -0
  64. package/src/components/InfiniteTable/components/InfiniteTableHeader/useColumnGroupResizeHandle.tsx +114 -0
  65. package/src/components/InfiniteTable/components/InfiniteTableHeader/useColumnResizeHandle.tsx +146 -0
  66. package/src/components/InfiniteTable/components/InfiniteTableLicenseFooter/footer.css.ts +7 -0
  67. package/src/components/InfiniteTable/components/InfiniteTableLicenseFooter/index.tsx +110 -0
  68. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableCell.tsx +120 -0
  69. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableCellTypes.ts +74 -0
  70. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableColumnCell.tsx +457 -0
  71. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRow.tsx +107 -0
  72. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRowClassName.ts +6 -0
  73. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRowTypes.ts +32 -0
  74. package/src/components/InfiniteTable/components/InfiniteTableRow/InfiniteTableRowUnvirtualized.tsx +75 -0
  75. package/src/components/InfiniteTable/components/InfiniteTableRow/columnRendering.tsx +263 -0
  76. package/src/components/InfiniteTable/components/InfiniteTableRow/index.tsx +1 -0
  77. package/src/components/InfiniteTable/components/InfiniteTableRow/row.css.ts +74 -0
  78. package/src/components/InfiniteTable/components/InfiniteTableRow/useRowDOMProps.ts +199 -0
  79. package/src/components/InfiniteTable/components/LoadMask.css.ts +34 -0
  80. package/src/components/InfiniteTable/components/LoadMask.tsx +27 -0
  81. package/src/components/InfiniteTable/components/ScrollbarPlaceholder.tsx +56 -0
  82. package/src/components/InfiniteTable/components/cell.css.ts +192 -0
  83. package/src/components/InfiniteTable/components/icons/ArrowDown.tsx +14 -0
  84. package/src/components/InfiniteTable/components/icons/ArrowUp.tsx +14 -0
  85. package/src/components/InfiniteTable/components/icons/ExpanderIcon.css.ts +31 -0
  86. package/src/components/InfiniteTable/components/icons/ExpanderIcon.tsx +63 -0
  87. package/src/components/InfiniteTable/components/icons/Icon.tsx +18 -0
  88. package/src/components/InfiniteTable/components/icons/InfiniteTableIconClassName.ts +1 -0
  89. package/src/components/InfiniteTable/components/icons/LoadingIcon.css.ts +8 -0
  90. package/src/components/InfiniteTable/components/icons/LoadingIcon.tsx +45 -0
  91. package/src/components/InfiniteTable/components/icons/MenuIcon.tsx +50 -0
  92. package/src/components/InfiniteTable/components/icons/SortIcon.tsx +113 -0
  93. package/src/components/InfiniteTable/eventHandlers/eventHandlerTypes.ts +57 -0
  94. package/src/components/InfiniteTable/eventHandlers/index.ts +95 -0
  95. package/src/components/InfiniteTable/eventHandlers/keyboardNavigation.ts +232 -0
  96. package/src/components/InfiniteTable/eventHandlers/keyboardSelection.ts +110 -0
  97. package/src/components/InfiniteTable/eventHandlers/onCellClick.ts +94 -0
  98. package/src/components/InfiniteTable/eventHandlers/onKeyDown.ts +25 -0
  99. package/src/components/InfiniteTable/hooks/adjustColumnOrderForAllColumns.ts +70 -0
  100. package/src/components/InfiniteTable/hooks/reorderColumnsOnDrag.ts +476 -0
  101. package/src/components/InfiniteTable/hooks/useAutoSizeColumns.ts +206 -0
  102. package/src/components/InfiniteTable/hooks/useCellClassName.ts +80 -0
  103. package/src/components/InfiniteTable/hooks/useCellRendering.tsx +217 -0
  104. package/src/components/InfiniteTable/hooks/useColumnGroups.ts +71 -0
  105. package/src/components/InfiniteTable/hooks/useColumnMenu.ts +72 -0
  106. package/src/components/InfiniteTable/hooks/useColumnPointerEvents.ts +258 -0
  107. package/src/components/InfiniteTable/hooks/useColumnRowspan.ts +43 -0
  108. package/src/components/InfiniteTable/hooks/useColumnSizeFn.ts +24 -0
  109. package/src/components/InfiniteTable/hooks/useColumnsWhen.ts +582 -0
  110. package/src/components/InfiniteTable/hooks/useComputed.ts +186 -0
  111. package/src/components/InfiniteTable/hooks/useComputedVisibleColumns.ts +219 -0
  112. package/src/components/InfiniteTable/hooks/useDOMProps.ts +306 -0
  113. package/src/components/InfiniteTable/hooks/useEffectWithRaf.ts +47 -0
  114. package/src/components/InfiniteTable/hooks/useInfiniteTable.ts +10 -0
  115. package/src/components/InfiniteTable/hooks/useInfiniteTableState.ts +11 -0
  116. package/src/components/InfiniteTable/hooks/useInternalProps.ts +8 -0
  117. package/src/components/InfiniteTable/hooks/useLicense/LicenseDetails.ts +13 -0
  118. package/src/components/InfiniteTable/hooks/useLicense/crc32.ts +56 -0
  119. package/src/components/InfiniteTable/hooks/useLicense/decode.ts +147 -0
  120. package/src/components/InfiniteTable/hooks/useLicense/useLicense.ts +39 -0
  121. package/src/components/InfiniteTable/hooks/useRerenderOnKeyChange.ts +21 -0
  122. package/src/components/InfiniteTable/hooks/useRowHover.ts +18 -0
  123. package/src/components/InfiniteTable/hooks/useScrollToActiveCell.ts +48 -0
  124. package/src/components/InfiniteTable/hooks/useScrollToActiveRow.ts +44 -0
  125. package/src/components/InfiniteTable/hooks/useScrollbars.ts +51 -0
  126. package/src/components/InfiniteTable/hooks/useToggleGroupRow.ts +56 -0
  127. package/src/components/InfiniteTable/hooks/useUnpinnedRendering.tsx +165 -0
  128. package/src/components/InfiniteTable/hooks/useYourBrain.ts +55 -0
  129. package/src/components/InfiniteTable/index.tsx +352 -0
  130. package/src/components/InfiniteTable/internalProps.ts +4 -0
  131. package/src/components/InfiniteTable/state/computeColumnGroupsDepths.ts +29 -0
  132. package/src/components/InfiniteTable/state/getColumnVisibilityForHideEmptyGroupColumns.ts +127 -0
  133. package/src/components/InfiniteTable/state/getInitialState.ts +375 -0
  134. package/src/components/InfiniteTable/theme.css.ts +556 -0
  135. package/src/components/InfiniteTable/types/InfiniteTableAction.ts +6 -0
  136. package/src/components/InfiniteTable/types/InfiniteTableActionType.ts +12 -0
  137. package/src/components/InfiniteTable/types/InfiniteTableColumn.ts +478 -0
  138. package/src/components/InfiniteTable/types/InfiniteTableComputedValues.ts +43 -0
  139. package/src/components/InfiniteTable/types/InfiniteTableContextValue.ts +12 -0
  140. package/src/components/InfiniteTable/types/InfiniteTableInternalProps.ts +3 -0
  141. package/src/components/InfiniteTable/types/InfiniteTableProps.ts +511 -0
  142. package/src/components/InfiniteTable/types/InfiniteTableState.ts +195 -0
  143. package/src/components/InfiniteTable/types/Utility.ts +35 -0
  144. package/src/components/InfiniteTable/types/index.ts +96 -0
  145. package/src/components/InfiniteTable/utilities.css.ts +179 -0
  146. package/src/components/InfiniteTable/utils/MultiRowSelector.ts +111 -0
  147. package/src/components/InfiniteTable/utils/RenderHookComponentForInfinite.tsx +52 -0
  148. package/src/components/InfiniteTable/utils/adjustColumnOrderForPinning.ts +33 -0
  149. package/src/components/InfiniteTable/utils/assignFiltered.ts +29 -0
  150. package/src/components/InfiniteTable/utils/defaultGetColumContextMenuItems.tsx +108 -0
  151. package/src/components/InfiniteTable/utils/getColumnComputedType.ts +33 -0
  152. package/src/components/InfiniteTable/utils/getColumnForGroupBy.tsx +313 -0
  153. package/src/components/InfiniteTable/utils/getComputedVisibleColumns.ts +773 -0
  154. package/src/components/InfiniteTable/utils/getContextMenuForColumn.tsx +72 -0
  155. package/src/components/InfiniteTable/utils/infiniteDOMUtils.ts +189 -0
  156. package/src/components/InfiniteTable/utils/mapToObject.ts +20 -0
  157. package/src/components/InfiniteTable/utils/moveXatY.ts +25 -0
  158. package/src/components/InfiniteTable/utils/objectValuesExcept.ts +9 -0
  159. package/src/components/InfiniteTable/utils/progressiveSpeedScroller.ts +158 -0
  160. package/src/components/InfiniteTable/utils/rafFn.ts +13 -0
  161. package/src/components/InfiniteTable/utils/toCSSVarName.ts +1 -0
  162. package/src/components/InfiniteTable/utils/toMap.ts +24 -0
  163. package/src/components/Menu/Menu.tsx +750 -0
  164. package/src/components/Menu/MenuCls.css.ts +139 -0
  165. package/src/components/Menu/MenuContext.ts +10 -0
  166. package/src/components/Menu/MenuContextValue.ts +15 -0
  167. package/src/components/Menu/MenuProps.ts +98 -0
  168. package/src/components/Menu/MenuState.ts +56 -0
  169. package/src/components/Menu/MenuTriangleContext.ts +352 -0
  170. package/src/components/Menu/childrenToRuntimeItems.tsx +103 -0
  171. package/src/components/Menu/getMenuState.tsx +119 -0
  172. package/src/components/Menu/index.tsx +88 -0
  173. package/src/components/Menu/propToIdentifyMenu.ts +1 -0
  174. package/src/components/RawList/AvoidReactDiff.tsx +48 -0
  175. package/src/components/RawList/MappedItems.ts +143 -0
  176. package/src/components/RawList/ReactVirtualRenderer.tsx +307 -0
  177. package/src/components/RawList/index.tsx +42 -0
  178. package/src/components/RawList/types.ts +23 -0
  179. package/src/components/RawList/useVirtualRenderer.ts +36 -0
  180. package/src/components/RenderHookComponent.tsx +10 -0
  181. package/src/components/ResizeObserver/index.tsx +170 -0
  182. package/src/components/VirtualBrain/MatrixBrain.ts +1621 -0
  183. package/src/components/VirtualBrain/ScrollListener.ts +39 -0
  184. package/src/components/VirtualBrain/index.ts +664 -0
  185. package/src/components/VirtualList/ColumnListWithExternalScrolling.tsx +78 -0
  186. package/src/components/VirtualList/InfiniteListRootClassName.ts +1 -0
  187. package/src/components/VirtualList/RowListWithExternalScrolling.tsx +120 -0
  188. package/src/components/VirtualList/SpacePlaceholder.tsx +32 -0
  189. package/src/components/VirtualList/VirtualList.css.ts +29 -0
  190. package/src/components/VirtualList/VirtualList.tsx +141 -0
  191. package/src/components/VirtualList/VirtualRowList.tsx +34 -0
  192. package/src/components/VirtualList/types.ts +56 -0
  193. package/src/components/VirtualList/useCorrectHeightForRowElements.ts +14 -0
  194. package/src/components/VirtualScrollContainer/VirtualScrollContainer.css.ts +51 -0
  195. package/src/components/VirtualScrollContainer/getScrollableClassName.ts +30 -0
  196. package/src/components/VirtualScrollContainer/index.tsx +63 -0
  197. package/src/components/flexbox/index.ts +562 -0
  198. package/src/components/hooks/useComponentState/index.tsx +562 -0
  199. package/src/components/hooks/useComponentState/types.ts +32 -0
  200. package/src/components/hooks/useEffectOnceWithProperUnmount.ts +33 -0
  201. package/src/components/hooks/useEffectWithChanges.ts +56 -0
  202. package/src/components/hooks/useImmutableRef.ts +7 -0
  203. package/src/components/hooks/useInterceptedMap.ts +71 -0
  204. package/src/components/hooks/useLatest.tsx +8 -0
  205. package/src/components/hooks/useLazyLatest.ts +23 -0
  206. package/src/components/hooks/useMemoShallowObjectMerge.ts +16 -0
  207. package/src/components/hooks/useMounted.ts +18 -0
  208. package/src/components/hooks/useOnMount.ts +21 -0
  209. package/src/components/hooks/useOnScroll.ts +34 -0
  210. package/src/components/hooks/useOnce.ts +10 -0
  211. package/src/components/hooks/useOverlay/index.tsx +418 -0
  212. package/src/components/hooks/usePrevious.ts +8 -0
  213. package/src/components/hooks/usePropertyOld.ts +168 -0
  214. package/src/components/hooks/useRerender.ts +12 -0
  215. package/src/components/hooks/xuseProperty.ts +165 -0
  216. package/src/components/types/CellPosition.ts +4 -0
  217. package/src/components/types/NonUndefined.ts +1 -0
  218. package/src/components/types/RemoveObject.ts +22 -0
  219. package/src/components/types/Renderable.ts +3 -0
  220. package/src/components/types/ScrollPosition.ts +7 -0
  221. package/src/components/types/Setter.ts +2 -0
  222. package/src/components/types/Size.ts +6 -0
  223. package/src/components/types/SubscriptionCallback.ts +10 -0
  224. package/src/components/types/VoidFn.ts +1 -0
  225. package/src/components/utils/buildSubscriptionCallback.tsx +60 -0
  226. package/src/components/utils/clamp.ts +3 -0
  227. package/src/components/utils/debounce.ts +16 -0
  228. package/src/components/utils/decamelize.ts +11 -0
  229. package/src/components/utils/discardCallsWithEqualArg.ts +42 -0
  230. package/src/components/utils/getRenderIndexes.ts +33 -0
  231. package/src/components/utils/getScrollbarWidth.ts +31 -0
  232. package/src/components/utils/getVirtualRenderCount.ts +21 -0
  233. package/src/components/utils/isControlled.ts +12 -0
  234. package/src/components/utils/isControlledValue.ts +5 -0
  235. package/src/index.tsx +34 -0
  236. package/src/test.tsx +18 -0
  237. package/src/utils/DeepMap/index.ts +588 -0
  238. package/src/utils/DeepMap/once.ts +16 -0
  239. package/src/utils/DeepMap/sortAscending.ts +1 -0
  240. package/src/utils/DeepMap/tsconfig.deepmap.json +17 -0
  241. package/src/utils/DeepMap/xpackage.json +26 -0
  242. package/src/utils/debug.ts +40 -0
  243. package/src/utils/deepClone.ts +38 -0
  244. package/src/utils/getGlobal.ts +3 -0
  245. package/src/utils/groupAndPivot/defaultToKey.ts +3 -0
  246. package/src/utils/groupAndPivot/getGroupKeysForDataItem.ts +19 -0
  247. package/src/utils/groupAndPivot/getPivotColumnsAndColumnGroups.ts +361 -0
  248. package/src/utils/groupAndPivot/index.ts +1229 -0
  249. package/src/utils/groupAndPivot/types.ts +10 -0
  250. package/src/utils/join.ts +4 -0
  251. package/src/utils/mathIntersection.ts +21 -0
  252. package/src/utils/minified.d.ts +2 -0
  253. package/src/utils/minified.js +6 -0
  254. package/src/utils/multisort/index.ts +151 -0
  255. package/src/utils/multisort/sortTypes.ts +25 -0
  256. package/src/utils/notes-status.md +92 -0
  257. package/src/utils/pageGeometry/ConvexPoly.ts +34 -0
  258. package/src/utils/pageGeometry/Point.ts +51 -0
  259. package/src/utils/pageGeometry/PolyWithPoints.ts +52 -0
  260. package/src/utils/pageGeometry/Rectangle.ts +98 -0
  261. package/src/utils/pageGeometry/Triangle.ts +16 -0
  262. package/src/utils/pageGeometry/alignment/index.ts +221 -0
  263. package/src/utils/pageGeometry/polyContainsPoint.ts +182 -0
  264. package/src/utils/pageGeometry/types.ts +3 -0
  265. package/src/utils/proxyFnCall.ts +46 -0
  266. package/src/utils/raf.ts +13 -0
  267. package/src/utils/selectParent.ts +18 -0
  268. package/src/utils/shallowEqualObjects.ts +49 -0
  269. package/src/utils/stripVar.ts +3 -0
  270. package/src/utils/toUpperFirst.ts +5 -0
  271. package/todo.md +2 -0
  272. package/tsconfig.build.json +8 -0
  273. package/tsconfig.dev.json +7 -0
  274. package/tsconfig.json +30 -0
  275. package/tsconfig.typebundler.json +13 -0
  276. package/typedoc.json +4 -0
  277. package/README.md +0 -104
  278. package/index.css +0 -857
  279. package/index.d.ts +0 -2111
  280. package/index.esm.css +0 -857
  281. package/index.esm.js +0 -13689
  282. package/index.js +0 -13560
@@ -0,0 +1,39 @@
1
+ import { OnScrollFn, ScrollPosition } from '../types/ScrollPosition';
2
+
3
+ const initialScrollPosition = {
4
+ scrollLeft: 0,
5
+ scrollTop: 0,
6
+ };
7
+ export class ScrollListener {
8
+ private scrollPosition: ScrollPosition = initialScrollPosition;
9
+ private onScrollFns: OnScrollFn[] = [];
10
+
11
+ public getScrollPosition = () => {
12
+ return this.scrollPosition;
13
+ };
14
+
15
+ public onScroll = (fn: OnScrollFn) => {
16
+ this.onScrollFns.push(fn);
17
+ return () => {
18
+ this.onScrollFns = this.onScrollFns.filter((f) => f !== fn);
19
+ };
20
+ };
21
+ public setScrollPosition = (scrollPosition: ScrollPosition) => {
22
+ this.scrollPosition = scrollPosition;
23
+
24
+ this.notifyScrollChange();
25
+ };
26
+
27
+ private notifyScrollChange() {
28
+ const { scrollPosition } = this;
29
+
30
+ const fns = this.onScrollFns;
31
+ for (let i = 0, len = fns.length; i < len; i++) {
32
+ fns[i](scrollPosition);
33
+ }
34
+ }
35
+
36
+ destroy = () => {
37
+ this.onScrollFns = [];
38
+ };
39
+ }
@@ -0,0 +1,664 @@
1
+ import binarySearch from 'binary-search';
2
+ import { Logger } from '../../utils/debug';
3
+ import type { Size } from '../types/Size';
4
+
5
+ import type { OnScrollFn, ScrollPosition } from '../types/ScrollPosition';
6
+
7
+ type MainAxisOptions = 'vertical' | 'horizontal';
8
+
9
+ const initialScrollPosition = {
10
+ scrollLeft: 0,
11
+ scrollTop: 0,
12
+ };
13
+
14
+ const raf =
15
+ typeof window !== 'undefined'
16
+ ? requestAnimationFrame
17
+ : (fn: () => void) => setTimeout(fn, 0);
18
+
19
+ export type VirtualBrainOptions = {
20
+ mainAxis: MainAxisOptions;
21
+ itemSize: MainAxisSize;
22
+
23
+ itemSpan?: ({ itemIndex }: { itemIndex: number }) => number;
24
+
25
+ count: number;
26
+ name?: string;
27
+ };
28
+
29
+ const ItemIndexBag = Object.seal({ itemIndex: 0 });
30
+
31
+ export type MainAxisSize = number | ((itemIndex: number) => number);
32
+ type BrainCache = number[];
33
+ type OnRenderCountChangeFn = (renderCount: number) => void;
34
+ type OnSizeChangeFn = (size: number) => void;
35
+ type OnDestroyFn = () => void;
36
+
37
+ export type GetItemPositionFn = (itemIndex: number) => number;
38
+
39
+ export type RenderRange = {
40
+ renderStartIndex: number;
41
+ renderEndIndex: number;
42
+ };
43
+ export type OnRenderRangeChangeFn = (renderRange: RenderRange) => void;
44
+
45
+ const SORT_ASC = (a: number, b: number) => a - b;
46
+
47
+ export class VirtualBrain extends Logger {
48
+ private scrollPosition: ScrollPosition = initialScrollPosition;
49
+ private scrollPositionOnMainAxis: number = 0;
50
+
51
+ private itemSpanParent!: BrainCache;
52
+ private itemSpanValue!: BrainCache;
53
+ private itemSizeCache!: BrainCache;
54
+ private itemOffsetCache!: BrainCache;
55
+ private options!: VirtualBrainOptions;
56
+
57
+ private destroyed: boolean = false;
58
+
59
+ private totalSize: number = 0;
60
+
61
+ private onScrollFns: OnScrollFn[] = [];
62
+ private onTotalSizeChangeFns: OnSizeChangeFn[] = [];
63
+ private onRenderCountChangeFns: OnRenderCountChangeFn[] = [];
64
+ private onRenderRangeChangeFns: OnRenderRangeChangeFn[] = [];
65
+ private onDestroyFns: OnDestroyFn[] = [];
66
+
67
+ name: string | undefined;
68
+
69
+ private renderCount: number = 0;
70
+ private renderRange: RenderRange = {
71
+ renderStartIndex: -1,
72
+ renderEndIndex: -1,
73
+ };
74
+ private availableSize: Size = { width: 0, height: 0 };
75
+
76
+ constructor(options: VirtualBrainOptions) {
77
+ super(`VirtualBrain:${options.mainAxis}`);
78
+
79
+ this.options = options;
80
+
81
+ this.reset();
82
+
83
+ this.renderCount = 0;
84
+
85
+ this.debug(`Init ${options.mainAxis}`);
86
+ }
87
+
88
+ public setAvailableSize = (size: Size) => {
89
+ this.availableSize = size;
90
+ if (__DEV__) {
91
+ this.debug(
92
+ 'New available size %d',
93
+ size[this.options.mainAxis === 'vertical' ? 'height' : 'width'],
94
+ );
95
+ }
96
+
97
+ this.updateRenderCount();
98
+ };
99
+
100
+ getOptions() {
101
+ return this.options;
102
+ }
103
+
104
+ private updateRenderCount(renderCount?: number) {
105
+ const { itemSize, mainAxis, count } = this.options;
106
+ const size =
107
+ this.availableSize[mainAxis === 'vertical' ? 'height' : 'width'];
108
+
109
+ if (!size) {
110
+ this.setRenderCount(0);
111
+ return;
112
+ }
113
+
114
+ this.setRenderCount(
115
+ renderCount ?? this.computeRenderCount({ itemSize, count, size }),
116
+ );
117
+ }
118
+
119
+ private computeRenderCount = ({
120
+ itemSize,
121
+ count,
122
+ size,
123
+ }: {
124
+ itemSize: MainAxisSize;
125
+ count: number;
126
+ size: number;
127
+ }) => {
128
+ let renderCount = 0;
129
+ if (typeof itemSize === 'function') {
130
+ const sizes = [];
131
+ for (let i = 0; i < count; i++) {
132
+ sizes.push(this.getItemSize(i));
133
+ }
134
+ sizes.sort(SORT_ASC);
135
+ let sum = 0;
136
+ for (let i = 0; i < count; i++) {
137
+ sum += sizes[i];
138
+
139
+ renderCount++;
140
+ if (sum > size) {
141
+ break;
142
+ }
143
+ }
144
+ renderCount += 1;
145
+ } else {
146
+ renderCount = Math.ceil(size / itemSize) + 1;
147
+ }
148
+
149
+ renderCount = Math.min(count, renderCount);
150
+
151
+ return renderCount;
152
+ };
153
+
154
+ private setRenderCount = (renderCount: number) => {
155
+ if (renderCount === this.renderCount) {
156
+ this.notifyTotalSizeChange();
157
+ return;
158
+ }
159
+
160
+ if (__DEV__) {
161
+ this.debug('set renderCount to %d', renderCount);
162
+ }
163
+ this.renderCount = renderCount;
164
+
165
+ this.updateRenderRange();
166
+
167
+ this.notifyRenderCountChange();
168
+ this.notifyTotalSizeChange();
169
+ };
170
+
171
+ private updateRenderRange() {
172
+ this.setRenderRange(this.computeRenderRange());
173
+ }
174
+
175
+ public getRenderStartIndex() {
176
+ return this.renderRange.renderStartIndex;
177
+ }
178
+
179
+ public getRenderCount = () => {
180
+ return this.renderCount ?? 0;
181
+ };
182
+
183
+ public setScrollPosition = (scrollPosition: ScrollPosition) => {
184
+ this.scrollPosition = scrollPosition;
185
+
186
+ const oldScrollPosOnMainAxis = this.scrollPositionOnMainAxis;
187
+
188
+ this.scrollPositionOnMainAxis =
189
+ this.scrollPosition[
190
+ this.options.mainAxis === 'vertical' ? 'scrollTop' : 'scrollLeft'
191
+ ];
192
+
193
+ if (this.scrollPositionOnMainAxis !== oldScrollPosOnMainAxis) {
194
+ this.updateRenderRange();
195
+ }
196
+ this.notifyScrollChange();
197
+ };
198
+
199
+ private notifyScrollChange() {
200
+ const { scrollPosition } = this;
201
+
202
+ const fns = this.onScrollFns;
203
+ for (let i = 0, len = fns.length; i < len; i++) {
204
+ fns[i](scrollPosition);
205
+ }
206
+ }
207
+
208
+ private notifyDestroy() {
209
+ const fns = this.onDestroyFns;
210
+ for (let i = 0, len = fns.length; i < len; i++) {
211
+ fns[i]();
212
+ }
213
+ }
214
+
215
+ private notifyTotalSizeChange() {
216
+ const totalSize = this.getTotalSize();
217
+
218
+ const fns = this.onTotalSizeChangeFns;
219
+ for (let i = 0, len = fns.length; i < len; i++) {
220
+ raf(() => {
221
+ fns[i](totalSize);
222
+ });
223
+ }
224
+ }
225
+
226
+ private notifyRenderCountChange() {
227
+ const { renderCount } = this;
228
+
229
+ const fns = this.onRenderCountChangeFns;
230
+ for (let i = 0, len = fns.length; i < len; i++) {
231
+ raf(() => {
232
+ fns[i](renderCount);
233
+ });
234
+ }
235
+ }
236
+
237
+ private notifyRenderRangeChange() {
238
+ const { renderRange } = this;
239
+
240
+ const fns = this.onRenderRangeChangeFns;
241
+
242
+ for (let i = 0, len = fns.length; i < len; i++) {
243
+ raf(() => {
244
+ fns[i](renderRange);
245
+ });
246
+ }
247
+ }
248
+
249
+ public getRenderRange = (): RenderRange => {
250
+ return this.renderRange;
251
+ };
252
+
253
+ private setRenderRange(renderRange: RenderRange) {
254
+ const { renderStartIndex, renderEndIndex } = this.renderRange;
255
+
256
+ if (
257
+ renderEndIndex !== renderRange.renderEndIndex ||
258
+ renderStartIndex !== renderRange.renderStartIndex
259
+ ) {
260
+ this.renderRange = renderRange;
261
+ if (__DEV__) {
262
+ this.debug(
263
+ 'Set renderRange to %d-%d',
264
+ renderRange.renderStartIndex,
265
+ renderRange.renderEndIndex,
266
+ );
267
+ }
268
+
269
+ this.notifyRenderRangeChange();
270
+ }
271
+ }
272
+
273
+ private computeRenderRange(): RenderRange {
274
+ const count = this.getOptions().count;
275
+ let renderCount = this.getRenderCount();
276
+
277
+ if (renderCount === 0) {
278
+ return {
279
+ renderStartIndex: -1,
280
+ renderEndIndex: -1,
281
+ };
282
+ }
283
+
284
+ let renderStartIndex = this.getItemAt(this.scrollPositionOnMainAxis);
285
+ let renderEndIndex = renderStartIndex + this.renderCount - 1;
286
+
287
+ if (renderEndIndex >= count) {
288
+ renderEndIndex = count - 1;
289
+ renderStartIndex = renderEndIndex - renderCount + 1;
290
+ }
291
+
292
+ //TODO we can avoid doing this expand here
293
+ // if the renderStartIndex row is spanned over, we only need to render the span parent instead
294
+ // of the current row - so big wins
295
+ if (this.options.itemSpan) {
296
+ this.computeItemSpanUpTo(renderStartIndex);
297
+
298
+ while (this.itemSpanParent[renderStartIndex] != renderStartIndex) {
299
+ renderStartIndex--;
300
+ }
301
+ }
302
+
303
+ return {
304
+ renderStartIndex,
305
+ renderEndIndex,
306
+ };
307
+ }
308
+
309
+ public getScrollPosition = () => {
310
+ return this.scrollPosition;
311
+ };
312
+
313
+ public onScroll = (fn: OnScrollFn) => {
314
+ this.onScrollFns.push(fn);
315
+ return () => {
316
+ this.onScrollFns = this.onScrollFns.filter((f) => f !== fn);
317
+ };
318
+ };
319
+
320
+ public onTotalSizeChange = (fn: OnSizeChangeFn) => {
321
+ this.onTotalSizeChangeFns.push(fn);
322
+ return () => {
323
+ this.onTotalSizeChangeFns = this.onTotalSizeChangeFns.filter(
324
+ (f) => f !== fn,
325
+ );
326
+ };
327
+ };
328
+ public onRenderCountChange = (fn: OnRenderCountChangeFn) => {
329
+ this.onRenderCountChangeFns.push(fn);
330
+ return () => {
331
+ this.onRenderCountChangeFns = this.onRenderCountChangeFns.filter(
332
+ (f) => f !== fn,
333
+ );
334
+ };
335
+ };
336
+ public onRenderRangeChange = (fn: OnRenderRangeChangeFn) => {
337
+ this.onRenderRangeChangeFns.push(fn);
338
+
339
+ return () => {
340
+ this.onRenderRangeChangeFns = this.onRenderRangeChangeFns.filter(
341
+ (f) => f !== fn,
342
+ );
343
+ };
344
+ };
345
+
346
+ public onDestroy = (fn: OnDestroyFn) => {
347
+ this.onDestroyFns.push(fn);
348
+
349
+ return () => {
350
+ this.onDestroyFns = this.onDestroyFns.filter((f) => f !== fn);
351
+ };
352
+ };
353
+
354
+ public update = (
355
+ newCount: VirtualBrainOptions['count'],
356
+ newItemSize: VirtualBrainOptions['itemSize'],
357
+ newItemSpan?: VirtualBrainOptions['itemSpan'],
358
+ ) => {
359
+ const { itemSize, count, itemSpan } = this.options;
360
+
361
+ const countChange = count !== newCount;
362
+ const itemSizeChange = itemSize !== newItemSize;
363
+ const itemSpanChange = itemSpan !== newItemSpan;
364
+
365
+ if (!countChange && !itemSizeChange && !itemSpanChange) {
366
+ // this.debug(
367
+ // `Range (count=${count}, itemSize=${
368
+ // typeof itemSize === 'number' ? itemSize : 'fn'
369
+ // }) has not changed, so skip update`,
370
+ // );
371
+
372
+ return;
373
+ }
374
+
375
+ // this.debug(
376
+ // `Range changed (count=${newCount}, itemSize=${
377
+ // typeof newItemSize === 'number' ? newItemSize : 'fn'
378
+ // }), so update`,
379
+ // );
380
+
381
+ this.options.count = newCount;
382
+ this.options.itemSize = newItemSize;
383
+ this.options.itemSpan = newItemSpan;
384
+
385
+ const size =
386
+ this.availableSize[
387
+ this.options.mainAxis === 'vertical' ? 'height' : 'width'
388
+ ];
389
+
390
+ const newRenderCount = this.computeRenderCount({
391
+ itemSize: newItemSize,
392
+ count: newCount,
393
+ size,
394
+ });
395
+
396
+ //TODO this can be optimized even further
397
+ // since when just count changes, even if it's smaller, the cache should be kept
398
+ if (!itemSizeChange && !itemSpanChange && countChange && newCount > count) {
399
+ const currentRenderCount = this.getRenderCount();
400
+
401
+ // no need to continue, since just the count grew - with the exception when the render count was zero before
402
+ if (currentRenderCount > newRenderCount) {
403
+ return;
404
+ }
405
+ }
406
+
407
+ if (__DEV__) {
408
+ if (itemSizeChange && countChange) {
409
+ this.debug(
410
+ `Reset brain: reason itemSize changed and count changed (old ${count}, new ${newCount})`,
411
+ {
412
+ oldItemSize: itemSize,
413
+ newItemSize,
414
+ },
415
+ );
416
+ } else if (itemSizeChange) {
417
+ this.debug(`Reset brain: reason itemSize changed`, {
418
+ oldItemSize: itemSize,
419
+ newItemSize,
420
+ });
421
+ } else if (countChange) {
422
+ this.debug(
423
+ `Reset brain: reason count changed (old ${count}, new ${newCount})`,
424
+ );
425
+ }
426
+ }
427
+
428
+ this.reset();
429
+
430
+ this.updateRenderCount(newRenderCount);
431
+ };
432
+
433
+ private reset = () => {
434
+ this.itemSpanParent = [];
435
+ this.itemSpanValue = [];
436
+ this.itemSizeCache = [];
437
+ this.itemOffsetCache = [0];
438
+ this.renderCount = -1;
439
+ this.totalSize = 0;
440
+ };
441
+
442
+ private computeItemSpanUpTo = (itemIndex: number) => {
443
+ const { count, itemSpan } = this.options;
444
+
445
+ if (!itemSpan) {
446
+ return;
447
+ }
448
+
449
+ const cache = this.itemSpanParent;
450
+ const spanResultArr = this.itemSpanValue;
451
+
452
+ if (cache[itemIndex] === undefined) {
453
+ let i = cache.length;
454
+ let lastIndex = Math.min(itemIndex, count - 1);
455
+
456
+ for (; i <= lastIndex; i++) {
457
+ if (cache[i] === undefined) {
458
+ ItemIndexBag.itemIndex = i;
459
+ let spanValue = itemSpan(ItemIndexBag);
460
+
461
+ if (__DEV__ && spanValue < 1) {
462
+ this.debug(
463
+ 'itemSpan cannot be less than 1 - got %d for index %d',
464
+ spanValue,
465
+ i,
466
+ );
467
+ }
468
+ // this is it's own span parent
469
+ cache[i] = i;
470
+ spanResultArr[i] = spanValue;
471
+
472
+ let spannedItemIndex: number = i;
473
+ while (spanValue > 1) {
474
+ spannedItemIndex += 1;
475
+ cache[spannedItemIndex] = i;
476
+ spanResultArr[spannedItemIndex] = 0;
477
+
478
+ spanValue -= 1;
479
+ }
480
+ }
481
+ }
482
+ // spanParent = this.getItemSizeCacheFor(itemIndex);
483
+ }
484
+ };
485
+
486
+ private computeCacheFor = (itemIndex: number) => {
487
+ if (typeof this.options.itemSize !== 'function') {
488
+ return;
489
+ }
490
+
491
+ const itemSize: number = this.options.itemSize(itemIndex);
492
+ this.itemSizeCache[itemIndex] = itemSize;
493
+
494
+ if (itemIndex > 0) {
495
+ const prevOffset = this.itemOffsetCache[itemIndex - 1];
496
+ const prevSize = this.itemSizeCache[itemIndex - 1];
497
+ if (prevOffset == null) {
498
+ console.error(`Offset was not available for item ${itemIndex - 1}`);
499
+ }
500
+ if (prevSize == null) {
501
+ console.error(`Size was not available for item ${itemIndex - 1}`);
502
+ }
503
+ const offset = prevOffset + prevSize;
504
+ this.itemOffsetCache[itemIndex] = offset;
505
+ }
506
+ };
507
+
508
+ private getItemSizeCacheFor(itemIndex: number): number {
509
+ return this.itemSizeCache[itemIndex];
510
+ }
511
+
512
+ getItemSpan = (itemIndex: number) => {
513
+ if (!this.options.itemSpan) {
514
+ return 1;
515
+ }
516
+ if (this.itemSpanValue[itemIndex] === undefined) {
517
+ this.computeItemSpanUpTo(itemIndex);
518
+ }
519
+ return this.itemSpanValue[itemIndex] || 1;
520
+ };
521
+
522
+ getItemSpanParent = (itemIndex: number) => {
523
+ if (!this.options.itemSpan) {
524
+ return itemIndex;
525
+ }
526
+ if (this.itemSpanParent[itemIndex] === undefined) {
527
+ this.computeItemSpanUpTo(itemIndex);
528
+ }
529
+ return this.itemSpanParent[itemIndex];
530
+ };
531
+
532
+ getItemSizeWithSpan = (itemIndex: number, itemSpan: number) => {
533
+ let itemSize = this.getItemSize(itemIndex);
534
+
535
+ while (itemSpan > 1) {
536
+ itemIndex += 1;
537
+ itemSpan -= 1;
538
+ itemSize += this.getItemSize(itemIndex);
539
+ }
540
+
541
+ return itemSize;
542
+ };
543
+
544
+ /**
545
+ * For now, this doesn't take into account the itemspan, and it's okay not to
546
+ * @param itemIndex
547
+ * @returns the size of the specified item
548
+ */
549
+ getItemSize = (itemIndex: number): number => {
550
+ const { itemSize, count } = this.options;
551
+
552
+ if (typeof itemSize !== 'function') {
553
+ return itemSize;
554
+ }
555
+
556
+ // we make sure when we request the size for an item at a given index
557
+ // all previous items have their size cached
558
+
559
+ // we could take a more optimized approach, but for now, we only have
560
+ // itemMainAxisSize as a function for column virtualization
561
+ // which doesn't need a more performant approach right now, as we dont expect a column count > 10k
562
+
563
+ let cachedSize = this.getItemSizeCacheFor(itemIndex);
564
+
565
+ if (cachedSize === undefined) {
566
+ let i = this.itemSizeCache.length;
567
+ let lastIndex = Math.min(itemIndex, count - 1);
568
+
569
+ for (; i <= lastIndex; i++) {
570
+ this.computeCacheFor(i);
571
+ }
572
+ cachedSize = this.getItemSizeCacheFor(itemIndex);
573
+ }
574
+
575
+ return cachedSize;
576
+ };
577
+
578
+ getTotalSize = () => {
579
+ const { count, itemSize } = this.options;
580
+ if (typeof itemSize !== 'function') {
581
+ return itemSize * count;
582
+ }
583
+ if (this.totalSize !== 0) {
584
+ return this.totalSize;
585
+ }
586
+
587
+ const lastItemSize = this.getItemSize(count - 1);
588
+ const lastItemOffset = this.itemOffsetCache[count - 1];
589
+
590
+ return (this.totalSize = lastItemSize + lastItemOffset);
591
+ };
592
+
593
+ public getItemAt = (scrollPos: number): number => {
594
+ const { itemSize, count } = this.options;
595
+ if (typeof itemSize !== 'function') {
596
+ return Math.min(Math.max(0, Math.floor(scrollPos / itemSize)), count - 1);
597
+ }
598
+
599
+ let lastOffsetIndex = this.itemOffsetCache.length - 1;
600
+ let lastSizeIndex = this.itemSizeCache.length - 1;
601
+ let lastOffset = this.itemOffsetCache[lastOffsetIndex];
602
+
603
+ let _prev: number;
604
+
605
+ while (lastOffset < scrollPos) {
606
+ if (lastOffsetIndex >= count) {
607
+ return count - 1;
608
+ }
609
+ lastSizeIndex += 1;
610
+ this.computeCacheFor(lastSizeIndex);
611
+ _prev = lastOffset;
612
+ lastOffset = this.itemOffsetCache[lastSizeIndex];
613
+ if (_prev === lastOffset && _prev != 0) {
614
+ return count - 1;
615
+ }
616
+ }
617
+
618
+ const searchResult = binarySearch(
619
+ this.itemOffsetCache,
620
+ scrollPos,
621
+ SORT_ASC,
622
+ );
623
+
624
+ if (searchResult >= 0) {
625
+ return searchResult;
626
+ }
627
+
628
+ return ~searchResult - 1;
629
+ };
630
+
631
+ public getItemOffsetFor = (itemIndex: number): number => {
632
+ if (typeof this.options.itemSize !== 'function') {
633
+ return itemIndex * this.options.itemSize;
634
+ }
635
+ let result = this.itemOffsetCache[itemIndex];
636
+
637
+ if (result == null) {
638
+ let lastSizeIndex = this.itemSizeCache.length - 1;
639
+
640
+ while (lastSizeIndex < itemIndex) {
641
+ lastSizeIndex += 1;
642
+ this.computeCacheFor(lastSizeIndex);
643
+ }
644
+
645
+ result = this.itemOffsetCache[itemIndex];
646
+ }
647
+ return result;
648
+ };
649
+
650
+ destroy = () => {
651
+ if (this.destroyed) {
652
+ return;
653
+ }
654
+ this.notifyDestroy();
655
+ this.reset();
656
+
657
+ this.destroyed = true;
658
+ this.onDestroyFns = [];
659
+ this.onScrollFns = [];
660
+ this.onTotalSizeChangeFns = [];
661
+ this.onRenderCountChangeFns = [];
662
+ this.onRenderRangeChangeFns = [];
663
+ };
664
+ }