@shopify/flash-list 1.8.0 → 2.0.0-alpha.2

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 (170) hide show
  1. package/README.md +147 -26
  2. package/dist/FlashListProps.d.ts +65 -2
  3. package/dist/FlashListProps.d.ts.map +1 -1
  4. package/dist/__tests__/AverageWindow.test.js +35 -0
  5. package/dist/__tests__/AverageWindow.test.js.map +1 -1
  6. package/dist/enableNewCore.d.ts +3 -0
  7. package/dist/enableNewCore.d.ts.map +1 -0
  8. package/dist/enableNewCore.js +25 -0
  9. package/dist/enableNewCore.js.map +1 -0
  10. package/dist/index.d.ts +5 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +28 -8
  13. package/dist/index.js.map +1 -1
  14. package/dist/recyclerview/RecycleKeyManager.d.ts +82 -0
  15. package/dist/recyclerview/RecycleKeyManager.d.ts.map +1 -0
  16. package/dist/recyclerview/RecycleKeyManager.js +135 -0
  17. package/dist/recyclerview/RecycleKeyManager.js.map +1 -0
  18. package/dist/recyclerview/RecyclerView.d.ts +12 -0
  19. package/dist/recyclerview/RecyclerView.d.ts.map +1 -0
  20. package/dist/recyclerview/RecyclerView.js +283 -0
  21. package/dist/recyclerview/RecyclerView.js.map +1 -0
  22. package/dist/recyclerview/RecyclerViewContextProvider.d.ts +12 -0
  23. package/dist/recyclerview/RecyclerViewContextProvider.d.ts.map +1 -0
  24. package/dist/recyclerview/RecyclerViewContextProvider.js +11 -0
  25. package/dist/recyclerview/RecyclerViewContextProvider.js.map +1 -0
  26. package/dist/recyclerview/RecyclerViewManager.d.ts +52 -0
  27. package/dist/recyclerview/RecyclerViewManager.d.ts.map +1 -0
  28. package/dist/recyclerview/RecyclerViewManager.js +323 -0
  29. package/dist/recyclerview/RecyclerViewManager.js.map +1 -0
  30. package/dist/recyclerview/RecyclerViewProps.d.ts +9 -0
  31. package/dist/recyclerview/RecyclerViewProps.d.ts.map +1 -0
  32. package/dist/recyclerview/RecyclerViewProps.js +3 -0
  33. package/dist/recyclerview/RecyclerViewProps.js.map +1 -0
  34. package/dist/recyclerview/ViewHolder.d.ts +45 -0
  35. package/dist/recyclerview/ViewHolder.d.ts.map +1 -0
  36. package/dist/recyclerview/ViewHolder.js +96 -0
  37. package/dist/recyclerview/ViewHolder.js.map +1 -0
  38. package/dist/recyclerview/ViewHolderCollection.d.ts +57 -0
  39. package/dist/recyclerview/ViewHolderCollection.d.ts.map +1 -0
  40. package/dist/recyclerview/ViewHolderCollection.js +75 -0
  41. package/dist/recyclerview/ViewHolderCollection.js.map +1 -0
  42. package/dist/recyclerview/components/CompatScroller.d.ts +7 -0
  43. package/dist/recyclerview/components/CompatScroller.d.ts.map +1 -0
  44. package/dist/recyclerview/components/CompatScroller.js +8 -0
  45. package/dist/recyclerview/components/CompatScroller.js.map +1 -0
  46. package/dist/recyclerview/components/CompatView.d.ts +7 -0
  47. package/dist/recyclerview/components/CompatView.d.ts.map +1 -0
  48. package/dist/recyclerview/components/CompatView.js +8 -0
  49. package/dist/recyclerview/components/CompatView.js.map +1 -0
  50. package/dist/recyclerview/components/ScrollAnchor.d.ts +28 -0
  51. package/dist/recyclerview/components/ScrollAnchor.d.ts.map +1 -0
  52. package/dist/recyclerview/components/ScrollAnchor.js +35 -0
  53. package/dist/recyclerview/components/ScrollAnchor.js.map +1 -0
  54. package/dist/recyclerview/components/StickyHeaders.d.ts +38 -0
  55. package/dist/recyclerview/components/StickyHeaders.d.ts.map +1 -0
  56. package/dist/recyclerview/components/StickyHeaders.js +119 -0
  57. package/dist/recyclerview/components/StickyHeaders.js.map +1 -0
  58. package/dist/recyclerview/helpers/ConsecutiveNumbers.d.ts +51 -0
  59. package/dist/recyclerview/helpers/ConsecutiveNumbers.d.ts.map +1 -0
  60. package/dist/recyclerview/helpers/ConsecutiveNumbers.js +122 -0
  61. package/dist/recyclerview/helpers/ConsecutiveNumbers.js.map +1 -0
  62. package/dist/recyclerview/helpers/EngagedIndicesTracker.d.ts +59 -0
  63. package/dist/recyclerview/helpers/EngagedIndicesTracker.d.ts.map +1 -0
  64. package/dist/recyclerview/helpers/EngagedIndicesTracker.js +138 -0
  65. package/dist/recyclerview/helpers/EngagedIndicesTracker.js.map +1 -0
  66. package/dist/recyclerview/hooks/useBoundDetection.d.ts +19 -0
  67. package/dist/recyclerview/hooks/useBoundDetection.d.ts.map +1 -0
  68. package/dist/recyclerview/hooks/useBoundDetection.js +103 -0
  69. package/dist/recyclerview/hooks/useBoundDetection.js.map +1 -0
  70. package/dist/recyclerview/hooks/useLayoutState.d.ts +12 -0
  71. package/dist/recyclerview/hooks/useLayoutState.d.ts.map +1 -0
  72. package/dist/recyclerview/hooks/useLayoutState.js +43 -0
  73. package/dist/recyclerview/hooks/useLayoutState.js.map +1 -0
  74. package/dist/recyclerview/hooks/useOnLoad.d.ts +25 -0
  75. package/dist/recyclerview/hooks/useOnLoad.d.ts.map +1 -0
  76. package/dist/recyclerview/hooks/useOnLoad.js +73 -0
  77. package/dist/recyclerview/hooks/useOnLoad.js.map +1 -0
  78. package/dist/recyclerview/hooks/useRecyclerViewController.d.ts +72 -0
  79. package/dist/recyclerview/hooks/useRecyclerViewController.d.ts.map +1 -0
  80. package/dist/recyclerview/hooks/useRecyclerViewController.js +370 -0
  81. package/dist/recyclerview/hooks/useRecyclerViewController.js.map +1 -0
  82. package/dist/recyclerview/hooks/useRecyclerViewManager.d.ts +6 -0
  83. package/dist/recyclerview/hooks/useRecyclerViewManager.d.ts.map +1 -0
  84. package/dist/recyclerview/hooks/useRecyclerViewManager.js +27 -0
  85. package/dist/recyclerview/hooks/useRecyclerViewManager.js.map +1 -0
  86. package/dist/recyclerview/hooks/useRecyclingState.d.ts +16 -0
  87. package/dist/recyclerview/hooks/useRecyclingState.d.ts.map +1 -0
  88. package/dist/recyclerview/hooks/useRecyclingState.js +54 -0
  89. package/dist/recyclerview/hooks/useRecyclingState.js.map +1 -0
  90. package/dist/recyclerview/hooks/useSecondaryProps.d.ts +27 -0
  91. package/dist/recyclerview/hooks/useSecondaryProps.d.ts.map +1 -0
  92. package/dist/recyclerview/hooks/useSecondaryProps.js +93 -0
  93. package/dist/recyclerview/hooks/useSecondaryProps.js.map +1 -0
  94. package/dist/recyclerview/hooks/useUnmountFlag.d.ts +11 -0
  95. package/dist/recyclerview/hooks/useUnmountFlag.d.ts.map +1 -0
  96. package/dist/recyclerview/hooks/useUnmountFlag.js +28 -0
  97. package/dist/recyclerview/hooks/useUnmountFlag.js.map +1 -0
  98. package/dist/recyclerview/layout-managers/GridLayoutManager.d.ts +65 -0
  99. package/dist/recyclerview/layout-managers/GridLayoutManager.d.ts.map +1 -0
  100. package/dist/recyclerview/layout-managers/GridLayoutManager.js +204 -0
  101. package/dist/recyclerview/layout-managers/GridLayoutManager.js.map +1 -0
  102. package/dist/recyclerview/layout-managers/LayoutManager.d.ts +281 -0
  103. package/dist/recyclerview/layout-managers/LayoutManager.d.ts.map +1 -0
  104. package/dist/recyclerview/layout-managers/LayoutManager.js +250 -0
  105. package/dist/recyclerview/layout-managers/LayoutManager.js.map +1 -0
  106. package/dist/recyclerview/layout-managers/LinearLayoutManager.d.ts +52 -0
  107. package/dist/recyclerview/layout-managers/LinearLayoutManager.d.ts.map +1 -0
  108. package/dist/recyclerview/layout-managers/LinearLayoutManager.js +191 -0
  109. package/dist/recyclerview/layout-managers/LinearLayoutManager.js.map +1 -0
  110. package/dist/recyclerview/layout-managers/MasonryLayoutManager.d.ts +73 -0
  111. package/dist/recyclerview/layout-managers/MasonryLayoutManager.d.ts.map +1 -0
  112. package/dist/recyclerview/layout-managers/MasonryLayoutManager.js +274 -0
  113. package/dist/recyclerview/layout-managers/MasonryLayoutManager.js.map +1 -0
  114. package/dist/recyclerview/utils/adjustOffsetForRTL.d.ts +12 -0
  115. package/dist/recyclerview/utils/adjustOffsetForRTL.d.ts.map +1 -0
  116. package/dist/recyclerview/utils/adjustOffsetForRTL.js +18 -0
  117. package/dist/recyclerview/utils/adjustOffsetForRTL.js.map +1 -0
  118. package/dist/recyclerview/utils/componentUtils.d.ts +19 -0
  119. package/dist/recyclerview/utils/componentUtils.d.ts.map +1 -0
  120. package/dist/recyclerview/utils/componentUtils.js +32 -0
  121. package/dist/recyclerview/utils/componentUtils.js.map +1 -0
  122. package/dist/recyclerview/utils/findVisibleIndex.d.ts +24 -0
  123. package/dist/recyclerview/utils/findVisibleIndex.d.ts.map +1 -0
  124. package/dist/recyclerview/utils/findVisibleIndex.js +82 -0
  125. package/dist/recyclerview/utils/findVisibleIndex.js.map +1 -0
  126. package/dist/recyclerview/utils/measureLayout.d.ts +56 -0
  127. package/dist/recyclerview/utils/measureLayout.d.ts.map +1 -0
  128. package/dist/recyclerview/utils/measureLayout.js +77 -0
  129. package/dist/recyclerview/utils/measureLayout.js.map +1 -0
  130. package/dist/tsconfig.tsbuildinfo +1 -1
  131. package/dist/utils/AverageWindow.d.ts +13 -0
  132. package/dist/utils/AverageWindow.d.ts.map +1 -1
  133. package/dist/utils/AverageWindow.js +30 -1
  134. package/dist/utils/AverageWindow.js.map +1 -1
  135. package/package.json +1 -1
  136. package/src/FlashListProps.ts +73 -2
  137. package/src/__tests__/AverageWindow.test.ts +49 -1
  138. package/src/enableNewCore.ts +22 -0
  139. package/src/index.ts +21 -0
  140. package/src/recyclerview/RecycleKeyManager.ts +185 -0
  141. package/src/recyclerview/RecyclerView.tsx +500 -0
  142. package/src/recyclerview/RecyclerViewContextProvider.ts +19 -0
  143. package/src/recyclerview/RecyclerViewManager.ts +379 -0
  144. package/src/recyclerview/RecyclerViewProps.ts +10 -0
  145. package/src/recyclerview/ViewHolder.tsx +173 -0
  146. package/src/recyclerview/ViewHolderCollection.tsx +164 -0
  147. package/src/recyclerview/components/CompatScroller.ts +9 -0
  148. package/src/recyclerview/components/CompatView.ts +9 -0
  149. package/src/recyclerview/components/ScrollAnchor.tsx +53 -0
  150. package/src/recyclerview/components/StickyHeaders.tsx +210 -0
  151. package/src/recyclerview/helpers/ConsecutiveNumbers.ts +120 -0
  152. package/src/recyclerview/helpers/EngagedIndicesTracker.ts +191 -0
  153. package/src/recyclerview/hooks/useBoundDetection.ts +127 -0
  154. package/src/recyclerview/hooks/useLayoutState.ts +46 -0
  155. package/src/recyclerview/hooks/useOnLoad.ts +78 -0
  156. package/src/recyclerview/hooks/useRecyclerViewController.tsx +487 -0
  157. package/src/recyclerview/hooks/useRecyclerViewManager.ts +30 -0
  158. package/src/recyclerview/hooks/useRecyclingState.ts +63 -0
  159. package/src/recyclerview/hooks/useSecondaryProps.tsx +119 -0
  160. package/src/recyclerview/hooks/useUnmountFlag.ts +26 -0
  161. package/src/recyclerview/layout-managers/GridLayoutManager.ts +215 -0
  162. package/src/recyclerview/layout-managers/LayoutManager.ts +493 -0
  163. package/src/recyclerview/layout-managers/LinearLayoutManager.ts +167 -0
  164. package/src/recyclerview/layout-managers/MasonryLayoutManager.ts +302 -0
  165. package/src/recyclerview/utils/adjustOffsetForRTL.ts +17 -0
  166. package/src/recyclerview/utils/componentUtils.ts +28 -0
  167. package/src/recyclerview/utils/findVisibleIndex.ts +94 -0
  168. package/src/recyclerview/utils/measureLayout.ts +89 -0
  169. package/src/utils/AverageWindow.ts +33 -0
  170. package/src/viewability/ViewToken.ts +1 -1
