Skip to content

Commit

Permalink
feat: close #30
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuever committed Jun 24, 2023
1 parent 8304086 commit 5fc47b1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
24 changes: 22 additions & 2 deletions packages/data-model/src/BaseLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ON_END_REACHED_THRESHOLD,
WINDOW_SIZE,
RECYCLE_BUFFERED_COUNT,
LENGTH_PRECISION,
} from './common';
import SelectValue, {
selectHorizontalValue,
Expand All @@ -28,6 +29,7 @@ class BaseLayout {
private _recycleThreshold: number;
readonly _onEndReachedThreshold: number;
readonly _fillingMode: FillingMode;
readonly _lengthPrecision: number;
private _recycleBufferedCount: number;
private _canIUseRIC: boolean;

Expand All @@ -47,6 +49,8 @@ class BaseLayout {
recycleEnabled?: boolean;

canIUseRIC?: boolean;

lengthPrecision?: number;
}) {
const {
id,
Expand All @@ -58,6 +62,7 @@ class BaseLayout {
canIUseRIC,
stickyHeaderIndices = [],
windowSize = WINDOW_SIZE,
lengthPrecision = LENGTH_PRECISION,
recycleBufferedCount = RECYCLE_BUFFERED_COUNT,
maxToRenderPerBatch = MAX_TO_RENDER_PER_BATCH,
initialNumToRender = INITIAL_NUM_TO_RENDER,
Expand All @@ -77,15 +82,17 @@ class BaseLayout {
this._recycleThreshold = recycleEnabled
? recycleThreshold || maxToRenderPerBatch * 2
: 0;
this._recycleBufferedCount = recycleBufferedCount;

// recycleBufferedCount should greater than 0.
this._recycleBufferedCount = Math.max(recycleBufferedCount, 1);
this._stickyHeaderIndices = stickyHeaderIndices;
this._maxToRenderPerBatch = maxToRenderPerBatch;
this._initialNumToRender = initialNumToRender;
this._onEndReachedThreshold = onEndReachedThreshold;
this._recycleBufferedCount = recycleBufferedCount;
this.persistanceIndices = persistanceIndices;
this.stickyHeaderIndices = stickyHeaderIndices;
this._canIUseRIC = canIUseRIC;
this._lengthPrecision = lengthPrecision;
}

get initialNumToRender() {
Expand Down Expand Up @@ -228,6 +235,19 @@ class BaseLayout {
getSelectValue() {
return this._selectValue;
}

normalizeLengthNumber(length: number) {
return +length.toPrecision(this._lengthPrecision);
}

normalizeLengthInfo(info: ItemLayout) {
const { width, height, ...rest } = info;
return {
width: this.normalizeLengthNumber(width),
height: this.normalizeLengthNumber(height),
...rest,
};
}
}

export default BaseLayout;
9 changes: 6 additions & 3 deletions packages/data-model/src/ItemsDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ class ItemsDimensions extends BaseDimensions {
if (!meta) return false;

if (typeof info === 'number') {
const length = info;
const length = this.normalizeLengthNumber(info);
if (this._selectValue.selectLength(meta.getLayout()) !== length) {
this._selectValue.setLength(meta.getLayout(), length);
this._sortedItems.add(meta);
return true;
}
return false;
}

if (!layoutEqual(meta.getLayout(), info as ItemLayout)) {
meta.setLayout(info as ItemLayout);
const _info = this.normalizeLengthInfo(info);

if (!layoutEqual(meta.getLayout(), _info as ItemLayout)) {
meta.setLayout(_info as ItemLayout);
this._sortedItems.add(meta);
return true;
}
Expand Down
17 changes: 13 additions & 4 deletions packages/data-model/src/ListDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
if (!meta) return false;

if (typeof info === 'number') {
let length = info;
let length = this.normalizeLengthNumber(info);
if (this._selectValue.selectLength(meta.getLayout() || {}) !== length) {
this._selectValue.setLength(meta.ensureLayout(), length);

Expand All @@ -868,14 +868,23 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
return true;
}
}
return false;
}
const _info = this.normalizeLengthInfo(info);

if (!layoutEqual(meta.getLayout(), _info as ItemLayout)) {
if (meta.getLayout()) {
console.warn(
'[infinite-list/data-model] override existing key item ',
`${+key}from value ${meta.getLayout()}to ${_info}`
);
}

if (!layoutEqual(meta.getLayout(), info as ItemLayout)) {
const currentLength = this._selectValue.selectLength(
meta.getLayout() || {}
);
let length = this._selectValue.selectLength((info as ItemLayout) || {});
meta.setLayout(info as ItemLayout);
let length = this._selectValue.selectLength((_info as ItemLayout) || {});
meta.setLayout(_info as ItemLayout);
// 只有关心的值发生变化时,才会再次触发setIntervalTreeValue
if (currentLength !== length && _update) {
if (index !== this._data.length - 1) {
Expand Down
13 changes: 9 additions & 4 deletions packages/data-model/src/PseudoListDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,27 @@ class PseudoListDimensions extends BaseDimensions {
if (!meta) return false;

if (typeof info === 'number') {
const length = info;
const length = this.normalizeLengthNumber(info);
if (meta && this._selectValue.selectLength(meta.getLayout()) !== length) {
this._selectValue.setLength(meta.getLayout(), length);
if (_update) {
this.setIntervalTreeValue(index, length);
return true;
}
}
return false;
}

if (!layoutEqual(meta.getLayout(), info as ItemLayout)) {
const _info = this.normalizeLengthInfo(info);

if (!layoutEqual(meta.getLayout(), _info as ItemLayout)) {
const currentLength = this._selectValue.selectLength(
meta.getLayout() || {}
);
const length = this._selectValue.selectLength((info as ItemLayout) || {});
meta.setLayout(info as ItemLayout);
const length = this._selectValue.selectLength(
(_info as ItemLayout) || {}
);
meta.setLayout(_info as ItemLayout);

if (currentLength !== length && _update) {
this.setIntervalTreeValue(index, length);
Expand Down
1 change: 1 addition & 0 deletions packages/data-model/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const ON_END_REACHED_TIMEOUT_THRESHOLD = 200;
export const ON_END_REACHED_HANDLER_TIMEOUT_THRESHOLD = 2000;
export const DISPATCH_METRICS_THRESHOLD = 50;
export const RECYCLE_BUFFERED_COUNT = 4;
export const LENGTH_PRECISION = 4;

// 建议 ON_END_REACHED_THRESHOLD * VisibleLength > MAX_TO_RENDER_PER_BATCH * itemLength
// 这样可以在滚动停止的时候,自动获取一屏幕
Expand Down
2 changes: 2 additions & 0 deletions packages/data-model/src/types/Dimensions.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export type BaseDimensionsProps = {

canIUseRIC?: boolean;

lengthPrecision?: number;

onUpdateItemLayout?: Function;
onUpdateIntervalTree?: Function;
isIntervalTreeItems?: boolean;
Expand Down

0 comments on commit 5fc47b1

Please sign in to comment.