Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/Flume.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
useCallback,
useEffect,
useImperativeHandle,
useInsertionEffect,
useLayoutEffect,
useMemo,
useReducer,
Expand All @@ -17,12 +18,12 @@ import {
} from 'react';
import { type StyleProp, View, type ViewStyle } from 'react-native';
import { createSerializable } from 'react-native-worklets';
import { cellKinds, type ResolvedCell, resolveCell } from './cells';
import { cellKinds, itemComponentCell, type ResolvedCell, resolveCell } from './cells';
import { type DataState, diffData, itemOf, type NativeItems, typeOf } from './data';
import { FlumeSlot } from './FlumeSlot';
import { hostProps } from './foreign';
import { contentPadding, nativeProps, refreshOf, settleProps, stickyIndicesOf } from './props';
import { lastRowStart } from './separators';
import { createSeparatorLinks, lastRowStart } from './separators';
import {
applySnapshot,
gridCellWidth,
Expand All @@ -42,6 +43,7 @@ import FlumeViewNativeComponent, {
import { logSetData, statsEnabled } from './stats';
import { getHost, isDev } from './store';
import type {
CellRendererProps,
FlumeProps,
FlumeRef,
PressInfo,
Expand All @@ -66,7 +68,7 @@ function FlumeInner<T>(given: FlumeProps<T>, ref: Ref<FlumeRef>): ReactElement {
const {
data,
renderCell,
renderItem,
renderItem: givenRenderItem,
reactCells,
keyExtractor,
getItemType,
Expand All @@ -82,6 +84,8 @@ function FlumeInner<T>(given: FlumeProps<T>, ref: Ref<FlumeRef>): ReactElement {
ListFooterComponentStyle,
ListEmptyComponentStyle,
ItemSeparatorComponent,
ListItemComponent,
CellRendererComponent,
inverted,
itemAnimations = true,
onPress,
Expand Down Expand Up @@ -132,6 +136,12 @@ function FlumeInner<T>(given: FlumeProps<T>, ref: Ref<FlumeRef>): ReactElement {
count.current = data.length;
const dataRef = useRef<readonly unknown[]>(data);
dataRef.current = data;
// The keys separators.highlight reads when called, so the row before is the latest one.
const separatorKeys = useRef<{ keys: readonly string[]; index: ReadonlyMap<string, number> }>({
keys: [],
index: new Map(),
});
const links = useMemo(() => createSeparatorLinks(separatorKeys), []);

// Each distinct message reports once per mount, in development only.
const report = useCallback((level: 'warn' | 'error', message: string) => {
Expand All @@ -140,6 +150,18 @@ function FlumeInner<T>(given: FlumeProps<T>, ref: Ref<FlumeRef>): ReactElement {
console[level](`react-native-flume: ${message}`);
}, []);
const warn = useCallback((message: string) => report('warn', message), [report]);
// Memoized on the component, so every slot keeps its memo while it stays the same.
const itemCell = useMemo(
() => (ListItemComponent == null ? undefined : itemComponentCell(ListItemComponent)),
[ListItemComponent],
);
const renderItem = itemCell ?? givenRenderItem;
if (itemCell !== undefined && givenRenderItem !== undefined) {
warn('ListItemComponent takes precedence over renderItem');
}
if (CellRendererComponent != null && renderCell !== undefined) {
warn('CellRendererComponent wraps React rows only; worklet rows ignore it');
}

if (renderCell !== undefined && renderItem !== undefined) {
warn('renderItem is ignored when renderCell is given; use reactCells to mix the two');
Expand Down Expand Up @@ -342,7 +364,14 @@ function FlumeInner<T>(given: FlumeProps<T>, ref: Ref<FlumeRef>): ReactElement {
return [...bySlot.values()].sort((a, b) => a.slot - b.slot);
}, [fresh, table]);
const { next } = commit.diff;
// Before every layout effect of the commit, and never from a render React throws away.
useInsertionEffect(() => {
separatorKeys.current = next;
}, [next]);
const lastStart = lastRowStart(next.keys.length, horizontal ? 1 : numColumns, stickyIndices);
const cellRenderer = (CellRendererComponent ?? null) as ComponentType<
CellRendererProps<unknown>
> | null;
const separator = (ItemSeparatorComponent ?? null) as
| ComponentType<SeparatorProps<unknown>>
| ReactElement
Expand Down Expand Up @@ -372,6 +401,8 @@ function FlumeInner<T>(given: FlumeProps<T>, ref: Ref<FlumeRef>): ReactElement {
render={resolved?.kind === 'react' ? (resolved.render as ReactCell<unknown>) : undefined}
type={type ?? 'default'}
dataRef={dataRef}
links={links}
cellRenderer={row ? cellRenderer : null}
element={elementFor(entry.kind)}
extraData={extraData}
keyboardTaps={keyboardShouldPersistTaps ?? 'never'}
Expand Down
60 changes: 55 additions & 5 deletions src/FlumeSlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import {
useLayoutEffect,
useMemo,
useRef,
useState,
} from 'react';
import { Keyboard, View } from 'react-native';
import type { SeparatorLinks } from './separators';
import { SlotKind, type SlotLayout, slotStyle } from './slots';
import FlumeSlotViewNativeComponent from './specs/FlumeSlotViewNativeComponent';
import type { ReactCell, SeparatorProps } from './types';
import type { CellRendererProps, ReactCell, SeparatorProps, Separators } from './types';

type FlumeSlotProps = {
slot: number;
Expand All @@ -36,8 +38,29 @@ type FlumeSlotProps = {
last: boolean;
trailingItem: unknown;
inverted: boolean;
links: SeparatorLinks;
cellRenderer: ComponentType<CellRendererProps<unknown>> | null;
};

type SeparatorCellProps = {
component: ComponentType<SeparatorProps<unknown>>;
itemKey: string;
links: SeparatorLinks;
leadingItem: unknown;
trailingItem: unknown;
};

// Holds what highlight and updateProps sent, so they re-render the separator alone.
const SeparatorCell = memo(function SeparatorCell(props: SeparatorCellProps): ReactElement {
const { component, itemKey, links, leadingItem, trailingItem } = props;
const [sent, setSent] = useState<Record<string, unknown>>({});
useLayoutEffect(
() => links.register(itemKey, (update) => setSent((current) => ({ ...current, ...update }))),
[links, itemKey],
);
return createElement(component, { leadingItem, trailingItem, highlighted: false, ...sent });
});

type Shown = {
item?: unknown;
render?: ReactCell<unknown>;
Expand All @@ -55,6 +78,7 @@ const closeKeyboard = () => Keyboard.dismiss();

// FlashList's horizontal cell: the row keeps its height and the separator stretches to it.
const SIDE_BY_SIDE = { flexDirection: 'row', alignSelf: 'flex-start' } as const;
const ROW = { flexDirection: 'row' } as const;

// One React cell. A key that left the data keeps its last item and renderer while native still lists the slot.
export const FlumeSlot = memo(function FlumeSlot(props: FlumeSlotProps): ReactElement | null {
Expand All @@ -77,7 +101,17 @@ export const FlumeSlot = memo(function FlumeSlot(props: FlumeSlotProps): ReactEl
last,
trailingItem,
inverted,
links,
cellRenderer,
} = props;
const separators = useMemo<Separators>(
() => ({
highlight: () => links.highlight(itemKey, true),
unhighlight: () => links.highlight(itemKey, false),
updateProps: (select, newProps) => links.update(itemKey, select, newProps),
}),
[links, itemKey],
);
const snapshot = useRef<Shown>({});
// A row that left the data renders its exit with what it last committed, type and data included.
const live = item !== undefined && render !== undefined;
Expand All @@ -103,11 +137,12 @@ export const FlumeSlot = memo(function FlumeSlot(props: FlumeSlotProps): ReactEl
index: rowIndex,
target,
extraData,
separators,
data: shown.data ?? dataRef.current,
type: shown.type ?? type,
}) ?? null)
: null,
[cell, shown.render, shown.item, rowIndex, target, extraData, shown.type, type],
[cell, shown.render, shown.item, rowIndex, target, extraData, separators, shown.type, type],
);
const entry = { slot, kind, key: itemKey, index: rowIndex, bound: true, minMain };
const style = slotStyle(entry, span, layout);
Expand All @@ -119,10 +154,12 @@ export const FlumeSlot = memo(function FlumeSlot(props: FlumeSlotProps): ReactEl
separator !== null && shown.last === false
? isValidElement(separator)
? separator
: createElement(separator, {
: createElement(SeparatorCell, {
component: separator,
itemKey,
links,
leadingItem: shown.item,
trailingItem: shown.trailingItem,
highlighted: false,
})
: null;
// Keyed, so the row keeps its state when its separator or the separator prop comes or goes.
Expand All @@ -132,10 +169,23 @@ export const FlumeSlot = memo(function FlumeSlot(props: FlumeSlotProps): ReactEl
];
if (inverted) parts.reverse();
content =
layout.horizontal && separator !== null
layout.horizontal && separator !== null && cellRenderer === null
? createElement(View, { style: SIDE_BY_SIDE }, parts)
: parts;
}
// FlatList's cell renderer holds the row and its separator, laid out in a row when horizontal.
if (cell && cellRenderer !== null) {
content = createElement(
cellRenderer,
{
cellKey: itemKey,
index: rowIndex,
item: shown.item,
style: layout.horizontal ? ROW : undefined,
},
content,
);
}
return (
<FlumeSlotViewNativeComponent
slot={slot}
Expand Down
Loading
Loading