package/src/index.ts CHANGED
@@ -1,3 +1,8 @@
1
+ import { default as OriginalFlashList } from "./FlashList";
2
+ import { isNewCoreEnabled } from "./enableNewCore";
3
+ import { RecyclerView } from "./recyclerview/RecyclerView";
4
+
5
+ // Keep this unmodified for TS type checking
1
6
  export { default as FlashList } from "./FlashList";
2
7
  export {
3
8
  FlashListProps,
@@ -37,7 +42,23 @@ export {
37
42
  MasonryListRenderItem,
38
43
  MasonryListRenderItemInfo,
39
44
  } from "./MasonryFlashList";
45
+ export { useLayoutState } from "./recyclerview/hooks/useLayoutState";
46
+ export { useRecyclingState } from "./recyclerview/hooks/useRecyclingState";
40
47
  export { JSFPSMonitor, JSFPSResult } from "./benchmark/JSFPSMonitor";
41
48
  export { autoScroll, Cancellable } from "./benchmark/AutoScrollHelper";
42
49
  export { default as ViewToken } from "./viewability/ViewToken";
43
50
  export { default as CellContainer } from "./native/cell-container/CellContainer";
51
+ export { RecyclerView } from "./recyclerview/RecyclerView";
52
+ export { RecyclerViewProps } from "./recyclerview/RecyclerViewProps";
53
+ export { useRecyclerViewContext } from "./recyclerview/RecyclerViewContextProvider";
54
+
55
+ // @ts-ignore - This is ignored by TypeScript but will be present in the compiled JS
56
+ // In the compiled JS, this will override the previous FlashList export with a conditional one
57
+ if (typeof module !== "undefined" && module.exports) {
58
+ Object.defineProperty(module.exports, "FlashList", {
59
+ get: function () {
60
+ return isNewCoreEnabled() ? RecyclerView : OriginalFlashList;
61
+ },
62
+ configurable: true,
63
+ });
64
+ }
@@ -0,0 +1,185 @@
1
+ export interface RecycleKeyManager {
2
+ /**
3
+ * Retrieves a unique key for an item type, maintaining a separate pool for each type.
4
+ * If a stableId is provided and exists, returns the associated key.
5
+ * Otherwise, generates a new key or reuses one from the pool.
6
+ * @param itemType - The type/category of the item (e.g., 'header', 'product', 'footer')
7
+ * @param stableId - Optional unique identifier for stable item tracking
8
+ * @param currentKey - Optional current key to maintain if it exists in the pool
9
+ * @returns A unique key for the item
10
+ */
11
+ getKey: (itemType: string, stableId: string, currentKey?: string) => string;
12
+
13
+ /**
14
+ * Recycles a key back into its item type's pool when the associated view is no longer visible.
15
+ * This allows the key to be reused for new items of the same type.
16
+ * @param key - The key to be recycled back into the pool
17
+ */
18
+ recycleKey: (key: string) => void;
19
+
20
+ /**
21
+ * Checks if a key is currently available in the recycling pool (not in use).
22
+ * @param key - The key to check
23
+ * @returns True if the key is available in the pool, false otherwise
24
+ */
25
+ hasKeyInPool: (key: string) => boolean;
26
+
27
+ /**
28
+ * Clears all recycled keys from the pool, resetting the recycling system.
29
+ * This is useful when the list needs to be completely reset.
30
+ */
31
+ clearPool: () => void;
32
+ }
33
+
34
+ export class RecycleKeyManagerImpl implements RecycleKeyManager {
35
+ // Maximum number of unique keys that can be active at any time
36
+ private maxItems: number;
37
+
38
+ // Stores pools of recycled keys for each item type
39
+ private keyPools: Map<string, Set<string>>;
40
+
41
+ // Maps active keys to their metadata (item type and stable ID)
42
+ private keyMap: Map<string, { itemType: string; stableId?: string }>;
43
+
44
+ // Maps stable IDs to their corresponding keys for quick lookups
45
+ private stableIdMap: Map<string, string>;
46
+
47
+ // Counter for generating unique sequential keys
48
+ private keyCounter: number;
49
+
50
+ /**
51
+ * Creates a new RecycleKeyManager with a specified maximum number of items.
52
+ * @param maxItems - Maximum number of unique keys that can be active simultaneously.
53
+ * Defaults to Number.MAX_SAFE_INTEGER if not specified.
54
+ */
55
+ constructor(maxItems: number = Number.MAX_SAFE_INTEGER) {
56
+ this.maxItems = maxItems;
57
+ this.keyPools = new Map();
58
+ this.keyMap = new Map();
59
+ this.stableIdMap = new Map();
60
+ this.keyCounter = 0;
61
+ }
62
+
63
+ /**
64
+ * Gets a key for the specified item type, prioritizing stable ID associations.
65
+ * If a stable ID exists, returns its associated key. Otherwise, either reuses
66
+ * a key from the pool or generates a new one.
67
+ * @param itemType - The type/category of the item
68
+ * @param stableId - Optional unique identifier for stable item tracking
69
+ * @param currentKey - Optional current key to maintain if it exists in the pool
70
+ * @returns A unique key for the item
71
+ */
72
+ public getKey(
73
+ itemType: string,
74
+ stableId: string,
75
+ currentKey?: string
76
+ ): string {
77
+ // Return existing key if stableId is already mapped
78
+ if (stableId && this.stableIdMap.has(stableId)) {
79
+ return this.stableIdMap.get(stableId)!;
80
+ }
81
+
82
+ // Get or create the pool for this item type
83
+ let pool = this.keyPools.get(itemType);
84
+ if (!pool) {
85
+ pool = new Set();
86
+ this.keyPools.set(itemType, pool);
87
+ }
88
+
89
+ let key: string;
90
+ // Reuse existing key from pool if available
91
+ if (pool.size > 0) {
92
+ key =
93
+ currentKey && pool.has(currentKey)
94
+ ? currentKey
95
+ : pool.values().next().value;
96
+ pool.delete(key);
97
+ } else {
98
+ // Generate new key if pool is empty
99
+ key = this.generateKey();
100
+ }
101
+
102
+ // Update mappings with new key information
103
+ this.keyMap.set(key, { itemType, stableId });
104
+ if (stableId) {
105
+ this.stableIdMap.set(stableId, key);
106
+ }
107
+
108
+ // Ensure we don't exceed the maximum number of active keys
109
+ this.ensurePoolSize();
110
+
111
+ return key;
112
+ }
113
+
114
+ /**
115
+ * Recycles a key by adding it back to its item type's pool and cleaning up
116
+ * associated mappings. This should be called when a view is no longer visible.
117
+ * @param key - The key to be recycled
118
+ */
119
+ public recycleKey(key: string): void {
120
+ const keyInfo = this.keyMap.get(key);
121
+
122
+ if (!keyInfo) {
123
+ return;
124
+ }
125
+
126
+ const { itemType, stableId } = keyInfo;
127
+ // Clean up stable ID mapping if it exists
128
+ if (stableId) {
129
+ this.stableIdMap.delete(stableId);
130
+ }
131
+
132
+ // Add key back to its type's pool
133
+ let pool = this.keyPools.get(itemType);
134
+ if (!pool) {
135
+ pool = new Set();
136
+ this.keyPools.set(itemType, pool);
137
+ }
138
+
139
+ pool.add(key);
140
+ this.keyMap.delete(key);
141
+ }
142
+
143
+ /**
144
+ * Checks if a key is currently available in the recycling pool.
145
+ * @param key - The key to check
146
+ * @returns True if the key is available in the pool, false otherwise
147
+ */
148
+ public hasKeyInPool(key: string): boolean {
149
+ return !this.keyMap.has(key);
150
+ }
151
+
152
+ /**
153
+ * Clears all recycled keys from the pool, effectively resetting the recycling system.
154
+ * This operation does not affect currently active keys.
155
+ */
156
+ public clearPool() {
157
+ this.keyPools.clear();
158
+ }
159
+
160
+ /**
161
+ * Generates a unique sequential key using an internal counter.
162
+ * @returns A unique key as a string
163
+ */
164
+ private generateKey(): string {
165
+ return (this.keyCounter++).toString();
166
+ }
167
+
168
+ /**
169
+ * Ensures the total number of active keys doesn't exceed the maximum limit.
170
+ * If the limit is exceeded, recycles the oldest keys until within bounds.
171
+ * Note: This operation may impact performance when dealing with large lists.
172
+ * TODO: Check performance impact of this
173
+ */
174
+ private ensurePoolSize(): void {
175
+ if (this.keyMap.size <= this.maxItems) return;
176
+
177
+ const keysToRecycle = Array.from(this.keyMap.keys()).slice(
178
+ 0,
179
+ this.keyMap.size - this.maxItems
180
+ );
181
+ for (const key of keysToRecycle) {
182
+ this.recycleKey(key);
183
+ }
184
+ }
185
+ }