Skip to content

Commit 8308323

Browse files
committed
Fix few more types
1 parent 54994bf commit 8308323

File tree

7 files changed

+58
-22
lines changed

7 files changed

+58
-22
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
"@shopify/jsx-no-complex-expressions": "off",
1919
"@shopify/react-prefer-private-members": "off",
2020
"eslint-comments/disable-enable-pair": "off",
21+
"@shopify/strict-component-boundaries": "off",
2122
"import/no-cycle": "off",
2223
"import/no-named-as-default": "off",
2324
"max-params": "off",

src/FlashList.tsx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
hasUnsupportedKeysInContentContainerStyle,
4343
updateContentStyle,
4444
} from "./utils/ContentContainerUtils";
45+
import { FlashListRef, ScrollToEdgeParams } from "./FlashListRef";
4546

4647
interface StickyProps extends StickyContainerProps {
4748
children: any;
@@ -62,10 +63,10 @@ interface ExtraData<T> {
6263
value?: T;
6364
}
6465

65-
class FlashList<T> extends React.PureComponent<
66-
FlashListProps<T>,
67-
FlashListState<T>
68-
> {
66+
class FlashList<T>
67+
extends React.PureComponent<FlashListProps<T>, FlashListState<T>>
68+
implements FlashListRef<T>
69+
{
6970
private rlvRef?: RecyclerListView<RecyclerListViewProps, any>;
7071
private stickyContentContainerRef?: PureComponentWrapper;
7172
private listFixedDimensionSize = 0;
@@ -788,7 +789,7 @@ class FlashList<T> extends React.PureComponent<
788789
this.rlvRef?.scrollToEnd(Boolean(params?.animated));
789790
}
790791

791-
public scrollToIndex(params: {
792+
public async scrollToIndex(params: {
792793
animated?: boolean | null | undefined;
793794
index: number;
794795
viewOffset?: number | undefined;
@@ -885,7 +886,7 @@ class FlashList<T> extends React.PureComponent<
885886
* @returns {Object} The dimensions of the child container.
886887
*/
887888
public getChildContainerDimensions() {
888-
return this.rlvRef?.getContentDimension();
889+
return this.rlvRef!.getContentDimension();
889890
}
890891

891892
/**
@@ -902,7 +903,7 @@ class FlashList<T> extends React.PureComponent<
902903
* @returns {Object} The size of the list.
903904
*/
904905
public getWindowSize() {
905-
return this.rlvRef?.getRenderedSize();
906+
return this.rlvRef!.getRenderedSize();
906907
}
907908

908909
/**
@@ -924,6 +925,30 @@ class FlashList<T> extends React.PureComponent<
924925
public getFirstVisibleIndex() {
925926
return this.rlvRef?.findApproxFirstVisibleIndex() ?? -1;
926927
}
928+
929+
flashScrollIndicators() {
930+
this.getNativeScrollRef()?.flashScrollIndicators();
931+
}
932+
933+
getNativeScrollRef() {
934+
console.log("hello");
935+
return (this.rlvRef as any)?._scrollComponent?._scrollViewRef;
936+
}
937+
938+
getScrollResponder() {
939+
return this.getNativeScrollRef()?.getScrollResponder();
940+
}
941+
942+
scrollToTop(params?: ScrollToEdgeParams) {
943+
this.rlvRef?.scrollToTop(params?.animated);
944+
}
945+
946+
computeVisibleIndices() {
947+
console.warn(
948+
"computeVisibleIndices is not implemented in old architecture"
949+
);
950+
return { startIndex: -1, endIndex: -2 };
951+
}
927952
}
928953

929954
export default FlashList;

src/FlashListRef.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RVLayout } from "./recyclerview/layout-managers/LayoutManager";
22
import { RecyclerViewProps } from "./recyclerview/RecyclerViewProps";
3+
import { CompatScroller } from "./recyclerview/components/CompatScroller";
34

45
/**
56
* Base parameters for scrolling to the edges of the list.
@@ -112,7 +113,7 @@ export interface FlashListRef<T> {
112113
*
113114
* @returns The native scroll view reference
114115
*/
115-
getNativeScrollRef: () => any;
116+
getNativeScrollRef: () => CompatScroller | null;
116117

117118
/**
118119
* Returns a reference to the scroll responder.
@@ -121,7 +122,7 @@ export interface FlashListRef<T> {
121122
*
122123
* @returns The scroll responder
123124
*/
124-
getScrollResponder: () => any;
125+
getScrollResponder: CompatScroller["getScrollResponder"];
125126

126127
/**
127128
* Returns the underlying scrollable node.
@@ -231,7 +232,7 @@ export interface FlashListRef<T> {
231232
* const itemLayout = listRef.current?.getLayout(5);
232233
* console.log(`Item 5 position: (${itemLayout.x}, ${itemLayout.y})`);
233234
*/
234-
getLayout: (index: number) => RVLayout;
235+
getLayout: (index: number) => RVLayout | undefined;
235236

236237
/**
237238
* Returns the absolute last scroll offset.
@@ -268,7 +269,7 @@ export interface FlashListRef<T> {
268269
* const { startIndex, endIndex } = listRef.current?.getVisibleIndices();
269270
* console.log(`Visible items: ${startIndex} to ${endIndex}`);
270271
*/
271-
getVisibleIndices: () => { startIndex: number; endIndex: number };
272+
computeVisibleIndices: () => { startIndex: number; endIndex: number };
272273

273274
/**
274275
* Returns the index of the first visible item.
@@ -283,7 +284,7 @@ export interface FlashListRef<T> {
283284
getFirstVisibleIndex: () => number;
284285

285286
/**
286-
* Forces recalculation of viewable items.
287+
* Forces recalculation of viewable items (vieability callbacks).
287288
*
288289
* Call this after any operation that might affect item visibility but
289290
* doesn't trigger a scroll event.

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RecyclerView } from "./recyclerview/RecyclerView";
55

66
// Keep this unmodified for TS type checking
77
export { default as FlashList } from "./FlashList";
8+
export { FlashListRef } from "./FlashListRef";
89
export {
910
FlashListProps,
1011
ContentStyle,
@@ -63,6 +64,7 @@ if (
6364
) {
6465
Object.defineProperty(module.exports, "FlashList", {
6566
get() {
67+
return OriginalFlashList;
6668
return isNewCoreEnabled() ? RecyclerView : OriginalFlashList;
6769
},
6870
configurable: true,

src/recyclerview/RecyclerView.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
NativeSyntheticEvent,
2020
} from "react-native";
2121

22+
import { FlashListRef } from "../FlashListRef";
23+
2224
import { RVDimension } from "./layout-managers/LayoutManager";
2325
import {
2426
areDimensionsNotEqual,
@@ -55,7 +57,7 @@ import { RenderTimeTracker } from "./helpers/RenderTimeTracker";
5557
*/
5658
const RecyclerViewComponent = <T,>(
5759
props: RecyclerViewProps<T>,
58-
ref: React.Ref<any>
60+
ref: React.Ref<FlashListRef<T>>
5961
) => {
6062
// Destructure props and initialize refs
6163
const {
@@ -535,7 +537,7 @@ const RecyclerViewComponent = <T,>(
535537

536538
// Type definition for the RecyclerView component
537539
type RecyclerViewType = <T>(
538-
props: RecyclerViewProps<T> & { ref?: React.Ref<any> }
540+
props: RecyclerViewProps<T> & { ref?: React.Ref<FlashListRef<T>> }
539541
) => React.JSX.Element;
540542

541543
// Create and export the memoized, forwarded ref component

src/recyclerview/RecyclerViewManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class RecyclerViewManager<T> {
262262
return this.layoutManager !== undefined;
263263
}
264264

265-
getVisibleIndices() {
265+
computeVisibleIndices() {
266266
if (!this.layoutManager) {
267267
throw new Error(
268268
"LayoutManager is not initialized, visible indices are not unavailable"
@@ -302,7 +302,7 @@ export class RecyclerViewManager<T> {
302302
this.itemViewabilityManager.updateViewableItems(
303303
this.propsRef.masonry
304304
? this.engagedIndicesTracker.getEngagedIndices().toArray()
305-
: this.getVisibleIndices().toArray()
305+
: this.computeVisibleIndices().toArray()
306306
);
307307
}
308308

@@ -396,7 +396,7 @@ export class RecyclerViewManager<T> {
396396
const layoutManager = this.layoutManager;
397397
if (layoutManager) {
398398
this.applyInitialScrollAdjustment();
399-
const visibleIndices = this.getVisibleIndices();
399+
const visibleIndices = this.computeVisibleIndices();
400400
// console.log("---------> visibleIndices", visibleIndices);
401401
this.hasRenderedProgressively = visibleIndices.every(
402402
(index) =>

src/recyclerview/hooks/useRecyclerViewController.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export function useRecyclerViewController<T>(
157157
// Update the tracked first visible item
158158
const firstVisibleIndex = Math.max(
159159
0,
160-
recyclerViewManager.getVisibleIndices().startIndex
160+
recyclerViewManager.computeVisibleIndices().startIndex
161161
);
162162
if (firstVisibleIndex !== undefined && firstVisibleIndex >= 0) {
163163
firstVisibleItemKey.current = keyExtractor(
@@ -431,7 +431,12 @@ export function useRecyclerViewController<T>(
431431
return recyclerViewManager.getWindowSize();
432432
},
433433
getLayout: (index: number) => {
434-
return recyclerViewManager.getLayout(index);
434+
try {
435+
return recyclerViewManager.getLayout(index);
436+
} catch (error) {
437+
console.warn(error);
438+
return undefined;
439+
}
435440
},
436441
getAbsoluteLastScrollOffset: () => {
437442
return recyclerViewManager.getAbsoluteLastScrollOffset();
@@ -442,11 +447,11 @@ export function useRecyclerViewController<T>(
442447
recordInteraction: () => {
443448
recyclerViewManager.recordInteraction();
444449
},
445-
getVisibleIndices: () => {
446-
return recyclerViewManager.getVisibleIndices();
450+
computeVisibleIndices: () => {
451+
return recyclerViewManager.computeVisibleIndices();
447452
},
448453
getFirstVisibleIndex: () => {
449-
return recyclerViewManager.getVisibleIndices().startIndex;
454+
return recyclerViewManager.computeVisibleIndices().startIndex;
450455
},
451456
recomputeViewableItems: () => {
452457
recyclerViewManager.recomputeViewableItems();

0 commit comments

Comments
 (0)