@shopify/flash-list 2.0.0-alpha.10 → 2.0.0-alpha.12
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/README.md +6 -2
- package/dist/AnimatedFlashList.d.ts.map +1 -1
- package/dist/AnimatedFlashList.js +3 -3
- package/dist/AnimatedFlashList.js.map +1 -1
- package/dist/FlashList.d.ts +9 -0
- package/dist/FlashList.d.ts.map +1 -1
- package/dist/FlashList.js +20 -0
- package/dist/FlashList.js.map +1 -1
- package/dist/FlashListProps.d.ts +13 -6
- package/dist/FlashListProps.d.ts.map +1 -1
- package/dist/FlashListProps.js.map +1 -1
- package/dist/FlashListRef.d.ts +305 -0
- package/dist/FlashListRef.d.ts.map +1 -0
- package/dist/FlashListRef.js +3 -0
- package/dist/FlashListRef.js.map +1 -0
- package/dist/__tests__/RecyclerView.test.js +62 -27
- package/dist/__tests__/RecyclerView.test.js.map +1 -1
- package/dist/__tests__/RenderStackManager.test.d.ts +2 -0
- package/dist/__tests__/RenderStackManager.test.d.ts.map +1 -0
- package/dist/__tests__/RenderStackManager.test.js +405 -0
- package/dist/__tests__/RenderStackManager.test.js.map +1 -0
- package/dist/__tests__/useUnmountAwareCallbacks.test.js +1 -1
- package/dist/__tests__/useUnmountAwareCallbacks.test.js.map +1 -1
- package/dist/benchmark/useFlatListBenchmark.js +8 -7
- package/dist/benchmark/useFlatListBenchmark.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/recyclerview/RecyclerView.d.ts +2 -1
- package/dist/recyclerview/RecyclerView.d.ts.map +1 -1
- package/dist/recyclerview/RecyclerView.js +39 -21
- package/dist/recyclerview/RecyclerView.js.map +1 -1
- package/dist/recyclerview/RecyclerViewContextProvider.d.ts +6 -5
- package/dist/recyclerview/RecyclerViewContextProvider.d.ts.map +1 -1
- package/dist/recyclerview/RecyclerViewContextProvider.js.map +1 -1
- package/dist/recyclerview/RecyclerViewManager.d.ts +14 -7
- package/dist/recyclerview/RecyclerViewManager.d.ts.map +1 -1
- package/dist/recyclerview/RecyclerViewManager.js +71 -102
- package/dist/recyclerview/RecyclerViewManager.js.map +1 -1
- package/dist/recyclerview/RenderStackManager.d.ts +85 -0
- package/dist/recyclerview/RenderStackManager.d.ts.map +1 -0
- package/dist/recyclerview/RenderStackManager.js +261 -0
- package/dist/recyclerview/RenderStackManager.js.map +1 -0
- package/dist/recyclerview/ViewHolder.d.ts.map +1 -1
- package/dist/recyclerview/ViewHolder.js +5 -3
- package/dist/recyclerview/ViewHolder.js.map +1 -1
- package/dist/recyclerview/ViewHolderCollection.d.ts +3 -1
- package/dist/recyclerview/ViewHolderCollection.d.ts.map +1 -1
- package/dist/recyclerview/ViewHolderCollection.js +19 -3
- package/dist/recyclerview/ViewHolderCollection.js.map +1 -1
- package/dist/recyclerview/components/ScrollAnchor.d.ts +2 -1
- package/dist/recyclerview/components/ScrollAnchor.d.ts.map +1 -1
- package/dist/recyclerview/components/ScrollAnchor.js +9 -4
- package/dist/recyclerview/components/ScrollAnchor.js.map +1 -1
- package/dist/recyclerview/components/StickyHeaders.d.ts +1 -1
- package/dist/recyclerview/components/StickyHeaders.d.ts.map +1 -1
- package/dist/recyclerview/components/StickyHeaders.js +39 -32
- package/dist/recyclerview/components/StickyHeaders.js.map +1 -1
- package/dist/recyclerview/hooks/useBoundDetection.d.ts +1 -2
- package/dist/recyclerview/hooks/useBoundDetection.d.ts.map +1 -1
- package/dist/recyclerview/hooks/useBoundDetection.js +19 -16
- package/dist/recyclerview/hooks/useBoundDetection.js.map +1 -1
- package/dist/recyclerview/hooks/useOnLoad.d.ts.map +1 -1
- package/dist/recyclerview/hooks/useOnLoad.js +4 -6
- package/dist/recyclerview/hooks/useOnLoad.js.map +1 -1
- package/dist/recyclerview/hooks/useRecyclerViewController.d.ts +3 -48
- package/dist/recyclerview/hooks/useRecyclerViewController.d.ts.map +1 -1
- package/dist/recyclerview/hooks/useRecyclerViewController.js +111 -77
- package/dist/recyclerview/hooks/useRecyclerViewController.js.map +1 -1
- package/dist/recyclerview/hooks/useRecyclerViewManager.d.ts.map +1 -1
- package/dist/recyclerview/hooks/useRecyclerViewManager.js +6 -0
- package/dist/recyclerview/hooks/useRecyclerViewManager.js.map +1 -1
- package/dist/recyclerview/hooks/useSecondaryProps.js +1 -1
- package/dist/recyclerview/hooks/useUnmountAwareCallbacks.d.ts +10 -3
- package/dist/recyclerview/hooks/useUnmountAwareCallbacks.d.ts.map +1 -1
- package/dist/recyclerview/hooks/useUnmountAwareCallbacks.js +33 -4
- package/dist/recyclerview/hooks/useUnmountAwareCallbacks.js.map +1 -1
- package/dist/recyclerview/layout-managers/GridLayoutManager.d.ts +6 -0
- package/dist/recyclerview/layout-managers/GridLayoutManager.d.ts.map +1 -1
- package/dist/recyclerview/layout-managers/GridLayoutManager.js +27 -5
- package/dist/recyclerview/layout-managers/GridLayoutManager.js.map +1 -1
- package/dist/recyclerview/layout-managers/LayoutManager.d.ts +2 -2
- package/dist/recyclerview/layout-managers/LayoutManager.js +2 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/jestSetup.js +30 -11
- package/package.json +1 -1
- package/src/AnimatedFlashList.ts +3 -2
- package/src/FlashList.tsx +24 -0
- package/src/FlashListProps.ts +16 -7
- package/src/FlashListRef.ts +320 -0
- package/src/__tests__/RecyclerView.test.tsx +83 -29
- package/src/__tests__/RenderStackManager.test.ts +488 -0
- package/src/__tests__/useUnmountAwareCallbacks.test.tsx +12 -12
- package/src/benchmark/useFlatListBenchmark.ts +2 -2
- package/src/index.ts +1 -0
- package/src/recyclerview/RecyclerView.tsx +49 -29
- package/src/recyclerview/RecyclerViewContextProvider.ts +12 -6
- package/src/recyclerview/RecyclerViewManager.ts +90 -88
- package/src/recyclerview/RenderStackManager.ts +265 -0
- package/src/recyclerview/ViewHolder.tsx +5 -3
- package/src/recyclerview/ViewHolderCollection.tsx +29 -8
- package/src/recyclerview/components/ScrollAnchor.tsx +21 -8
- package/src/recyclerview/components/StickyHeaders.tsx +62 -44
- package/src/recyclerview/hooks/useBoundDetection.ts +25 -18
- package/src/recyclerview/hooks/useOnLoad.ts +4 -6
- package/src/recyclerview/hooks/useRecyclerViewController.tsx +121 -132
- package/src/recyclerview/hooks/useRecyclerViewManager.ts +6 -0
- package/src/recyclerview/hooks/useSecondaryProps.tsx +1 -1
- package/src/recyclerview/hooks/useUnmountAwareCallbacks.ts +39 -3
- package/src/recyclerview/layout-managers/GridLayoutManager.ts +30 -7
- package/src/recyclerview/layout-managers/LayoutManager.ts +2 -2
- package/dist/__tests__/RecycleKeyManager.test.d.ts +0 -2
- package/dist/__tests__/RecycleKeyManager.test.d.ts.map +0 -1
- package/dist/__tests__/RecycleKeyManager.test.js +0 -210
- package/dist/__tests__/RecycleKeyManager.test.js.map +0 -1
- package/dist/recyclerview/RecycleKeyManager.d.ts +0 -82
- package/dist/recyclerview/RecycleKeyManager.d.ts.map +0 -1
- package/dist/recyclerview/RecycleKeyManager.js +0 -135
- package/dist/recyclerview/RecycleKeyManager.js.map +0 -1
- package/src/__tests__/RecycleKeyManager.test.ts +0 -254
- package/src/recyclerview/RecycleKeyManager.ts +0 -185
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { ConsecutiveNumbers } from "./helpers/ConsecutiveNumbers";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Manages the recycling of rendered items in a virtualized list.
|
|
5
|
+
* This class handles tracking, recycling, and reusing item keys to optimize
|
|
6
|
+
* rendering performance by minimizing creation/destruction of components.
|
|
7
|
+
*/
|
|
8
|
+
export class RenderStackManager {
|
|
9
|
+
public disableRecycling = false;
|
|
10
|
+
|
|
11
|
+
// Maximum number of items that can be in the recycle pool
|
|
12
|
+
private maxItemsInRecyclePool: number;
|
|
13
|
+
|
|
14
|
+
// Stores pools of recycled keys for each item type
|
|
15
|
+
private recycleKeyPools: Map<string, Set<string>>;
|
|
16
|
+
|
|
17
|
+
// Maps active keys to their metadata (item type and stable ID)
|
|
18
|
+
private keyMap: Map<
|
|
19
|
+
string,
|
|
20
|
+
{ itemType: string; index: number; stableId: string }
|
|
21
|
+
>;
|
|
22
|
+
|
|
23
|
+
// Maps stable IDs to their corresponding keys for quick lookups
|
|
24
|
+
private stableIdMap: Map<string, string>;
|
|
25
|
+
|
|
26
|
+
// Counter for generating unique sequential keys
|
|
27
|
+
private keyCounter: number;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param maxItemsInRecyclePool - Maximum number of items that can be in the recycle pool
|
|
31
|
+
*/
|
|
32
|
+
constructor(maxItemsInRecyclePool: number = Number.MAX_SAFE_INTEGER) {
|
|
33
|
+
this.maxItemsInRecyclePool = maxItemsInRecyclePool;
|
|
34
|
+
this.recycleKeyPools = new Map();
|
|
35
|
+
this.keyMap = new Map();
|
|
36
|
+
this.stableIdMap = new Map();
|
|
37
|
+
this.keyCounter = 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Synchronizes the render stack with the current state of data.
|
|
42
|
+
* This method is the core orchestrator that:
|
|
43
|
+
* 1. Recycles keys for items that are no longer valid
|
|
44
|
+
* 2. Updates existing keys for items that remain visible
|
|
45
|
+
* 3. Assigns new keys for newly visible items
|
|
46
|
+
* 4. Cleans up excess items to maintain the recycling pool size
|
|
47
|
+
*
|
|
48
|
+
* @param getStableId - Function to get a stable identifier for an item at a specific index
|
|
49
|
+
* @param getItemType - Function to get the type of an item at a specific index
|
|
50
|
+
* @param engagedIndices - Collection of indices that are currently visible or engaged
|
|
51
|
+
* @param dataLength - Total length of the data set
|
|
52
|
+
*/
|
|
53
|
+
public sync(
|
|
54
|
+
getStableId: (index: number) => string,
|
|
55
|
+
getItemType: (index: number) => string,
|
|
56
|
+
engagedIndices: ConsecutiveNumbers,
|
|
57
|
+
dataLength: number
|
|
58
|
+
) {
|
|
59
|
+
if (this.disableRecycling) {
|
|
60
|
+
this.clearRecyclePool();
|
|
61
|
+
}
|
|
62
|
+
// Recycle keys for items that are no longer valid or visible
|
|
63
|
+
this.keyMap.forEach((keyInfo, key) => {
|
|
64
|
+
const { index, stableId, itemType } = keyInfo;
|
|
65
|
+
if (index >= dataLength) {
|
|
66
|
+
this.recycleKey(key);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const newStableId = getStableId(index);
|
|
70
|
+
const newItemType = getItemType(index);
|
|
71
|
+
if (stableId !== newStableId || itemType !== newItemType) {
|
|
72
|
+
this.recycleKey(key);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (!engagedIndices.includes(index)) {
|
|
76
|
+
this.recycleKey(key);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// First pass: process items that already have optimized keys
|
|
81
|
+
for (const index of engagedIndices) {
|
|
82
|
+
if (this.hasOptimizedKey(getStableId(index))) {
|
|
83
|
+
this.syncItem(index, getItemType(index), getStableId(index));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Second pass: process remaining items that need new keys
|
|
88
|
+
for (const index of engagedIndices) {
|
|
89
|
+
if (!this.hasOptimizedKey(getStableId(index))) {
|
|
90
|
+
this.syncItem(index, getItemType(index), getStableId(index));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Clean up stale items and manage the recycle pool size
|
|
95
|
+
this.cleanup(getStableId, engagedIndices, dataLength);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Checks if a stable ID already has an assigned key
|
|
100
|
+
*/
|
|
101
|
+
private hasOptimizedKey(stableId: string): boolean {
|
|
102
|
+
return this.stableIdMap.has(stableId);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Cleans up stale keys and manages the recycle pool size.
|
|
107
|
+
* This ensures we don't maintain references to items that are no longer in the dataset,
|
|
108
|
+
* and limits the number of recycled items to avoid excessive memory usage.
|
|
109
|
+
*/
|
|
110
|
+
private cleanup(
|
|
111
|
+
getStableId: (index: number) => string,
|
|
112
|
+
engagedIndices: ConsecutiveNumbers,
|
|
113
|
+
dataLength: number
|
|
114
|
+
) {
|
|
115
|
+
const itemsToDelete = new Array<string>();
|
|
116
|
+
|
|
117
|
+
// Remove items that are no longer in the dataset
|
|
118
|
+
for (const [key, keyInfo] of this.keyMap.entries()) {
|
|
119
|
+
const { index, itemType, stableId } = keyInfo;
|
|
120
|
+
if (index >= dataLength || getStableId(index) !== stableId) {
|
|
121
|
+
this.deleteKeyFromRecyclePool(itemType, key);
|
|
122
|
+
this.stableIdMap.delete(stableId);
|
|
123
|
+
itemsToDelete.push(key);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
for (const key of itemsToDelete) {
|
|
128
|
+
this.keyMap.delete(key);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Limit the size of the recycle pool
|
|
132
|
+
const itemsRenderedForRecycling = this.keyMap.size - engagedIndices.length;
|
|
133
|
+
if (itemsRenderedForRecycling > this.maxItemsInRecyclePool) {
|
|
134
|
+
const deleteCount =
|
|
135
|
+
itemsRenderedForRecycling - this.maxItemsInRecyclePool;
|
|
136
|
+
let deleted = 0;
|
|
137
|
+
|
|
138
|
+
// Use a for loop so we can break early once we've deleted enough items
|
|
139
|
+
const entries = Array.from(this.keyMap.entries()).reverse();
|
|
140
|
+
for (let i = 0; i < entries.length && deleted < deleteCount; i++) {
|
|
141
|
+
const [key, keyInfo] = entries[i];
|
|
142
|
+
const { index, itemType, stableId } = keyInfo;
|
|
143
|
+
|
|
144
|
+
if (!engagedIndices.includes(index)) {
|
|
145
|
+
this.deleteKeyFromRecyclePool(itemType, key);
|
|
146
|
+
this.stableIdMap.delete(stableId);
|
|
147
|
+
this.keyMap.delete(key);
|
|
148
|
+
deleted++;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Places a key back into its type-specific recycle pool for future reuse
|
|
156
|
+
*/
|
|
157
|
+
private recycleKey(key: string): void {
|
|
158
|
+
if (this.disableRecycling) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const keyInfo = this.keyMap.get(key);
|
|
162
|
+
|
|
163
|
+
if (!keyInfo) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const { itemType } = keyInfo;
|
|
168
|
+
|
|
169
|
+
// Add key back to its type's pool
|
|
170
|
+
const pool = this.getRecyclePoolForType(itemType);
|
|
171
|
+
|
|
172
|
+
pool.add(key);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Returns the current render stack containing all active keys and their metadata
|
|
177
|
+
*/
|
|
178
|
+
public getRenderStack() {
|
|
179
|
+
return this.keyMap;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Syncs an individual item by assigning it an appropriate key.
|
|
184
|
+
* Will use an existing key if available, or generate a new one.
|
|
185
|
+
*
|
|
186
|
+
* @returns The key assigned to the item
|
|
187
|
+
*/
|
|
188
|
+
private syncItem(index: number, itemType: string, stableId: string): string {
|
|
189
|
+
// Try to reuse an existing key, or get one from the recycle pool, or generate a new one
|
|
190
|
+
const newKey =
|
|
191
|
+
this.stableIdMap.get(stableId) ||
|
|
192
|
+
this.getKeyFromRecyclePool(itemType) ||
|
|
193
|
+
this.generateKey();
|
|
194
|
+
|
|
195
|
+
const keyInfo = this.keyMap.get(newKey);
|
|
196
|
+
if (keyInfo) {
|
|
197
|
+
// Update an existing key's metadata
|
|
198
|
+
this.deleteKeyFromRecyclePool(itemType, newKey);
|
|
199
|
+
this.deleteKeyFromRecyclePool(keyInfo.itemType, newKey);
|
|
200
|
+
this.stableIdMap.delete(keyInfo.stableId);
|
|
201
|
+
keyInfo.index = index;
|
|
202
|
+
keyInfo.itemType = itemType;
|
|
203
|
+
keyInfo.stableId = stableId;
|
|
204
|
+
} else {
|
|
205
|
+
// Create a new entry in the key map
|
|
206
|
+
this.keyMap.set(newKey, {
|
|
207
|
+
itemType,
|
|
208
|
+
index,
|
|
209
|
+
stableId,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
this.stableIdMap.set(stableId, newKey);
|
|
213
|
+
|
|
214
|
+
return newKey;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Clears all recycled keys from the pool, effectively resetting the recycling system.
|
|
219
|
+
* This operation does not affect currently active keys.
|
|
220
|
+
*/
|
|
221
|
+
private clearRecyclePool() {
|
|
222
|
+
this.recycleKeyPools.clear();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Generates a unique sequential key using an internal counter.
|
|
227
|
+
* @returns A unique key as a string
|
|
228
|
+
*/
|
|
229
|
+
private generateKey(): string {
|
|
230
|
+
return (this.keyCounter++).toString();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Removes a specific key from its type's recycle pool
|
|
235
|
+
*/
|
|
236
|
+
private deleteKeyFromRecyclePool(itemType: string, key: string) {
|
|
237
|
+
this.recycleKeyPools.get(itemType)?.delete(key);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Gets or creates a recycle pool for a specific item type
|
|
242
|
+
*/
|
|
243
|
+
private getRecyclePoolForType(itemType: string) {
|
|
244
|
+
let pool = this.recycleKeyPools.get(itemType);
|
|
245
|
+
if (!pool) {
|
|
246
|
+
pool = new Set();
|
|
247
|
+
this.recycleKeyPools.set(itemType, pool);
|
|
248
|
+
}
|
|
249
|
+
return pool;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Retrieves and removes a key from the type's recycle pool
|
|
254
|
+
* @returns A recycled key or undefined if none available
|
|
255
|
+
*/
|
|
256
|
+
private getKeyFromRecyclePool(itemType: string) {
|
|
257
|
+
const pool = this.getRecyclePoolForType(itemType);
|
|
258
|
+
if (pool.size > 0) {
|
|
259
|
+
const key = pool.values().next().value;
|
|
260
|
+
pool.delete(key);
|
|
261
|
+
return key;
|
|
262
|
+
}
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
@@ -88,7 +88,7 @@ const ViewHolderInternal = <TItem,>(props: ViewHolderProps<TItem>) => {
|
|
|
88
88
|
);
|
|
89
89
|
|
|
90
90
|
const separator = useMemo(() => {
|
|
91
|
-
return ItemSeparatorComponent ? (
|
|
91
|
+
return ItemSeparatorComponent && trailingItem !== undefined ? (
|
|
92
92
|
<ItemSeparatorComponent leadingItem={item} trailingItem={trailingItem} />
|
|
93
93
|
) : null;
|
|
94
94
|
}, [ItemSeparatorComponent, item, trailingItem]);
|
|
@@ -97,7 +97,10 @@ const ViewHolderInternal = <TItem,>(props: ViewHolderProps<TItem>) => {
|
|
|
97
97
|
|
|
98
98
|
const children = useMemo(() => {
|
|
99
99
|
return renderItem?.({ item, index, extraData, target }) ?? null;
|
|
100
|
-
|
|
100
|
+
// TODO: Test more thoroughly
|
|
101
|
+
// We don't really to re-render the children when the index changes
|
|
102
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
103
|
+
}, [item, extraData, target, renderItem]);
|
|
101
104
|
|
|
102
105
|
const style = {
|
|
103
106
|
flexDirection: horizontal ? "row" : "column",
|
|
@@ -110,7 +113,6 @@ const ViewHolderInternal = <TItem,>(props: ViewHolderProps<TItem>) => {
|
|
|
110
113
|
maxWidth: layout.maxWidth,
|
|
111
114
|
left: layout.x,
|
|
112
115
|
top: layout.y,
|
|
113
|
-
zIndex: 0,
|
|
114
116
|
} as const;
|
|
115
117
|
|
|
116
118
|
// TODO: Fix this type issue
|
|
@@ -21,7 +21,7 @@ export interface ViewHolderCollectionProps<TItem> {
|
|
|
21
21
|
/** The data array to be rendered */
|
|
22
22
|
data: FlashListProps<TItem>["data"];
|
|
23
23
|
/** Map of indices to React keys for each rendered item */
|
|
24
|
-
renderStack: Map<
|
|
24
|
+
renderStack: Map<string, { index: number }>;
|
|
25
25
|
/** Function to get layout information for a specific index */
|
|
26
26
|
getLayout: (index: number) => RVLayout;
|
|
27
27
|
/** Ref to control layout updates from parent components */
|
|
@@ -99,27 +99,37 @@ export const ViewHolderCollection = <TItem,>(
|
|
|
99
99
|
// );
|
|
100
100
|
recyclerViewContext?.layout();
|
|
101
101
|
}
|
|
102
|
+
// we need to run this callback on when fixedContainerSize changes
|
|
103
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
102
104
|
}, [fixedContainerSize]);
|
|
103
105
|
|
|
104
106
|
useLayoutEffect(() => {
|
|
105
107
|
if (renderId > 0) {
|
|
106
108
|
onCommitLayoutEffect?.();
|
|
107
109
|
}
|
|
110
|
+
// we need to run this callback on when renderId changes
|
|
111
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
108
112
|
}, [renderId]);
|
|
109
113
|
|
|
110
114
|
useEffect(() => {
|
|
111
115
|
if (renderId > 0) {
|
|
112
116
|
onCommitEffect?.();
|
|
113
117
|
}
|
|
118
|
+
// we need to run this callback on when renderId changes
|
|
119
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
114
120
|
}, [renderId]);
|
|
115
121
|
|
|
116
122
|
// Expose forceUpdate through ref
|
|
117
|
-
useImperativeHandle(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
+
useImperativeHandle(
|
|
124
|
+
viewHolderCollectionRef,
|
|
125
|
+
() => ({
|
|
126
|
+
commitLayout: () => {
|
|
127
|
+
// This will trigger a re-render of the component
|
|
128
|
+
setRenderId((prev) => prev + 1);
|
|
129
|
+
},
|
|
130
|
+
}),
|
|
131
|
+
[setRenderId]
|
|
132
|
+
);
|
|
123
133
|
|
|
124
134
|
const hasData = data && data.length > 0;
|
|
125
135
|
|
|
@@ -128,6 +138,17 @@ export const ViewHolderCollection = <TItem,>(
|
|
|
128
138
|
height: containerLayout?.height,
|
|
129
139
|
};
|
|
130
140
|
|
|
141
|
+
// sort by index and log
|
|
142
|
+
// const sortedRenderStack = Array.from(renderStack.entries()).sort(
|
|
143
|
+
// ([, a], [, b]) => a.index - b.index
|
|
144
|
+
// );
|
|
145
|
+
// console.log(
|
|
146
|
+
// "sortedRenderStack",
|
|
147
|
+
// sortedRenderStack.map(([reactKey, { index }]) => {
|
|
148
|
+
// return `${index} => ${reactKey}`;
|
|
149
|
+
// })
|
|
150
|
+
// );
|
|
151
|
+
|
|
131
152
|
return (
|
|
132
153
|
<CompatView
|
|
133
154
|
// TODO: Take care of web scroll bar here
|
|
@@ -135,7 +156,7 @@ export const ViewHolderCollection = <TItem,>(
|
|
|
135
156
|
>
|
|
136
157
|
{containerLayout &&
|
|
137
158
|
hasData &&
|
|
138
|
-
Array.from(renderStack, ([
|
|
159
|
+
Array.from(renderStack.entries(), ([reactKey, { index }]) => {
|
|
139
160
|
const item = data[index];
|
|
140
161
|
const trailingItem = ItemSeparatorComponent
|
|
141
162
|
? data[index + 1]
|
|
@@ -15,6 +15,7 @@ import { CompatView } from "./CompatView";
|
|
|
15
15
|
export interface ScrollAnchorProps {
|
|
16
16
|
/** Ref to access scroll anchor methods */
|
|
17
17
|
scrollAnchorRef: React.Ref<ScrollAnchorRef>;
|
|
18
|
+
horizontal: boolean;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
/**
|
|
@@ -31,24 +32,36 @@ export interface ScrollAnchorRef {
|
|
|
31
32
|
* @param props - Component props
|
|
32
33
|
* @returns An invisible anchor element used for scrolling
|
|
33
34
|
*/
|
|
34
|
-
export function ScrollAnchor({
|
|
35
|
+
export function ScrollAnchor({
|
|
36
|
+
scrollAnchorRef,
|
|
37
|
+
horizontal,
|
|
38
|
+
}: ScrollAnchorProps) {
|
|
35
39
|
const [scrollOffset, setScrollOffset] = useState(1000000); // TODO: Fix this value
|
|
36
40
|
|
|
37
41
|
// Expose scrollBy method through ref
|
|
38
|
-
useImperativeHandle(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
useImperativeHandle(
|
|
43
|
+
scrollAnchorRef,
|
|
44
|
+
() => ({
|
|
45
|
+
scrollBy: (offset: number) => {
|
|
46
|
+
setScrollOffset((prev) => prev + offset);
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
[]
|
|
50
|
+
);
|
|
43
51
|
|
|
44
52
|
// Create an invisible anchor element that can be positioned
|
|
45
53
|
const anchor = useMemo(() => {
|
|
46
54
|
return (
|
|
47
55
|
<CompatView
|
|
48
|
-
style={{
|
|
56
|
+
style={{
|
|
57
|
+
position: "absolute",
|
|
58
|
+
height: 0,
|
|
59
|
+
top: horizontal ? 0 : scrollOffset,
|
|
60
|
+
left: horizontal ? scrollOffset : 0,
|
|
61
|
+
}}
|
|
49
62
|
/>
|
|
50
63
|
);
|
|
51
|
-
}, [scrollOffset]);
|
|
64
|
+
}, [scrollOffset, horizontal]);
|
|
52
65
|
|
|
53
66
|
return anchor;
|
|
54
67
|
}
|
|
@@ -49,6 +49,11 @@ export interface StickyHeaderRef {
|
|
|
49
49
|
reportScrollEvent: (event: NativeScrollEvent) => void;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
interface StickyHeaderState {
|
|
53
|
+
currentStickyIndex: number;
|
|
54
|
+
pushStartsAt: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
52
57
|
export const StickyHeaders = <TItem,>({
|
|
53
58
|
stickyHeaderIndices,
|
|
54
59
|
renderItem,
|
|
@@ -58,25 +63,35 @@ export const StickyHeaders = <TItem,>({
|
|
|
58
63
|
data,
|
|
59
64
|
extraData,
|
|
60
65
|
}: StickyHeaderProps<TItem>) => {
|
|
61
|
-
const [
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
const [stickyHeaderState, setStickyHeaderState] = useState<StickyHeaderState>(
|
|
67
|
+
{
|
|
68
|
+
currentStickyIndex: -1,
|
|
69
|
+
pushStartsAt: Number.MAX_SAFE_INTEGER,
|
|
70
|
+
}
|
|
71
|
+
);
|
|
65
72
|
|
|
66
|
-
const { currentStickyIndex,
|
|
73
|
+
const { currentStickyIndex, pushStartsAt } = stickyHeaderState;
|
|
67
74
|
|
|
68
|
-
//
|
|
75
|
+
// sort indices and memoize compute
|
|
69
76
|
const sortedIndices = useMemo(() => {
|
|
70
77
|
return stickyHeaderIndices.sort((first, second) => first - second);
|
|
71
78
|
}, [stickyHeaderIndices]);
|
|
72
79
|
|
|
80
|
+
const legthInvalid =
|
|
81
|
+
sortedIndices.length === 0 ||
|
|
82
|
+
recyclerViewManager.getDataLength() <=
|
|
83
|
+
sortedIndices[sortedIndices.length - 1];
|
|
84
|
+
|
|
73
85
|
const compute = useCallback(() => {
|
|
74
|
-
|
|
86
|
+
if (legthInvalid) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const adjustedScrollOffset = recyclerViewManager.getLastScrollOffset();
|
|
75
90
|
|
|
76
91
|
// Binary search for current sticky index
|
|
77
92
|
const currentIndexInArray = findCurrentStickyIndex(
|
|
78
93
|
sortedIndices,
|
|
79
|
-
|
|
94
|
+
adjustedScrollOffset,
|
|
80
95
|
(index) => recyclerViewManager.getLayout(index).y
|
|
81
96
|
);
|
|
82
97
|
|
|
@@ -87,16 +102,33 @@ export const StickyHeaders = <TItem,>({
|
|
|
87
102
|
newNextStickyIndex = -1;
|
|
88
103
|
}
|
|
89
104
|
|
|
105
|
+
// To make sure header offset is 0 in the interpolate compute
|
|
106
|
+
const newNextStickyY =
|
|
107
|
+
newNextStickyIndex === -1
|
|
108
|
+
? Number.MAX_SAFE_INTEGER
|
|
109
|
+
: (recyclerViewManager.tryGetLayout(newNextStickyIndex)?.y ?? 0) +
|
|
110
|
+
recyclerViewManager.firstItemOffset;
|
|
111
|
+
const newCurrentStickyHeight =
|
|
112
|
+
recyclerViewManager.tryGetLayout(newStickyIndex)?.height ?? 0;
|
|
113
|
+
|
|
114
|
+
const newPushStartsAt = newNextStickyY - newCurrentStickyHeight;
|
|
115
|
+
|
|
90
116
|
if (
|
|
91
117
|
newStickyIndex !== currentStickyIndex ||
|
|
92
|
-
|
|
118
|
+
newPushStartsAt !== pushStartsAt
|
|
93
119
|
) {
|
|
94
|
-
|
|
120
|
+
setStickyHeaderState({
|
|
95
121
|
currentStickyIndex: newStickyIndex,
|
|
96
|
-
|
|
122
|
+
pushStartsAt: newPushStartsAt,
|
|
97
123
|
});
|
|
98
124
|
}
|
|
99
|
-
}, [
|
|
125
|
+
}, [
|
|
126
|
+
legthInvalid,
|
|
127
|
+
recyclerViewManager,
|
|
128
|
+
sortedIndices,
|
|
129
|
+
currentStickyIndex,
|
|
130
|
+
pushStartsAt,
|
|
131
|
+
]);
|
|
100
132
|
|
|
101
133
|
useEffect(() => {
|
|
102
134
|
compute();
|
|
@@ -115,35 +147,19 @@ export const StickyHeaders = <TItem,>({
|
|
|
115
147
|
|
|
116
148
|
const refHolder = useRef(new Map()).current;
|
|
117
149
|
|
|
118
|
-
// Memoize translateY calculation
|
|
119
150
|
const translateY = useMemo(() => {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
inputRange: [0, Infinity],
|
|
123
|
-
outputRange: [0, 0],
|
|
124
|
-
extrapolate: "clamp",
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const currentLayout = recyclerViewManager.getLayout(currentStickyIndex);
|
|
129
|
-
const nextLayout = recyclerViewManager.getLayout(nextStickyIndex);
|
|
130
|
-
|
|
131
|
-
const pushStartsAt = nextLayout.y - currentLayout.height;
|
|
151
|
+
const currentStickyHeight =
|
|
152
|
+
recyclerViewManager.tryGetLayout(currentStickyIndex)?.height ?? 0;
|
|
132
153
|
|
|
133
154
|
return scrollY.interpolate({
|
|
134
|
-
inputRange: [
|
|
135
|
-
|
|
136
|
-
nextLayout.y + recyclerViewManager.firstItemOffset,
|
|
137
|
-
],
|
|
138
|
-
outputRange: [0, -currentLayout.height],
|
|
155
|
+
inputRange: [pushStartsAt, pushStartsAt + currentStickyHeight],
|
|
156
|
+
outputRange: [0, -currentStickyHeight],
|
|
139
157
|
extrapolate: "clamp",
|
|
140
158
|
});
|
|
141
|
-
}, [
|
|
159
|
+
}, [recyclerViewManager, currentStickyIndex, scrollY, pushStartsAt]);
|
|
142
160
|
|
|
143
161
|
// Memoize header content
|
|
144
162
|
const headerContent = useMemo(() => {
|
|
145
|
-
if (currentStickyIndex === -1) return null;
|
|
146
|
-
|
|
147
163
|
return (
|
|
148
164
|
<CompatAnimatedView
|
|
149
165
|
style={{
|
|
@@ -155,19 +171,21 @@ export const StickyHeaders = <TItem,>({
|
|
|
155
171
|
transform: [{ translateY }],
|
|
156
172
|
}}
|
|
157
173
|
>
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
174
|
+
{currentStickyIndex !== -1 ? (
|
|
175
|
+
<ViewHolder
|
|
176
|
+
index={currentStickyIndex}
|
|
177
|
+
item={data[currentStickyIndex]}
|
|
178
|
+
renderItem={renderItem}
|
|
179
|
+
layout={{ x: 0, y: 0, width: 0, height: 0 }}
|
|
180
|
+
refHolder={refHolder}
|
|
181
|
+
extraData={extraData}
|
|
182
|
+
trailingItem={null}
|
|
183
|
+
target="StickyHeader"
|
|
184
|
+
/>
|
|
185
|
+
) : null}
|
|
168
186
|
</CompatAnimatedView>
|
|
169
187
|
);
|
|
170
|
-
}, [currentStickyIndex, data, renderItem,
|
|
188
|
+
}, [translateY, currentStickyIndex, data, renderItem, refHolder, extraData]);
|
|
171
189
|
|
|
172
190
|
return headerContent;
|
|
173
191
|
};
|