Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuever committed Jun 26, 2023
1 parent 1174924 commit bf93610
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
10 changes: 10 additions & 0 deletions packages/data-model/src/ItemMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ItemMeta extends ViewabilityItemMeta {
readonly getMetaOnViewableItemsChanged?: any;
readonly _ownerId: string;
readonly _canIUseRIC?: boolean;
private _isApproximateLayout: boolean;

constructor(props: {
onViewable?: StateEventListener;
Expand Down Expand Up @@ -60,6 +61,7 @@ class ItemMeta extends ViewabilityItemMeta {
: {};

this._canIUseRIC = canIUseRIC;
this._isApproximateLayout = false;

// this.addStateListener = this.addStateListener.bind(this);
// this.removeStateListener = this.removeStateListener.bind(this);
Expand All @@ -70,6 +72,14 @@ class ItemMeta extends ViewabilityItemMeta {
return this._id;
}

get isApproximatedLayout() {
return this._isApproximateLayout;
}

set isApproximateLayout(value: boolean) {
this._isApproximateLayout = value;
}

setLayout(layout: ItemLayout) {
this._layout = layout;
}
Expand Down
44 changes: 34 additions & 10 deletions packages/data-model/src/ListDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import memoizeOne from 'memoize-one';
import shallowEqual from '@x-oasis/shallow-equal';
import shallowArrayEqual from '@x-oasis/shallow-array-equal';
import StillnessHelper from './utils/StillnessHelper';
import defaultBooleanValue from '@x-oasis/default-boolean-value';

class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
private _data: Array<ItemT> = [];
Expand Down Expand Up @@ -113,13 +114,17 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
state: ListState<ItemT>
) => RecycleStateResult<ItemT>;

private _itemApproximateLength: number;
private _approximateMode: boolean;

constructor(props: ListDimensionsProps<ItemT>) {
super({
...props,
isIntervalTreeItems: true,
});
const {
store,
recycleEnabled,
data = [],
deps = [],
keyExtractor,
Expand All @@ -140,18 +145,20 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
onEndReachedTimeoutThreshold,
distanceFromEndThresholdValue,
onEndReachedHandlerTimeoutThreshold,
useItemApproximateLength,
maxCountOfHandleOnEndReachedAfterStillness,
} = props;
// this.approximateLayoutGetter = this.approximateLayoutGetter.bind(this)

this._keyExtractor = keyExtractor;
this._getItemLayout =
getItemLayout ||
(itemApproximateLength
? (_, index) => ({
length: itemApproximateLength,
index,
})
: null);
this._itemApproximateLength = itemApproximateLength || 0;
this._getItemLayout = getItemLayout;
this._approximateMode = recycleEnabled
? defaultBooleanValue(
useItemApproximateLength,
typeof this._getItemLayout !== 'function' ||
!this._itemApproximateLength
)
: false;
this._getItemSeparatorLength = getItemSeparatorLength;
// for ListItem include a basic items condition
this._parentItemsDimensions = parentItemsDimensions;
Expand Down Expand Up @@ -473,6 +480,12 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
const oldLength = this.intervalTree.getHeap()[1];
this.intervalTree.set(index, length);
const nextLength = this.intervalTree.getHeap()[1];
const len = this.intervalTree.getMaxUsefulLength();

if (typeof nextLength === 'number')
this._itemApproximateLength = this.normalizeLengthNumber(
nextLength / Math.max(len, 1)
);

if (oldLength !== nextLength && this._listGroupDimension) {
this._listGroupDimension.recalculateDimensionsIntervalTreeBatchinator.schedule();
Expand Down Expand Up @@ -521,6 +534,15 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
canIUseRIC: this.canIUseRIC,
});

if (this._approximateMode) {
meta.setLayout({ x: 0, y: 0, height: 0, width: 0 });
this._selectValue.setLength(
meta.getLayout(),
this._itemApproximateLength
);
meta.isApproximateLayout = true;
}

if (typeof this._getItemLayout === 'function') {
const { length } = this._getItemLayout(data, index);
// only List with getItemLayout has default layout value
Expand Down Expand Up @@ -884,6 +906,7 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
let length = this.normalizeLengthNumber(info);
if (this._selectValue.selectLength(meta.getLayout() || {}) !== length) {
this._selectValue.setLength(meta.ensureLayout(), length);
meta.isApproximateLayout = false;

if (index !== this._data.length - 1) {
length = meta.getSeparatorLength() + length;
Expand Down Expand Up @@ -918,6 +941,7 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
);
let length = this._selectValue.selectLength((_info as ItemLayout) || {});
meta.setLayout(_info as ItemLayout);
meta.isApproximateLayout = false;
// 只有关心的值发生变化时,才会再次触发setIntervalTreeValue
if (currentLength !== length && _update) {
if (index !== this._data.length - 1) {
Expand Down Expand Up @@ -1228,7 +1252,7 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
// remainingPosition
// );

if (this._getItemLayout) {
if (this._getItemLayout || this._approximateMode) {
if (Math.abs(velocity) <= 1) {
this.updateIndices(targetIndices, {
safeRange,
Expand Down
1 change: 1 addition & 0 deletions packages/data-model/src/types/Dimensions.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export type ListDimensionsProps<ItemT> = {
canIUseRIC?: boolean;

itemApproximateLength?: number;
useItemApproximateLength?: boolean;
} & BaseDimensionsProps &
OnEndReachedHelperProps;

Expand Down

0 comments on commit bf93610

Please sign in to comment.